opti + memory leak fixed
This commit is contained in:
1 parent
0f88c8a773
commit
f61621a927
2 files changed
+22
-50
No files matched your search
+8
-35
@@ -2,39 +2,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
char charPrintCell(square sqState) {
|
char charPrintCell(cgame g, uint i, uint j) {
|
||||||
if (sqState == S_BLANK) {
|
square sqState = game_get_state(g, i, j);
|
||||||
return ' ';
|
square sqFlags = game_get_flags(g, i, j);
|
||||||
}
|
char tab[] = {' ', '*', '-', '?', '?', '?', '?', '?', '0', '1', '2', '3', '4', 'w', '?', '?', '.'};
|
||||||
if ((sqState == S_LIGHTBULB) ||
|
if (!sqState)
|
||||||
(sqState == (S_LIGHTBULB | F_ERROR)) | (sqState == (S_LIGHTBULB | F_LIGHTED)) | (sqState == (S_LIGHTBULB | F_ERROR | F_LIGHTED))) {
|
return tab[sqFlags];
|
||||||
return '*';
|
return tab[sqState];
|
||||||
}
|
|
||||||
if ((sqState == S_MARK) || (sqState == (S_MARK | F_ERROR)) | (sqState == (S_MARK | F_LIGHTED))) {
|
|
||||||
return '-';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACK0 || sqState == (S_BLACK0 | F_ERROR)) {
|
|
||||||
return '0';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACK1 || sqState == (S_BLACK1 | F_ERROR)) {
|
|
||||||
return '1';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACK2 || sqState == (S_BLACK2 | F_ERROR)) {
|
|
||||||
return '2';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACK3 || sqState == (S_BLACK3 | F_ERROR)) {
|
|
||||||
return '3';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACK4 || sqState == (S_BLACK4 | F_ERROR)) {
|
|
||||||
return '4';
|
|
||||||
}
|
|
||||||
if (sqState == S_BLACKU || sqState == (S_BLACKU | F_ERROR)) {
|
|
||||||
return 'w';
|
|
||||||
}
|
|
||||||
if (sqState == F_LIGHTED) {
|
|
||||||
return '.';
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_print(cgame g) {
|
void game_print(cgame g) {
|
||||||
@@ -52,7 +26,7 @@ void game_print(cgame g) {
|
|||||||
for (i = 0; i < DEFAULT_SIZE; i++) {
|
for (i = 0; i < DEFAULT_SIZE; i++) {
|
||||||
printf("%u |", i);
|
printf("%u |", i);
|
||||||
for (uint j = 0; j < DEFAULT_SIZE; j++) {
|
for (uint j = 0; j < DEFAULT_SIZE; j++) {
|
||||||
printf("%c", charPrintCell(game_get_square(g, i, j)));
|
printf("%c", charPrintCell(g, i, j));
|
||||||
}
|
}
|
||||||
printf("|\n");
|
printf("|\n");
|
||||||
}
|
}
|
||||||
@@ -88,6 +62,5 @@ game game_default_solution(void) {
|
|||||||
game_set_square(g, 5, 0, S_LIGHTBULB);
|
game_set_square(g, 5, 0, S_LIGHTBULB);
|
||||||
game_set_square(g, 5, 5, S_LIGHTBULB);
|
game_set_square(g, 5, 5, S_LIGHTBULB);
|
||||||
game_set_square(g, 6, 1, S_LIGHTBULB);
|
game_set_square(g, 6, 1, S_LIGHTBULB);
|
||||||
game_update_flags(g);
|
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
+14
-15
@@ -4,7 +4,7 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "game_aux.h"
|
#include "game_aux.h"
|
||||||
|
|
||||||
bool checkerr(game g, char taberr[]) {
|
bool checkerr(cgame g, char taberr[]) {
|
||||||
if (taberr == NULL || g == NULL) {
|
if (taberr == NULL || g == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -74,26 +74,25 @@ int main(void) {
|
|||||||
game_print(g);
|
game_print(g);
|
||||||
} else if (c == 'q') {
|
} else if (c == 'q') {
|
||||||
printf("Solution: \n");
|
printf("Solution: \n");
|
||||||
game_print(game_default_solution());
|
game sol = game_default_solution();
|
||||||
|
game_update_flags(sol);
|
||||||
|
game_print(sol);
|
||||||
|
game_delete(sol);
|
||||||
|
game_delete(g);
|
||||||
printf("shame\n");
|
printf("shame\n");
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
} else if (c == 'l' || c == 'm' || c == 'b') {
|
} else if (c == 'l' || c == 'm' || c == 'b') {
|
||||||
if (scanf(" %u %u", &i, &j) == 2) {
|
if (scanf(" %u %u", &i, &j) == 2) {
|
||||||
if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && !game_is_black(g, i, j)) {
|
if (c == 'l' && game_check_move(g, i, j, S_LIGHTBULB)) {
|
||||||
if (c == 'l') {
|
game_play_move(g, i, j, S_LIGHTBULB);
|
||||||
game_set_square(g, i, j, S_LIGHTBULB);
|
|
||||||
game_update_flags(g);
|
|
||||||
game_print(g);
|
game_print(g);
|
||||||
} else if (c == 'm') {
|
} else if (c == 'm' && game_check_move(g, i, j, S_MARK)) {
|
||||||
game_set_square(g, i, j, S_MARK);
|
game_play_move(g, i, j, S_MARK);
|
||||||
game_update_flags(g);
|
|
||||||
game_print(g);
|
game_print(g);
|
||||||
} else if (c == 'b') {
|
} else if (c == 'b' && game_check_move(g, i, j, S_BLANK)) {
|
||||||
game_set_square(g, i, j, S_BLANK);
|
game_play_move(g, i, j, S_BLANK);
|
||||||
game_update_flags(g);
|
|
||||||
game_print(g);
|
game_print(g);
|
||||||
}
|
} else
|
||||||
} else {
|
|
||||||
printf("Erreur: (<i>,<j>) invalide.\n");
|
printf("Erreur: (<i>,<j>) invalide.\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -103,7 +102,7 @@ int main(void) {
|
|||||||
printf("Commande inconnue.\n");
|
printf("Commande inconnue.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
printf("congratulation\n");
|
printf("congratulation\n");
|
||||||
|
game_delete(g);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user