Merge branch 'main' of gitlab.emi.u-bordeaux.fr:pt2/teams/a42/lightup-a42e

This commit is contained in:
Théophyl Hoguet committed 2022-01-19 14:06:25 +01:00
commit 637760c76d
4 files changed
+76 -105

No files matched your search

+9 -3
View File
@@ -51,6 +51,7 @@ game game_copy(cgame g1) {
g2->tab_cell[i][j] = g1->tab_cell[i][j]; g2->tab_cell[i][j] = g1->tab_cell[i][j];
} }
} }
// delete first allocated hist (by game new) of g2
history_delete_entire_history(g2->hist); history_delete_entire_history(g2->hist);
g2->hist = history_copy(g1->hist); g2->hist = history_copy(g1->hist);
return g2; return g2;
@@ -70,6 +71,7 @@ bool game_equal(cgame g1, cgame g2) {
} }
void game_delete(game g) { void game_delete(game g) {
// free each tab of tabcell
for (int i = 0; i < g->height; i++) { for (int i = 0; i < g->height; i++) {
free(g->tab_cell[i]); free(g->tab_cell[i]);
g->tab_cell[i] = NULL; g->tab_cell[i] = NULL;
@@ -133,7 +135,7 @@ bool game_is_black(cgame g, uint i, uint j) {
int game_get_black_number(cgame g, uint i, uint j) { int game_get_black_number(cgame g, uint i, uint j) {
if (game_get_state(g, i, j) == S_BLACKU) if (game_get_state(g, i, j) == S_BLACKU)
return -1; return -1;
return game_get_state(g, i, j) - 8; return game_get_state(g, i, j) - S_BLACK;
} }
bool game_is_marked(cgame g, uint i, uint j) { bool game_is_marked(cgame g, uint i, uint j) {
@@ -149,16 +151,19 @@ bool game_has_error(cgame g, uint i, uint j) {
} }
bool game_check_move(cgame g, uint i, uint j, square s) { bool game_check_move(cgame g, uint i, uint j, square s) {
return !(i >= g->height || j >= g->width || (s != S_BLANK && s != S_MARK && s != S_LIGHTBULB) || game_is_black(g, i, j)); return (i < g->height && j < g->width && (s == S_BLANK || s == S_MARK || s == S_LIGHTBULB) && !game_is_black(g, i, j));
} }
void game_play_move(game g, uint i, uint j, square s) { void game_play_move(game g, uint i, uint j, square s) {
// shouldn't happen because we initalized in game new
if (g->hist == NULL) { if (g->hist == NULL) {
g->hist = history_create_empty(); g->hist = history_create_empty();
g->hist = history_insert_first(g->hist, game_get_state(g, i, j), i, j); g->hist = history_insert_first(g->hist, game_get_state(g, i, j), i, j);
} else { } else {
g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_state(g, i, j), i, j)); // to avoid redo after play
g->hist = history_delete_all_after(g->hist, g->hist); g->hist = history_delete_all_after(g->hist, g->hist);
// get the last element of
g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_state(g, i, j), i, j));
} }
game_set_square(g, i, j, s); game_set_square(g, i, j, s);
game_update_flags(g); game_update_flags(g);
@@ -274,5 +279,6 @@ void game_restart(game g) {
} }
game_update_flags(g); game_update_flags(g);
history_delete_entire_history(g->hist); history_delete_entire_history(g->hist);
// insert f_error on first cell of histo. initalized to avoid bug on first move undo
g->hist = history_insert_first(history_create_empty(), F_ERROR, 0, 0); g->hist = history_insert_first(history_create_empty(), F_ERROR, 0, 0);
} }
+3
View File
@@ -26,11 +26,13 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) {
g->width = nb_cols; g->width = nb_cols;
g->hist = history_create_empty(); g->hist = history_create_empty();
// alloc the tab with variables set before // alloc the tab with variables set before
// alloc first tab
g->tab_cell = (square**)malloc(sizeof(square*) * g->height); g->tab_cell = (square**)malloc(sizeof(square*) * g->height);
if (g->tab_cell == NULL) { if (g->tab_cell == NULL) {
fprintf(stderr, "not enough memory\n"); fprintf(stderr, "not enough memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// alloc each tab of tab_cell
for (int i = 0; i < g->height; i++) { for (int i = 0; i < g->height; i++) {
g->tab_cell[i] = (square*)calloc(g->width, sizeof(square)); g->tab_cell[i] = (square*)calloc(g->width, sizeof(square));
if (g->tab_cell[i] == NULL) { if (g->tab_cell[i] == NULL) {
@@ -38,6 +40,7 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
// insert f_error on first cell of histo. initalized to avoid bug on first move undo
g->hist = history_insert_first(g->hist, F_ERROR, 0, 0); g->hist = history_insert_first(g->hist, F_ERROR, 0, 0);
return g; return g;
} }
+59 -100
View File
@@ -15,31 +15,19 @@ bool test_dummy() {
/* ********** game_get_black_number ********** */ /* ********** game_get_black_number ********** */
bool test_game_get_black_number() { bool test_game_get_black_number() {
square tab_square[] = {S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4};
game g = game_new_empty(); game g = game_new_empty();
game_set_square(g, 0, 0, S_BLACK0); for (int i = 0; i < DEFAULT_SIZE; i++) {
if (game_get_black_number(g, 0, 0) != 0) { for (int j = 0; j < DEFAULT_SIZE; j++) {
game_delete(g); // check for each case given in tab_square if the function return the expected result
return false; for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) {
} game_set_square(g, i, j, tab_square[tab_index]);
game_set_square(g, 0, 0, S_BLACK1); if (game_get_black_number(g, i, j) != tab_index) {
if (game_get_black_number(g, 0, 0) != 1) { game_delete(g);
game_delete(g); return false;
return false; }
} }
game_set_square(g, 0, 0, S_BLACK2); }
if (game_get_black_number(g, 0, 0) != 2) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK3);
if (game_get_black_number(g, 0, 0) != 3) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK4);
if (game_get_black_number(g, 0, 0) != 4) {
game_delete(g);
return false;
} }
game_delete(g); game_delete(g);
return true; return true;
@@ -47,100 +35,71 @@ bool test_game_get_black_number() {
/* ********** game_is_black ********** */ /* ********** game_is_black ********** */
bool test_game_is_black() { bool test_game_is_black() {
square tab_square[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU};
game g = game_new_empty(); game g = game_new_empty();
if (game_is_black(g, 0, 0)) { for (int i = 0; i < DEFAULT_SIZE; i++) {
game_delete(g); for (int j = 0; j < DEFAULT_SIZE; j++) {
return false; // check for each case given in tab_square if the function return the expected result
} for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) {
game_set_square(g, 0, 0, S_BLACKU); game_set_square(g, i, j, tab_square[tab_index]);
if (!game_is_black(g, 0, 0)) { if (tab_square[tab_index] <= S_MARK) {
game_delete(g); if (game_is_black(g, i, j)) {
return false; game_delete(g);
} return false;
if (!((game_get_square(g, 0, 0) == S_BLACKU) && game_is_black(g, 0, 0))) { }
game_delete(g); } else {
return false; if (!((game_get_square(g, i, j) == tab_square[tab_index]) && game_is_black(g, i, j))) {
} game_delete(g);
game_set_square(g, 0, 0, S_BLACK0); return false;
if (!((game_get_square(g, 0, 0) == S_BLACK0) && game_is_black(g, 0, 0))) { }
game_delete(g); }
return false; }
} }
game_set_square(g, 0, 0, S_BLACK1);
if (!((game_get_square(g, 0, 0) == S_BLACK1) && game_is_black(g, 0, 0))) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK2);
if (!((game_get_square(g, 0, 0) == S_BLACK2) && game_is_black(g, 0, 0))) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK3);
if (!((game_get_square(g, 0, 0) == S_BLACK3) && game_is_black(g, 0, 0))) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK4);
if (!((game_get_square(g, 0, 0) == S_BLACK4) && game_is_black(g, 0, 0))) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_MARK);
if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_MARK);
if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) {
game_delete(g);
return false;
} }
game_delete(g);
return true;
}
// changer l'intérieur de la boucle
/* ********** game_is_lightbulb ********** */
bool test_game_is_lightbulb() {
square tab_square[] = {S_BLANK, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU};
game g = game_new_empty();
game_set_square(g, 0, 0, S_LIGHTBULB); game_set_square(g, 0, 0, S_LIGHTBULB);
if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { if (!((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0))) {
game_delete(g); game_delete(g);
return false; return false;
} }
game_set_square(g, 0, 0, S_LIGHTBULB); for (int i = 0; i < DEFAULT_SIZE; i++) {
if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { for (int j = 0; j < DEFAULT_SIZE; j++) {
game_delete(g); // check for each case given in tab_square if the function return the expected result
return false; for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) {
} game_set_square(g, i, j, tab_square[tab_index]);
game_set_square(g, 0, 0, F_ERROR); if (game_get_black_number(g, i, j) != tab_index) {
if ((game_get_square(g, 0, 0) == F_ERROR) && game_is_black(g, 0, 0)) { game_delete(g);
game_delete(g); return false;
return false; }
} }
game_set_square(g, 0, 0, F_LIGHTED); }
if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_black(g, 0, 0)) {
game_delete(g);
return false;
} }
game_delete(g); game_delete(g);
return true; return true;
} }
/* ********** game_is_lightbulb ********** */
bool test_game_is_lightbulb() {
game g = game_new_empty();
game_set_square(g, 0, 0, S_LIGHTBULB);
if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0)) {
game_delete(g);
return true;
}
game_delete(g);
return false;
}
/* ********** game_is_blank ********** */ /* ********** game_is_blank ********** */
bool test_game_is_blank() { bool test_game_is_blank() {
game g = game_new_empty(); game g = game_new_empty();
if (!game_is_blank(g, 0, 0)) {
game_delete(g);
return false;
}
game_set_square(g, 0, 0, S_BLACK0);
if (game_is_blank(g, 0, 0)) { if (game_is_blank(g, 0, 0)) {
game_delete(g); game_delete(g);
return true; return false;
} }
game_delete(g); game_delete(g);
return false; return true;
} }
/* ********** game_delete ********** */ /* ********** game_delete ********** */
+5 -2
View File
@@ -29,10 +29,13 @@ bool checkerrors(cgame g, char* stringError) {
if (game_has_error(g, i, j)) { if (game_has_error(g, i, j)) {
// if error append stringError with coordinate of the error, // if error append stringError with coordinate of the error,
stringError[index_taberr] = i + CHAR0ASCII; stringError[index_taberr] = i + CHAR0ASCII;
index_taberr++;
stringError[index_taberr] = ':'; stringError[index_taberr] = ':';
index_taberr++;
stringError[index_taberr] = j + CHAR0ASCII; stringError[index_taberr] = j + CHAR0ASCII;
index_taberr++;
stringError[index_taberr] = ' '; stringError[index_taberr] = ' ';
index_taberr = index_taberr + NBCHARERR; index_taberr++;
} }
} }
} }
@@ -96,7 +99,7 @@ int main(int argc, char* argv[]) {
while (!game_is_over(g)) { while (!game_is_over(g)) {
// print errors // print errors
if (checkerrors(g, stringError)) if (checkerrors(g, stringError))
printf("Il y a un problème case(s): %s .\n", stringError); printf("Il y a un problème case(s): %s\n", stringError);
printf("Entrez une commande (h pour afficher l'aide): "); printf("Entrez une commande (h pour afficher l'aide): ");
// scan the first command without args // scan the first command without args
if (scanf(" %c", &c)) { if (scanf(" %c", &c)) {