replaced g->height, g->width and g->wrapping
This commit is contained in:
1 parent
0403d6e9bb
commit
75db9fa6fb
5 files changed
+59
-55
No files matched your search
+6
-5
@@ -1,6 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "game.h"
|
||||
#include "game_ext.h"
|
||||
#include "game_private.h"
|
||||
|
||||
char charPrintCell(cgame g, uint i, uint j) {
|
||||
@@ -16,23 +17,23 @@ void game_print(cgame g) {
|
||||
if (g != NULL) {
|
||||
uint i;
|
||||
printf(" 0");
|
||||
for (i = 1; i < g->width; i++) {
|
||||
for (i = 1; i < game_nb_cols(g); i++) {
|
||||
printf("%u", i);
|
||||
}
|
||||
printf("\n -");
|
||||
for (i = 1; i < g->width; i++) {
|
||||
for (i = 1; i < game_nb_cols(g); i++) {
|
||||
printf("-");
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < g->height; i++) {
|
||||
for (i = 0; i < game_nb_rows(g); 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("|\n");
|
||||
}
|
||||
printf(" -");
|
||||
for (i = 1; i < g->width; i++) {
|
||||
for (i = 1; i < game_nb_cols(g); i++) {
|
||||
printf("-");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
+6
-6
@@ -5,9 +5,9 @@
|
||||
|
||||
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);
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
g->tab_cell[i][j] = squares[g->width * i + j];
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
for (int j = 0; j < game_nb_cols(g); j++) {
|
||||
g->tab_cell[i][j] = squares[game_nb_cols(g) * i + j];
|
||||
}
|
||||
}
|
||||
return g;
|
||||
@@ -27,14 +27,14 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) {
|
||||
g->hist = history_create_empty();
|
||||
// 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*) * game_nb_rows(g));
|
||||
if (g->tab_cell == NULL) {
|
||||
fprintf(stderr, "not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// alloc each tab of tab_cell
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
g->tab_cell[i] = (square*)calloc(g->width, sizeof(square));
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
g->tab_cell[i] = (square*)calloc(game_nb_cols(g), sizeof(square));
|
||||
if (g->tab_cell[i] == NULL) {
|
||||
fprintf(stderr, "not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
+33
-30
@@ -52,8 +52,8 @@ bool test_game_new_ext(void) {
|
||||
printf("%d\n", i);
|
||||
g = game_new_ext(h, w, tab, wrap);
|
||||
assert(g);
|
||||
for (uint i2 = 0; i2 < g->height; i2++) {
|
||||
for (uint j = 0; j < g->width; j++) {
|
||||
for (uint i2 = 0; i2 < game_nb_rows(g); i2++) {
|
||||
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));
|
||||
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++) {
|
||||
tab[j] = list[i];
|
||||
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]);
|
||||
assert(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 / game_nb_cols(g), j % game_nb_cols(g)) == tab[j]);
|
||||
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) {
|
||||
int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
|
||||
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] &&
|
||||
(/* 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) ||
|
||||
/* wrapping check*/ g->wrapping)) {
|
||||
flags = game_get_square(g, ((i + g->height + x * tab[y]) % g->height), ((j + g->width + x * tab[y + 1]) % g->width));
|
||||
(/* normal check */ (j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < game_nb_cols(g) && i + x * tab[y] >= 0 &&
|
||||
i + x * tab[y] < game_nb_rows(g)) ||
|
||||
/* 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)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -290,8 +292,8 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
|
||||
* @return false if one flags isn't correct
|
||||
*/
|
||||
bool check_update(game g) {
|
||||
for (uint i = 0; i < g->height; i++) {
|
||||
for (uint j = 0; j < g->width; j++) {
|
||||
for (uint i = 0; i < game_nb_rows(g); i++) {
|
||||
for (uint j = 0; j < game_nb_cols(g); j++) {
|
||||
if (game_is_lightbulb(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) {
|
||||
@@ -300,18 +302,18 @@ bool check_update(game g) {
|
||||
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) {
|
||||
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] &&
|
||||
(/* normal check */ (j + gap_distance * tab[tab_index + 1] >= 0 && j + gap_distance * tab[tab_index + 1] < g->width &&
|
||||
i + gap_distance * tab[tab_index] >= 0 && i + gap_distance * tab[tab_index] < g->height) ||
|
||||
/* wrapping check*/ g->wrapping)) {
|
||||
(/* 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] < game_nb_rows(g)) ||
|
||||
/* wrapping check*/ game_is_wrapping(g))) {
|
||||
// test if current case is a wall
|
||||
if (game_is_black(g, ((i + g->height + gap_distance * tab[tab_index]) % g->height),
|
||||
((j + g->width + gap_distance * tab[tab_index + 1]) % g->width))) {
|
||||
if (game_is_black(g, ((i + game_nb_rows(g) + gap_distance * tab[tab_index]) % game_nb_rows(g)),
|
||||
((j + game_nb_cols(g) + gap_distance * tab[tab_index + 1]) % game_nb_cols(g)))) {
|
||||
wall = true;
|
||||
} else {
|
||||
int tmp = checklightbulb(g, ((i + g->height + gap_distance * tab[tab_index]) % g->height),
|
||||
((j + g->width + gap_distance * tab[tab_index + 1]) % g->width), wall);
|
||||
int tmp = checklightbulb(g, ((i + game_nb_rows(g) + gap_distance * tab[tab_index]) % game_nb_rows(g)),
|
||||
((j + game_nb_cols(g) + gap_distance * tab[tab_index + 1]) % game_nb_cols(g)), wall);
|
||||
if (tmp == -1) {
|
||||
break;
|
||||
}
|
||||
@@ -330,12 +332,13 @@ bool check_update(game g) {
|
||||
int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
|
||||
bool res = true;
|
||||
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) ||
|
||||
/*wrapping check*/ g->wrapping) {
|
||||
if (game_is_lightbulb(g, (i + g->height + tab[y]) % g->height, (j + g->width + tab[y + 1]) % g->width))
|
||||
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*/ game_is_wrapping(g)) {
|
||||
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++;
|
||||
else if (!game_is_blank(g, (i + g->height + tab[y]) % g->height, (j + g->width + tab[y + 1]) % g->width) ||
|
||||
game_is_lighted(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),
|
||||
(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++;
|
||||
} else {
|
||||
notempty++;
|
||||
@@ -371,18 +374,18 @@ bool check_update(game g) {
|
||||
* @return false if there is a update_flags error.
|
||||
*/
|
||||
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) {
|
||||
fprintf(stderr, "Not enought memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
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 j = 0; j < g->width; j++) {
|
||||
if (deldup == NULL || !(deldup[i * g->width + j])) {
|
||||
for (uint i = 0; i < game_nb_rows(g); i++) {
|
||||
for (uint j = 0; j < game_nb_cols(g); j++) {
|
||||
if (deldup == NULL || !(deldup[i * game_nb_cols(g) + j])) {
|
||||
if (game_get_state(g, i, j) == S_BLANK) {
|
||||
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++) {
|
||||
game_set_square(g, i, j, list[k]);
|
||||
if (a > 0) {
|
||||
@@ -417,8 +420,8 @@ void randomise(game g) {
|
||||
assert(g);
|
||||
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};
|
||||
for (uint h = 0; h < g->height; h++) {
|
||||
for (uint w = 0; w < g->width; w++) {
|
||||
for (uint h = 0; h < game_nb_rows(g); h++) {
|
||||
for (uint w = 0; w < game_nb_cols(g); w++) {
|
||||
game_set_square(g, h, w, list[rand() % 16]);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-10
@@ -17,8 +17,8 @@ bool test_dummy() {
|
||||
bool test_game_get_black_number() {
|
||||
square tab_square[] = {S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4};
|
||||
game g = game_new_empty();
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
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
|
||||
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]);
|
||||
@@ -37,8 +37,8 @@ bool test_game_get_black_number() {
|
||||
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();
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
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
|
||||
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]);
|
||||
@@ -70,8 +70,8 @@ bool test_game_is_lightbulb() {
|
||||
game_delete(g);
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
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
|
||||
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]);
|
||||
@@ -90,8 +90,8 @@ bool test_game_is_lightbulb() {
|
||||
bool test_game_is_blank() {
|
||||
square tab_square[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU};
|
||||
game g = game_new_empty();
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
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
|
||||
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]);
|
||||
@@ -147,8 +147,8 @@ bool test_game_equal() {
|
||||
/* ********** game_new_empty ********** */
|
||||
bool test_game_new_empty() {
|
||||
game g = game_new_empty();
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
for (int j = 0; j < game_nb_cols(g); j++) {
|
||||
if (game_get_square(g, i, j) != S_BLANK) {
|
||||
game_delete(g);
|
||||
return false;
|
||||
|
||||
+4
-4
@@ -13,7 +13,7 @@
|
||||
* @brief Edit the stringError with all errors of g
|
||||
* @param g the game to analyze
|
||||
* @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
|
||||
*/
|
||||
bool checkerrors(cgame g, char* stringError) {
|
||||
@@ -23,8 +23,8 @@ bool checkerrors(cgame g, char* stringError) {
|
||||
uint index_taberr = 0;
|
||||
// initialize stringError
|
||||
stringError[0] = '\0';
|
||||
for (int i = 0; i < g->height; i++) {
|
||||
for (int j = 0; j < g->width; j++) {
|
||||
for (int i = 0; i < game_nb_rows(g); i++) {
|
||||
for (int j = 0; j < game_nb_cols(g); j++) {
|
||||
// check each case
|
||||
if (game_has_error(g, i, j)) {
|
||||
// if error append stringError with coordinate of the error,
|
||||
@@ -91,7 +91,7 @@ int main(int argc, char* argv[]) {
|
||||
else
|
||||
usage(argc, argv);
|
||||
// init string for errors
|
||||
char stringError[g->height * g->width * NBCHARERR];
|
||||
char stringError[game_nb_rows(g) * game_nb_cols(g) * NBCHARERR];
|
||||
char c;
|
||||
// print game
|
||||
game_print(g);
|
||||
|
||||
Reference in new issue
Block a user