bool test_copy() { piece p = new_piece_rh(0, 0, true, true); bool result = true; //the first one (set_up_1()) is with new_piece_rh (int x, int y, bool small, bool horizontal) set_up_1(); for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height"); result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width"); result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x"); result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y"); result = result && test_equality_bool(is_horizontal(pieces[i]), is_horizontal(p), "copy is_horizontal"); result = result && test_equality_bool(can_move_x(pieces[i]), can_move_x(p), "copy can_move_x"); result = result && test_equality_bool(can_move_y(pieces[i]), can_move_y(p), "copy can_move_y"); } tear_down(); //the second one (set_up_2()) is with new_piece(int x, int y, int width, int height, bool move_x, bool move_y) set_up_2(); for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height"); result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width"); result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x"); result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y"); result = result && test_equality_bool(is_horizontal(pieces[i]), is_horizontal(p), "copy is_horizontal"); result = result && test_equality_bool(can_move_x(pieces[i]), can_move_x(p), "copy can_move_x"); result = result && test_equality_bool(can_move_y(pieces[i]), can_move_y(p), "copy can_move_y"); } tear_down(); delete_piece(p); return result; }
bool test_move() { bool result = true; piece p = new_piece_rh(0, 0, true, true); set_up(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i=0; i < NB_PIECES; i++) { copy_piece(pieces[i],p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i])-dist,get_x(p),"move LEFT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move LEFT"); copy_piece(pieces[i],p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i])+dist,get_x(p),"move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move RIGHT"); copy_piece(pieces[i],p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i])+dist,get_y(p),"move UP"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move UP"); copy_piece(pieces[i],p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i])-dist,get_y(p),"move DOWN"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move DOWN"); } tear_down(); delete_piece(p); return result; }
bool test_equality_piece(cpiece expected, cpiece value, char *msg) { if ((get_x(expected) != get_x(value)) || (get_y(expected) != get_y(value)) || (is_horizontal(expected) != is_horizontal(value)) || (is_small(expected) != is_small(value))) { fprintf(stderr, "%s expected piece is not equal to value piece\n", msg); return false; } return true; }
bool piece_equals(cpiece P1, cpiece P2){ bool res = true; res = res && get_x(P1) == get_x(P2); res = res && get_y(P1) == get_y(P2); res = res && get_height(P1) == get_height(P2); res = res && get_width(P1) == get_width(P2); res = res && is_horizontal(P1) == is_horizontal(P2); return res; }
/** * \copydoc Detector::test_collision_custom */ bool Separator::test_collision_custom(Entity& entity) { // Trigger the collision if the center point crosses the middle of the // separator. const Point& separator_center = get_center_point(); const Point& center = entity.get_center_point(); if (!overlaps(center)) { return false; } if (is_horizontal()) { if (center.y < separator_center.y) { // The entity is above the separator. return center.y == separator_center.y - 1; } else { // The entity is below the separator. return center.y == separator_center.y; } } else { if (center.x < separator_center.x) { // The entity is west of the separator. return center.x == separator_center.x - 1; } else { // The entity is east of the separator. return center.x == separator_center.x; } } }
bool play_move(game g, int piece_num, dir d, int distance){ piece p = g->pieces[piece_num]; if(is_horizontal(p)){ if(d == UP || d == DOWN) return false; }else{ if(d == RIGHT || d == LEFT) return false; } int cover_distance; for(cover_distance = 1; cover_distance<=distance; ++cover_distance){ move_piece(p, d, 1); if(!is_in_game(g, piece_num) || is_above_piece(g, piece_num)){ if(d == RIGHT) move_piece(p, LEFT, cover_distance); else if(d == LEFT) move_piece(p, RIGHT, cover_distance); else if(d == UP) move_piece(p, DOWN, cover_distance); else move_piece(p, UP, cover_distance); return false; } } g->nb_moves += distance; return true; }
MLV_Image* init_car(game newGame, int indice_piece) { char size[6]; char orientation[2]; char color[30]; char path[50]; cpiece tmp = game_piece(newGame, indice_piece); int width = get_width(tmp) * RATIO; int height = get_height(tmp) * RATIO; if (get_width(tmp) >= 3 || get_height(tmp) >= 3) sprintf(size, "truck"); else sprintf(size, "car"); is_horizontal(tmp) ? sprintf(orientation, "h") : sprintf(orientation, "v"); sprintf(color, "%s", carColor[indice_piece % NB_OF_COLOR]); sprintf(path, "../../assets/%s_%s_%s.png", size, color, orientation); if (indice_piece == 0) sprintf(path, "../../assets/car_red_h.png"); MLV_Image *tmp_img = MLV_load_image(path); if (!tmp_img) { return NULL; } MLV_resize_image(tmp_img, width, height); return tmp_img; }
bool test_copy() { piece p = new_piece_rh(0, 0, true, true); bool result = true; set_up(); for (int i = 0 ; i < NB_PIECES; i++) { copy_piece(pieces[i],p); result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height"); result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width"); result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x"); result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y"); result = result && test_equality_bool(is_horizontal(pieces[i]), is_horizontal(p), "copy is_horizontal"); } tear_down(); delete_piece(p); return result; }
bool good_direction(game g, int piece_num, dir d){ cpiece p = game_piece(g, piece_num); if(is_horizontal(p)){ if(d == UP || d == DOWN) return false; }else{ if(d == RIGHT || d == LEFT) return false; } return true; }
// return the number of the piece in (,y) or -1 int is_piece(game g, int x, int y){ for(int i=0; i<game_nb_pieces(g); ++i){ cpiece p = game_piece(g, i); int x_piece = get_x(p); int y_piece = get_y(p); if(x_piece == x && !is_horizontal(p)){ if(y_piece == y || y_piece == y-1) return i; if(get_height(p) == 3 && y_piece == y-2) return i; } if(y_piece == y && is_horizontal(p)){ if(x_piece == x || x_piece == x-1) return i; if(get_width(p) == 3 && x_piece == x-2) return i; } } return -1; }
bool is_in_box(const Point_<TYPE, DIM> &pt) const { ASSERT(!empty()); if (is_horizontal()) { return (((psx() <= pt.x) && (pt.x <= pex())) || ((pex() <= pt.x) && (pt.x <= psx()))); } if (is_vertical()) { return (((psy() <= pt.y) && (pt.y <= pey())) || ((pey() <= pt.y) && (pt.y <= psy()))); } return (((psx() <= pt.x) && (pt.x <= pex())) || ((pex() <= pt.x) && (pt.x <= psx()))) && (((psy() <= pt.y) && (pt.y <= pey())) || ((pey() <= pt.y) && (pt.y <= psy()))); }
void test_failure_piece(cpiece src) { piece dst = NULL; copy_piece(src, dst); move_piece(dst, LEFT, 1); intersect(src, src); get_y(src); get_x(src); get_width(src); get_height(src); is_horizontal(src); can_move_x(src); can_move_y(src); delete_piece(dst); }
WINDOW *create_newcar(cpiece newPiece, int number, bool selected, int spaceBetween, int MAXROW) { WINDOW *local_win; int height = 0; int width = 0; int starty = get_y(newPiece); int startx = get_x(newPiece); //Mirror y axis starty = MAXROW - starty - get_height(newPiece); bool horizontal = is_horizontal(newPiece); //Setup size if (horizontal) { height = spaceBetween * get_height(newPiece); width = spaceBetween * get_width(newPiece)*2; } else if (!horizontal) { height = spaceBetween * get_height(newPiece); width = spaceBetween * get_width(newPiece)*2 + 1; } //End setup //New car with border local_win = newwin(height - 1, width - 2, starty * spaceBetween + 1, startx * spaceBetween * 2 + 1); if (number == 0) { wattron(local_win, COLOR_PAIR(1)); //COLOR init with init_pair wborder(local_win, '|', '|', '-', '-', '+', '+', '+', '+'); wattroff(local_win, COLOR_PAIR(1)); } else { wborder(local_win, '|', '|', '-', '-', '+', '+', '+', '+'); /* 0, 0 gives default characters * for the vertical and horizontal * lines */ } //Print number of the vehicle with color if (selected) { wattron(local_win, COLOR_PAIR(2)); //COLOR init with init_pair wattron(local_win, A_BOLD); //Bold char mvwprintw(local_win, (height - 1) / 2, (width - 2) / 2, "%d", number); wattroff(local_win, A_BOLD); wattroff(local_win, COLOR_PAIR(2)); } else { wattron(local_win, COLOR_PAIR(1)); //COLOR init with init_pair wattron(local_win, A_BOLD); //Bold char mvwprintw(local_win, (height - 1) / 2, (width - 2) / 2, "%d", number); wattroff(local_win, A_BOLD); wattroff(local_win, COLOR_PAIR(1)); } wmove(local_win, 0, 0); //Set cur pos to top left car wrefresh(local_win); /* Show that box */ return local_win; }
bool good_direction(game g, int piece_num, dir d){ //Verify if you can move to the direction d cpiece p = game_piece(g, piece_num); int x_piece = get_x(p); int y_piece = get_y(p); if(is_horizontal(p)){ if(d == UP || d == DOWN){ printf("The piece %d cannot move vertycaly\n", piece_num); return false; } if(d == RIGHT){ //Verify if the piece is not nears the limit board and if there is not an other piece sticks to hir if(x_piece+get_width(p) >= SIZE_ARRAY || is_piece(g, x_piece+get_width(p),y_piece) != -1){ printf("The piece %d cannot move to right\n", piece_num); return false; } } if(d == LEFT){ if(x_piece-1 < 0 || is_piece(g, x_piece-1, y_piece) != -1){ printf("The piece %d cannot move to left\n", piece_num); return false; } } }else{ if(d == RIGHT || d == LEFT){ printf("The piece %d cannot move horizontaly\n", piece_num); return false; } if(d == UP){ if(y_piece+get_height(p) >= SIZE_ARRAY || is_piece(g, x_piece, y_piece+get_height(p)) != -1){ printf("The piece %d cannot move to up\n", piece_num); return false; } } if(d == DOWN){ if(y_piece-1 < 0 || is_piece(g, x_piece, y_piece-1) != -1){ printf("The piece %d cannot move to down\n", piece_num); return false; } } } return true; }
int main() { test_intersection(); /** * creation of 3 pieces */ piece test = new_piece_rh(1,2,true,false); piece test2 = new_piece_rh(2,2,false, false); piece test3 = new_piece_rh(0,0,true,true); display_piece(test); display_piece(test2); /** * display the two first pieces */ copy_piece(test, test3); /** * copy_piece is supposed to copy test in test3 */ display_piece(test3); /** * display test3 in order to verify that it's the same as test. - OK */ delete_piece(test); printf("\nx : %d \ny : %d \nhauteur : %d \nlongueur : %d\n", get_x(test2), get_y(test2), get_height(test2), get_width(test2)); /** * test of the getters */ printf("\nhorizontal : %d \n", is_horizontal(test2)); /** * test is_horizontal */ printf("\nin board : %d \n", is_in_board(test2)); /** * test is_in_board */ delete_piece(test2); delete_piece(test3); return EXIT_SUCCESS; }
bool can_move(game g, int piece_num){ cpiece p = game_piece(g, piece_num); int x_piece = get_x(p); int y_piece = get_y(p); if(is_horizontal(p)){ //Check if the piece is not nears the limit board and if there is not an other piece sticks to hir if(x_piece-1 >= 0 && x_piece+get_width(p) < SIZE_ARRAY) return is_piece(g, x_piece-1, y_piece) == -1 || is_piece(g, x_piece+get_width(p), y_piece) == -1; if(x_piece+get_width(p) < SIZE_ARRAY) return is_piece(g, x_piece+get_width(p), y_piece) == -1; if(x_piece-1 >= 0) return is_piece(g, x_piece-1, y_piece) == -1; }else{ if(y_piece-1 >= 0 && y_piece+get_height(p) < SIZE_ARRAY) return is_piece(g, x_piece, y_piece-1) == -1 || is_piece(g, x_piece, y_piece+get_height(p)) == -1; if(y_piece+get_height(p) < SIZE_ARRAY) return is_piece(g, x_piece, y_piece+get_height(p)) == -1; if(y_piece-1 >= 0) return is_piece(g, x_piece, y_piece-1) == -1; } return false; }
void set_up_board(game g){ for(int x=0; x<SIZE_ARRAY; ++x){ for(int y=0; y<SIZE_ARRAY; ++y) board[x][y] = 100; } for(int y=0; y<SIZE_ARRAY; ++y){ for(int x=0; x<SIZE_ARRAY; ++x){ for(char piece_num=0; piece_num<NB_PIECES; ++piece_num){ cpiece p = game_piece(g, piece_num); if(get_x(p) == x && get_y(p) == y){ if(is_horizontal(p)){ for(int i=0; i<get_width(p); ++i) board[x+i][y] = piece_num; break; }else{ for(int i=0; i<get_height(p); ++i) board[x][y+i] = piece_num; break; } } } } } }
int main(int argc, char *argv[]){ set_up_pieces(); game g = new_game_hr(NB_PIECES, pieces); char buf[3][100]; int piece_num; dir d; int distance; while(!game_over_hr(g)){ bool good = false; while(!good){ while(!good){ set_up_board(g); print_game(g); printf("Move the pieces for free the piece 0 to the exit:\n"); printf("Total move: %d\n",game_nb_moves(g)); while(!good){ printf("What piece do you want to move?\n"); read(0, buf[0], sizeof(char)*100); if(strcmp(buf[0], "cancel") == 10) break; if(strcmp(buf[0], "exit") == 10) return EXIT_SUCCESS; if(buf[0][0]<48 || buf[0][0]>=48+NB_PIECES || buf[0][1] != 10) printf("Write a number between 0 and %d\tor write cancel or exit.\n",NB_PIECES-1); else{ piece_num = atoi(buf[0]); good = true; } } if(!good) break; good = false; while(!good){ printf("In what direction?\n"); read(0, buf[1], sizeof(char)*100); if(strcmp(buf[1], "cancel") == 10) break; if(strcmp(buf[1], "exit") == 10) return EXIT_SUCCESS; if(!is_dir_option(buf[1])) printf("Write one of those direction: up, down, right, left\tor write cancel or exit.\n"); else{ for(int i=0; i<4; ++i){ if(strcmp(buf[1], direction[i].dir_name) == 10) d = direction[i].dir_option; } if(!good_direction(g, piece_num, d)){ if(is_horizontal(game_piece(g, piece_num))) printf("The piece %d cannot move vertycaly\n", piece_num); else printf("The piece %d cannot move horizontaly\n", piece_num); }else good = true; } } if(!good) break; good = false; while(!good){ printf("How many case?\n"); read(0, buf[2], sizeof(char)*100); if(strcmp(buf[2], "cancel") == 10) break; if(strcmp(buf[2], "exit") == 10) return EXIT_SUCCESS; if(buf[2][0]<48 || buf[2][0]>=48+SIZE_ARRAY || buf[2][1] != 10) printf("Write a number between 0 and %d\tor write cancel or exit.\n",SIZE_ARRAY-1); else{ distance = atoi(buf[2]); good = play_move(g, piece_num, d, distance); if(!good) printf("The piece %d cannot move to that case.\n", piece_num); } } } } } set_up_board(g); print_game(g); printf("CONGRETULATION\nYou won in %d moves\n", game_nb_moves(g)); return EXIT_SUCCESS; }
bool test_move() { bool result = true; piece p = new_piece_rh(0, 0, true, true); //the first one (set_up_1()) is with new_piece_rh (int x, int y, bool small, bool horizontal) set_up_1(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) - dist, get_x(p), "move LEFT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move LEFT"); copy_piece(pieces[i], p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) + dist, get_x(p), "move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move RIGHT"); copy_piece(pieces[i], p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) + dist, get_y(p), "move UP"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move UP"); copy_piece(pieces[i], p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) - dist, get_y(p), "move DOWN"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move DOWN"); } tear_down(); //the second one (set_up_2()) is with new_piece(int x, int y, int width, int height, bool move_x, bool move_y) set_up_2(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) - dist, get_x(p), "move LEFT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move LEFT"); copy_piece(pieces[i], p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) + dist, get_x(p), "move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move RIGHT"); copy_piece(pieces[i], p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) + dist, get_y(p), "move UP"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move UP"); copy_piece(pieces[i], p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) - dist, get_y(p), "move DOWN"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move DOWN"); } tear_down(); delete_piece(p); return result; return false; }