bool test_intersect() { 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++) for (int j = 0; j < NB_PIECES; j++) { result = result && test_equality_bool(i == j, intersect(pieces[i], pieces[j]), "intersect"); } piece pb_piece1 = new_piece_rh(3, 3, false, false); piece pb_piece2 = new_piece_rh(3, 1, false, false); result = result && test_equality_bool(true, intersect(pieces[0], pb_piece1), "set up 1 intersect pb1"); result = result && test_equality_bool(true, intersect(pb_piece2, pb_piece1), "set up 1 intersect pb2"); tear_down(); delete_piece(pb_piece1); delete_piece(pb_piece2); //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++) for (int j = 0; j < NB_PIECES; j++) { result = result && test_equality_bool(i == j, intersect(pieces[i], pieces[j]), "intersect"); } piece pb_piece3 = new_piece(3, 3, 1, 3, false, true); piece pb_piece4 = new_piece(3, 1, 1, 3, false, true); result = result && test_equality_bool(true, intersect(pieces[0], pb_piece3), "set up 2 intersect pb1"); result = result && test_equality_bool(true, intersect(pb_piece4, pb_piece3), "set up 2 intersect pb2"); tear_down(); delete_piece(pb_piece3); delete_piece(pb_piece4); return result; }
/* configue de test .....3 .....3 ...003 ...... ...122 ...1.. */ void set_up_1() { pieces[0] = new_piece_rh(3, 3, true, true); pieces[1] = new_piece_rh(3, 0, true, false); pieces[2] = new_piece_rh(4, 1, true, true); pieces[3] = new_piece_rh(5, 3, false, false); }
void test_move_piece(void) { piece test = new_piece_rh(1,2,true,false); delete_piece(test); /** * move_piece(test, LEFT, 1); * return an exit_failure : the delete_piece works ! */ piece test2 = new_piece_rh(1,2,true,false); /** * move_piece(test2, UP, -2); * exit_failure because distance<0 -> test error */ move_piece(test2, UP, 2); display_piece(test2); /** * display the piece in order to verify that move_piece(test2, UP, 2) happened */ move_piece(test2, LEFT, 2); /** * don't move the piece because the direction doesn't suit the piece */ move_piece (test2, UP, 3); /** * don't move the piece, otherwise it would go outside the board */ display_piece(test2); /* * display the piece in order to verify that the movement didn't occur */ delete_piece(test2); }
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_new_piece() { bool result = true; for (int x= 0 ; x < 5; x++) for (int y= 0 ; y < 5; y++) for (bool small=false; !small ; small= !small) for (bool horizontal=false; !horizontal ; horizontal= !horizontal) { int size; if (small) size = 2; else size = 3; piece p = new_piece_rh(x, y, small, horizontal); result = result && test_equality_int(x, get_x(p),"get_x"); result = result && test_equality_int(y, get_y(p),"get_y"); if (horizontal) { result = result && test_equality_int(1, get_height(p), "get_height"); result = result && test_equality_int(size, get_width(p), "get_width"); } else { result = result && test_equality_int(size, get_height(p), "get_height"); result = result && test_equality_int(1, get_width(p), "get_width"); } delete_piece(p); } return result; }
game init_game(int level){ FILE* f = fopen("rush_hour.txt", "r"); if(f == NULL) exit(EXIT_FAILURE); char tmp[100]; for(int i=0; i<level; ++i){ if(!fgets(tmp, 100, f)){ fprintf(stderr, "There is no level %d\n", level); exit(EXIT_FAILURE); } } const char limit[1] = "."; char* splited = strtok(tmp, limit); int nb_pieces = atoi(splited); piece pieces[nb_pieces]; char value[1]; for(int i=0; i<nb_pieces; ++i){ splited = strtok(NULL, limit); printf("%s\n", splited); value[0] = splited[0]; int x = atoi(value); value[0] = splited[1]; int y = atoi(value); value[0] = splited[2]; bool small = atoi(value); value[0] = splited[3]; bool horizontal = atoi(value); pieces[i] = new_piece_rh(x, y, small, horizontal); } fclose(f); return new_game_hr(nb_pieces, pieces); }
bool test_new_piece_rh() { bool result = true; for (int x= 0 ; x < 5; x++) for (int y= 0 ; y < 5; y++) for (bool small=false; !small ; small= !small) for (bool horizontal=false; !horizontal ; horizontal= !horizontal) { if((y>=4 && !horizontal && !small) || (x>=4 && horizontal && !small)){ break; /** * without this condition and the break, * the program is going to create pieces outside of the board. * It would activate a security in the function new_piece_rh */ } int size; if (small) size = 2; else size = 3; piece p = new_piece_rh(x, y, small, horizontal); result = result && test_equality_int(x, get_x(p),"get_x"); result = result && test_equality_int(y, get_y(p),"get_y"); if (horizontal) { result = result && test_equality_int(1, get_height(p), "get_height"); result = result && test_equality_int(size, get_width(p), "get_width"); } else { result = result && test_equality_int(size, get_height(p), "get_height"); result = result && test_equality_int(1, get_width(p), "get_width"); } delete_piece(p); } return result; }
bool test_intersect() { bool result = true; set_up(); for (int i=0; i < NB_PIECES; i++) for (int j =0; j<NB_PIECES; j++) { result = result && test_equality_bool(i==j, intersect(pieces[i], pieces[j]),"intersect"); } piece pb_piece1 = new_piece_rh(3, 3, false, false); piece pb_piece2 = new_piece_rh(3, 1, false, false); result = result && test_equality_bool(true, intersect(pieces[0], pb_piece1),"intersect pb1"); result = result && test_equality_bool(true, intersect(pb_piece2, pb_piece1),"intersect pb2"); tear_down(); delete_piece(pb_piece1); delete_piece(pb_piece2); return result; }
void copy_game (cgame src, game dst){ dst->nb_pieces = src->nb_pieces; dst->nb_moves = src->nb_moves; dst->pieces = (piece*)malloc(sizeof(src->nb_pieces*sizeof(piece*))); for(int i=0; i<src->nb_pieces; ++i){ dst->pieces[i] = new_piece_rh(0, 0, true, true); copy_piece(src->pieces[i], dst->pieces[i]); } }
void test_intersection(void) { piece test = new_piece_rh(1,2,true,false); /** * intersect(test, test2); * exit_failure because test == NULL */ piece test2 = new_piece_rh(1,2,true,false); intersect(test, test2); printf("%d \n", intersect(test, test2)); /** * display true because the two pieces are crossing each other */ piece test3 = new_piece_rh(3,3,true,false); printf("%d \n", intersect(test2, test3)); delete_piece(test); delete_piece(test2); delete_piece(test3); }
void set_up() { pieces[0] = new_piece_rh(3, 3, true, true); pieces[1] = new_piece_rh(3, 0, true, false); pieces[2] = new_piece_rh(4, 1, true, true); pieces[3] = new_piece_rh(5, 3, false, false); pieces[4] = new_piece_rh(0, 4, true, true); pieces[5] = new_piece_rh(1, 2, true, false); pieces[6] = new_piece_rh(3, 4, true, false); }
void set_up_pieces(){ pieces[0] = new_piece_rh(0, 3, true, true); pieces[1] = new_piece_rh(0, 4, false, true); pieces[2] = new_piece_rh(1, 0, false, false); pieces[3] = new_piece_rh(3, 1, true, false); pieces[4] = new_piece_rh(4, 2, true, true); pieces[5] = new_piece_rh(3, 0, false, true); pieces[6] = new_piece_rh(4, 3, false, false); }
game new_game_hr (int nb_pieces, piece *pieces){ game g = (game)malloc(sizeof(struct game_s)); g->nb_pieces = nb_pieces; g->nb_moves = 0; g->pieces = (piece*)malloc(nb_pieces*sizeof(piece)); for(int i=0; i<nb_pieces; ++i){ g->pieces[i] = new_piece_rh(0, 0, true, true); copy_piece(pieces[i], g->pieces[i]); } return g; }
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; }
void copy_game(cgame src, game dst) { dst->nb_pieces=src->nb_pieces; dst->nb_moves=src->nb_moves; dst->piece_list=malloc(sizeof (piece)*src->nb_pieces); for (int i=0; i<src->nb_pieces; i++) { dst->piece_list[i]=new_piece_rh(0, 0, false, false);// create a new piece copy_piece(src->piece_list[i], dst->piece_list[i]);// overwrite new piece with the wanted copy } dst->width=src->width; dst->height=src->height; dst->dir_prev=malloc(sizeof(int)*src->nb_pieces); for (int i=0; i<src->nb_pieces; i++) { dst->dir_prev[i]=src->dir_prev[i]; } }
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 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; }
/* * @brief Show different game and wait for user to choose * @return game The new game created */ static game select_game() { int row, col; int pos = 0; int ch = 0; game newGame = NULL; while (ch != '\n') { clear(); getmaxyx(stdscr, row, col); mvprintw(row / 2, col / 2 - 20, "Use arrow key to select your game"); if (pos == 0) { mvprintw(row / 2 + 1, col / 2 - 20, "-> Rush Hour"); mvprintw(row / 2 + 2, col / 2 - 20, " Ane rouge"); } else { mvprintw(row / 2 + 1, col / 2 - 20, " Rush Hour"); mvprintw(row / 2 + 2, col / 2 - 20, "-> Ane rouge"); } ch = getch(); if (ch == KEY_UP) { if (pos > 0) pos--; } else if (ch == KEY_DOWN) { if (pos < 1) pos++; } refresh(); } if (pos == 0) { //Rush hour piece pieces[4]; pieces[0] = new_piece_rh(3, 3, true, true); pieces[1] = new_piece_rh(3, 0, true, false); pieces[2] = new_piece_rh(4, 1, true, true); pieces[3] = new_piece_rh(5, 3, false, false); newGame = new_game(6, 6, 4, pieces); MAXCOL = game_width(newGame); MAXROW = game_height(newGame); MINH = MAXROW * SIZE + 2; MINW = MAXCOL * SIZE; game_over = game_over_hr; gameOverRh = true; for (int i = 0; i < game_nb_pieces(newGame); i++) { delete_piece(pieces[i]); } clear(); refresh(); return newGame; } else { //Ane rouge piece pieces[10]; pieces[0] = new_piece(1, 3, 2, 2, true, true); //Rouge pieces[1] = new_piece(3, 3, 1, 2, true, true); // 2 pieces[2] = new_piece(3, 1, 1, 2, true, true); // 3 pieces[3] = new_piece(3, 0, 1, 1, true, true); // 4 pieces[4] = new_piece(1, 2, 2, 1, true, true); // 5 pieces[5] = new_piece(2, 1, 1, 1, true, true); // 6 pieces[6] = new_piece(1, 1, 1, 1, true, true); // 7 pieces[7] = new_piece(0, 0, 1, 1, true, true); // 8 pieces[8] = new_piece(0, 1, 1, 2, true, true); // 9 pieces[9] = new_piece(0, 3, 1, 2, true, true); // 10 newGame = new_game(4, 5, 10, pieces); MAXCOL = game_width(newGame); MAXROW = game_height(newGame); MINH = MAXROW * SIZE + 2; MINW = MAXCOL * SIZE; game_over = game_over_an; gameOverRh = false; for (int i = 0; i < game_nb_pieces(newGame); i++) { delete_piece(pieces[i]); } clear(); refresh(); return newGame; } }
bool test_new_piece() { bool result = true; for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { for (int compt_small = 0; compt_small < 2; ++compt_small) { for (int compt_horizontal = 0; compt_horizontal < 2; ++compt_horizontal) { int size; bool small; bool horizontal; if (compt_small == 0) { small = true; size = 2; } else { small = false; size = 3; } if (compt_horizontal == 0) horizontal = true; else horizontal = false; piece p = new_piece_rh(x, y, small, horizontal); result = result && test_equality_int(x, get_x(p), "get_x"); result = result && test_equality_int(y, get_y(p), "get_y"); if (horizontal) { result = result && test_equality_int(1, get_height(p), "get_height"); result = result && test_equality_int(size, get_width(p), "get_width"); result = result && test_equality_bool(true, can_move_x(p), "can_move_x"); result = result && test_equality_bool(false, can_move_y(p), "can_move_y"); } else { result = result && test_equality_int(size, get_height(p), "get_height"); result = result && test_equality_int(1, get_width(p), "get_width"); result = result && test_equality_bool(false, can_move_x(p), "can_move_x"); result = result && test_equality_bool(true, can_move_y(p), "can_move_y"); } delete_piece(p); } } for (int width = 1; width < 4; width++) { for (int height = 1; height < 4; height++) { for (int compt_move_x = 0; compt_move_x < 2; ++compt_move_x) { for (int compt_move_y = 0; compt_move_y < 2; ++compt_move_y) { bool move_x; bool move_y; if (compt_move_x == 0) move_x = true; else move_x = false; if (compt_move_y == 0) move_y = true; else move_y = false; piece p = new_piece(x, y, width, height, move_x, move_y); result = result && test_equality_int(x, get_x(p), "get_x"); result = result && test_equality_int(y, get_y(p), "get_y"); result = result && test_equality_int(height, get_height(p), "get_height"); result = result && test_equality_int(width, get_width(p), "get_width"); if (move_x) result = result && test_equality_bool(true, can_move_x(p), "can_move_x"); else result = result && test_equality_bool(false, can_move_x(p), "not can_move_x"); if (move_y) result = result && test_equality_bool(true, can_move_y(p), "can_move_y"); else result = result && test_equality_bool(false, can_move_y(p), "not can_move_y"); delete_piece(p); } } } } } } return result; }