replaced g->height, g->width and g->wrapping

This commit is contained in:
Théophyl Hoguet committed 2022-01-21 22:13:31 +01:00
1 parent 0403d6e9bb
commit 75db9fa6fb
5 files changed
+59 -55

No files matched your search

+6 -5
View File
@@ -1,6 +1,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "game.h" #include "game.h"
#include "game_ext.h"
#include "game_private.h" #include "game_private.h"
char charPrintCell(cgame g, uint i, uint j) { char charPrintCell(cgame g, uint i, uint j) {
@@ -16,23 +17,23 @@ void game_print(cgame g) {
if (g != NULL) { if (g != NULL) {
uint i; uint i;
printf(" 0"); printf(" 0");
for (i = 1; i < g->width; i++) { for (i = 1; i < game_nb_cols(g); i++) {
printf("%u", i); printf("%u", i);
} }
printf("\n -"); printf("\n -");
for (i = 1; i < g->width; i++) { for (i = 1; i < game_nb_cols(g); i++) {
printf("-"); printf("-");
} }
printf("\n"); printf("\n");
for (i = 0; i < g->height; i++) { for (i = 0; i < game_nb_rows(g); i++) {
printf("%u |", i); printf("%u |", i);
for (uint j = 0; j < g->width; j++) { for (uint j = 0; j < game_nb_cols(g); j++) {
printf("%c", charPrintCell(g, i, j)); printf("%c", charPrintCell(g, i, j));
} }
printf("|\n"); printf("|\n");
} }
printf(" -"); printf(" -");
for (i = 1; i < g->width; i++) { for (i = 1; i < game_nb_cols(g); i++) {
printf("-"); printf("-");
} }
printf("\n"); printf("\n");
+6 -6
View File
@@ -5,9 +5,9 @@
game game_new_ext(uint nb_rows, uint nb_cols, square* squares, bool wrapping) { game game_new_ext(uint nb_rows, uint nb_cols, square* squares, bool wrapping) {
game g = game_new_empty_ext(nb_rows, nb_cols, wrapping); game g = game_new_empty_ext(nb_rows, nb_cols, wrapping);
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
g->tab_cell[i][j] = squares[g->width * i + j]; g->tab_cell[i][j] = squares[game_nb_cols(g) * i + j];
} }
} }
return g; return g;
@@ -27,14 +27,14 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) {
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 // alloc first tab
g->tab_cell = (square**)malloc(sizeof(square*) * g->height); g->tab_cell = (square**)malloc(sizeof(square*) * game_nb_rows(g));
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 // alloc each tab of tab_cell
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
g->tab_cell[i] = (square*)calloc(g->width, sizeof(square)); g->tab_cell[i] = (square*)calloc(game_nb_cols(g), sizeof(square));
if (g->tab_cell[i] == NULL) { if (g->tab_cell[i] == NULL) {
fprintf(stderr, "not enough memory\n"); fprintf(stderr, "not enough memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
+33 -30
View File
@@ -52,8 +52,8 @@ bool test_game_new_ext(void) {
printf("%d\n", i); printf("%d\n", i);
g = game_new_ext(h, w, tab, wrap); g = game_new_ext(h, w, tab, wrap);
assert(g); assert(g);
for (uint i2 = 0; i2 < g->height; i2++) { for (uint i2 = 0; i2 < game_nb_rows(g); i2++) {
for (uint j = 0; j < g->width; j++) { for (uint j = 0; j < game_nb_cols(g); j++) {
printf("list :%d, get square :%d\n", list[i], game_get_square(g, i2, j)); printf("list :%d, get square :%d\n", list[i], game_get_square(g, i2, j));
assert(list[i] == game_get_square(g, i2, j)); assert(list[i] == game_get_square(g, i2, j));
} }
@@ -168,8 +168,8 @@ bool test_game_get_square(void) {
for (uint j = 0; j < h * w; j++) { for (uint j = 0; j < h * w; j++) {
tab[j] = list[i]; tab[j] = list[i];
g = game_new_ext(h, w, tab, false); g = game_new_ext(h, w, tab, false);
printf("[%d, %d] %d %d\n", j / g->width, j % g->width, game_get_square(g, j / g->width, j % g->width), tab[j]); printf("[%d, %d] %d %d\n", j / game_nb_cols(g), j % game_nb_cols(g), game_get_square(g, j / game_nb_cols(g), j % game_nb_cols(g)), tab[j]);
assert(game_get_square(g, j / g->width, j % g->width) == tab[j]); assert(game_get_square(g, j / game_nb_cols(g), j % game_nb_cols(g)) == tab[j]);
game_delete(g); game_delete(g);
} }
} }
@@ -262,11 +262,13 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
if (/*test if the current case is lighted*/ flags == F_LIGHTED) { if (/*test if the current case is lighted*/ flags == F_LIGHTED) {
int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1}; int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
for (uint y = 0; y < 7 /*tab size*/; y = y + 2) { for (uint y = 0; y < 7 /*tab size*/; y = y + 2) {
for (int x = 1; x < max(g->width, g->height); x++) { for (int x = 1; x < max(game_nb_cols(g), game_nb_rows(g)); x++) {
if (/* test if both tab are not 0*/ tab[y] != tab[y + 1] && if (/* test if both tab are not 0*/ tab[y] != tab[y + 1] &&
(/* normal check */ (j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < g->width && i + x * tab[y] >= 0 && i + x * tab[y] < g->height) || (/* normal check */ (j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < game_nb_cols(g) && i + x * tab[y] >= 0 &&
/* wrapping check*/ g->wrapping)) { i + x * tab[y] < game_nb_rows(g)) ||
flags = game_get_square(g, ((i + g->height + x * tab[y]) % g->height), ((j + g->width + x * tab[y + 1]) % g->width)); /* wrapping check*/ game_is_wrapping(g))) {
flags = game_get_square(g, ((i + game_nb_rows(g) + x * tab[y]) % game_nb_rows(g)),
((j + game_nb_cols(g) + x * tab[y + 1]) % game_nb_cols(g)));
if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
return -1; return -1;
} }
@@ -290,8 +292,8 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
* @return false if one flags isn't correct * @return false if one flags isn't correct
*/ */
bool check_update(game g) { bool check_update(game g) {
for (uint i = 0; i < g->height; i++) { for (uint i = 0; i < game_nb_rows(g); i++) {
for (uint j = 0; j < g->width; j++) { for (uint j = 0; j < game_nb_cols(g); j++) {
if (game_is_lightbulb(g, i, j)) { if (game_is_lightbulb(g, i, j)) {
square flags = game_get_flags(g, i, j); square flags = game_get_flags(g, i, j);
if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR) { if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR) {
@@ -300,18 +302,18 @@ bool check_update(game g) {
int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1}; int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
for (uint tab_index = 0; tab_index < sizeof(tab) / sizeof(tab[0]); tab_index = tab_index + 2) { for (uint tab_index = 0; tab_index < sizeof(tab) / sizeof(tab[0]); tab_index = tab_index + 2) {
bool wall = false; bool wall = false;
for (int gap_distance = 1; gap_distance < max(g->width, g->height); gap_distance++) { for (int gap_distance = 1; gap_distance < max(game_nb_cols(g), game_nb_rows(g)); gap_distance++) {
if (/* test if both tab are not 0*/ tab[tab_index] != tab[tab_index + 1] && if (/* test if both tab are not 0*/ tab[tab_index] != tab[tab_index + 1] &&
(/* normal check */ (j + gap_distance * tab[tab_index + 1] >= 0 && j + gap_distance * tab[tab_index + 1] < g->width && (/* normal check */ (j + gap_distance * tab[tab_index + 1] >= 0 && j + gap_distance * tab[tab_index + 1] < game_nb_cols(g) &&
i + gap_distance * tab[tab_index] >= 0 && i + gap_distance * tab[tab_index] < g->height) || i + gap_distance * tab[tab_index] >= 0 && i + gap_distance * tab[tab_index] < game_nb_rows(g)) ||
/* wrapping check*/ g->wrapping)) { /* wrapping check*/ game_is_wrapping(g))) {
// test if current case is a wall // test if current case is a wall
if (game_is_black(g, ((i + g->height + gap_distance * tab[tab_index]) % g->height), if (game_is_black(g, ((i + game_nb_rows(g) + gap_distance * tab[tab_index]) % game_nb_rows(g)),
((j + g->width + gap_distance * tab[tab_index + 1]) % g->width))) { ((j + game_nb_cols(g) + gap_distance * tab[tab_index + 1]) % game_nb_cols(g)))) {
wall = true; wall = true;
} else { } else {
int tmp = checklightbulb(g, ((i + g->height + gap_distance * tab[tab_index]) % g->height), int tmp = checklightbulb(g, ((i + game_nb_rows(g) + gap_distance * tab[tab_index]) % game_nb_rows(g)),
((j + g->width + gap_distance * tab[tab_index + 1]) % g->width), wall); ((j + game_nb_cols(g) + gap_distance * tab[tab_index + 1]) % game_nb_cols(g)), wall);
if (tmp == -1) { if (tmp == -1) {
break; break;
} }
@@ -330,12 +332,13 @@ bool check_update(game g) {
int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1}; int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
bool res = true; bool res = true;
for (uint y = 0; y < 7; y = y + 2) { for (uint y = 0; y < 7; y = y + 2) {
if (/* normal check*/ (j + tab[y + 1] >= 0 && j + tab[y + 1] < g->width && i + tab[y] >= 0 && i + tab[y] < g->height) || if (/* normal check*/ (j + tab[y + 1] >= 0 && j + tab[y + 1] < game_nb_cols(g) && i + tab[y] >= 0 && i + tab[y] < game_nb_rows(g)) ||
/*wrapping check*/ g->wrapping) { /*wrapping check*/ game_is_wrapping(g)) {
if (game_is_lightbulb(g, (i + g->height + tab[y]) % g->height, (j + g->width + tab[y + 1]) % g->width)) if (game_is_lightbulb(g, (i + game_nb_rows(g) + tab[y]) % game_nb_rows(g), (j + game_nb_cols(g) + tab[y + 1]) % game_nb_cols(g)))
lb++; lb++;
else if (!game_is_blank(g, (i + g->height + tab[y]) % g->height, (j + g->width + tab[y + 1]) % g->width) || else if (!game_is_blank(g, (i + game_nb_rows(g) + tab[y]) % game_nb_rows(g),
game_is_lighted(g, (i + g->height + tab[y]) % g->height, (j + g->width + tab[y + 1]) % g->width)) (j + game_nb_cols(g) + tab[y + 1]) % game_nb_cols(g)) ||
game_is_lighted(g, (i + game_nb_rows(g) + tab[y]) % game_nb_rows(g), (j + game_nb_cols(g) + tab[y + 1]) % game_nb_cols(g)))
notempty++; notempty++;
} else { } else {
notempty++; notempty++;
@@ -371,18 +374,18 @@ bool check_update(game g) {
* @return false if there is a update_flags error. * @return false if there is a update_flags error.
*/ */
bool brutforce(game g, int a, bool* deldup) { bool brutforce(game g, int a, bool* deldup) {
bool* delDupNext = (bool*)calloc(g->height * g->width, sizeof(bool)); bool* delDupNext = (bool*)calloc(game_nb_rows(g) * game_nb_cols(g), sizeof(bool));
if (delDupNext == NULL) { if (delDupNext == NULL) {
fprintf(stderr, "Not enought memory\n"); fprintf(stderr, "Not enought memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK}; square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK};
for (uint i = 0; i < g->height; i++) { for (uint i = 0; i < game_nb_rows(g); i++) {
for (uint j = 0; j < g->width; j++) { for (uint j = 0; j < game_nb_cols(g); j++) {
if (deldup == NULL || !(deldup[i * g->width + j])) { if (deldup == NULL || !(deldup[i * game_nb_cols(g) + j])) {
if (game_get_state(g, i, j) == S_BLANK) { if (game_get_state(g, i, j) == S_BLANK) {
if (delDupNext != NULL && a > 0) if (delDupNext != NULL && a > 0)
delDupNext[i * g->width + j] = true; delDupNext[i * game_nb_cols(g) + j] = true;
for (uint k = 0; k < 9; k++) { for (uint k = 0; k < 9; k++) {
game_set_square(g, i, j, list[k]); game_set_square(g, i, j, list[k]);
if (a > 0) { if (a > 0) {
@@ -417,8 +420,8 @@ void randomise(game g) {
assert(g); assert(g);
square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU,
S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK}; S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK};
for (uint h = 0; h < g->height; h++) { for (uint h = 0; h < game_nb_rows(g); h++) {
for (uint w = 0; w < g->width; w++) { for (uint w = 0; w < game_nb_cols(g); w++) {
game_set_square(g, h, w, list[rand() % 16]); game_set_square(g, h, w, list[rand() % 16]);
} }
} }
+10 -10
View File
@@ -17,8 +17,8 @@ bool test_dummy() {
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}; square tab_square[] = {S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4};
game g = game_new_empty(); game g = game_new_empty();
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
// check for each case given in tab_square if the function return the expected result // 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++) { 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, i, j, tab_square[tab_index]);
@@ -37,8 +37,8 @@ bool test_game_get_black_number() {
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}; 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();
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
// check for each case given in tab_square if the function return the expected result // 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++) { 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, i, j, tab_square[tab_index]);
@@ -70,8 +70,8 @@ bool test_game_is_lightbulb() {
game_delete(g); game_delete(g);
return false; return false;
} }
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
// check for each case given in tab_square if the function return the expected result // 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++) { 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, i, j, tab_square[tab_index]);
@@ -90,8 +90,8 @@ bool test_game_is_lightbulb() {
bool test_game_is_blank() { bool test_game_is_blank() {
square tab_square[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU}; square tab_square[] = {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();
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
// check for each case given in tab_square if the function return the expected result // 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++) { 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, i, j, tab_square[tab_index]);
@@ -147,8 +147,8 @@ bool test_game_equal() {
/* ********** game_new_empty ********** */ /* ********** game_new_empty ********** */
bool test_game_new_empty() { bool test_game_new_empty() {
game g = game_new_empty(); game g = game_new_empty();
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
if (game_get_square(g, i, j) != S_BLANK) { if (game_get_square(g, i, j) != S_BLANK) {
game_delete(g); game_delete(g);
return false; return false;
+4 -4
View File
@@ -13,7 +13,7 @@
* @brief Edit the stringError with all errors of g * @brief Edit the stringError with all errors of g
* @param g the game to analyze * @param g the game to analyze
* @param stringError A pointer toward a string allocated before * @param stringError A pointer toward a string allocated before
* @pre @p stringError size must be at least g->height * g->width * NBCHARERR * @pre @p stringError size must be at least height * width * NBCHARERR
* @return true if there is an error on the game g and the stringError has been modified * @return true if there is an error on the game g and the stringError has been modified
*/ */
bool checkerrors(cgame g, char* stringError) { bool checkerrors(cgame g, char* stringError) {
@@ -23,8 +23,8 @@ bool checkerrors(cgame g, char* stringError) {
uint index_taberr = 0; uint index_taberr = 0;
// initialize stringError // initialize stringError
stringError[0] = '\0'; stringError[0] = '\0';
for (int i = 0; i < g->height; i++) { for (int i = 0; i < game_nb_rows(g); i++) {
for (int j = 0; j < g->width; j++) { for (int j = 0; j < game_nb_cols(g); j++) {
// check each case // check each case
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,
@@ -91,7 +91,7 @@ int main(int argc, char* argv[]) {
else else
usage(argc, argv); usage(argc, argv);
// init string for errors // init string for errors
char stringError[g->height * g->width * NBCHARERR]; char stringError[game_nb_rows(g) * game_nb_cols(g) * NBCHARERR];
char c; char c;
// print game // print game
game_print(g); game_print(g);