bool test_new_piece() { bool result = true; for(int y = 0; y < 5; y++){ for(int x = 0; x < 5; x++){ for(bool move_y = false; !move_y; move_y = !move_y){ for(bool move_x = false; !move_x; move_x = !move_x){ int sizeX = 5 - x; int sizeY = 5 - y; piece p = new_piece(x, y, sizeX, sizeY, move_x, move_y); // Test coordonnées de p result = result && test_equality_int(x, get_x(p), "get_x"); result = result && test_equality_int(y, get_y(p), "get_y"); // Test hauteur/largeur de p result = result && test_equality_int(sizeY, get_height(p), "get_height"); result = result && test_equality_int(sizeX, get_width(p), "get_width"); // Test mouvement sur x result = result && test_equality_bool(move_x, can_move_x(p), "move_x"); // Test mouvement sur y result = result && test_equality_bool(move_y, can_move_y(p), "move_y"); delete_piece(p); } } } } return result; }
void tear_down() { for (int i = 0 ; i < NB_PIECES; i++) delete_piece(pieces[i]); }
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; }
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; }
void delete_game (game g){ for(int i=0;i<g->nb_pieces;i++) delete_piece(g->pieces[i]); free(g->pieces); free(g); }
void delete_pieces_array(piece *array, int size){ for(int i=0; i<size; i++){ delete_piece(array[i]); } free(array); }
/* * @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; } }