-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_text.c
145 lines (132 loc) · 3.84 KB
/
game_text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @file game_text.c
* @brief Game Interface in Text Mode.
* @copyright University of Bordeaux. All rights reserved, 2021.
**/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#include "game_aux.h"
#include "game_ext.h"
#include "game_tools.h"
/* ************************************************************************** */
static void game_print_errors(game g)
{
for (uint i = 0; i < game_nb_rows(g); i++) {
for (uint j = 0; j < game_nb_cols(g); j++) {
if (game_has_error(g, i, j)) {
if (game_is_lightbulb(g, i, j)) printf("Error at light bulb (%d,%d)\n", i, j);
if (game_is_black(g, i, j)) printf("Error at black wall (%d,%d)\n", i, j);
}
}
}
}
/* ************************************************************************** */
static bool game_step(game g)
{
printf("> ? [h for help]\n");
// <action> [<row> <col>]
char c = '?';
int r = scanf(" %c", &c); // don't forget the space ' ' before %c
if (r == EOF || r < 0) {
return false;
}
if (r != 1) {
printf("Error: invalid user input!\n");
return true; // but continue to play
}
if (c == 'h') { // help
printf("> action: help\n");
printf("- press 'l <i> <j>' to put a light bulb at square (i,j)\n");
printf("- press 'm <i> <j>' to put a mark at square (i,j)\n");
printf("- press 'b <i> <j>' to blank square (i,j)\n");
printf("- press 'r' to restart\n");
printf("- press 'z' to undo\n");
printf("- press 'y' to redo\n");
printf("- press 'q' to quit\n");
printf(
"- press 'w' to save the current state of the game (the file name should be defined by the user as a text "
"file)\n");
} else if (c == 's') {
game_solve(g);
return true;
} else if (c == 'n') {
printf("number of sol : %d\n", game_nb_solutions(g));
return true;
} else if (c == 'w') {
printf(">action: save game\n");
char filename[255] = "";
if (scanf("%s", filename) == 0) {
printf("empty string scanned\n");
}
game_save(g, filename);
} else if (c == 'z') { // undo
printf("> action: undo\n");
game_undo(g);
return true;
} else if (c == 'y') { // redo
printf("> action: redo\n");
game_redo(g);
return true;
} else if (c == 'r') { // restart
printf("> action: restart\n");
game_restart(g);
return true;
} else if (c == 'q') { // quit
printf("> action: quit\n");
return false; // exit
} else if (c == 'l' || c == 'm' || c == 'b') { // play move
uint i, j;
int ret = scanf(" %u %u", &i, &j);
if (ret != 2) {
printf("Error: invalid user input!\n");
return true;
}
printf("> action: play move '%c' into square (%d,%d)\n", c, i, j);
square s;
if (c == 'l') s = S_LIGHTBULB;
if (c == 'b') s = S_BLANK;
if (c == 'm') s = S_MARK;
bool check = game_check_move(g, i, j, s);
if (!check) {
printf("Error: illegal move on square (%d,%d)!\n", i, j);
return true;
}
game_play_move(g, i, j, s);
return true; // continue to play
} else {
printf("Error: invalid user input!\n");
return true;
}
return true; // continue...
}
/* ************************************************************************** */
int main(int argc, char* argv[])
{
game g = NULL;
if (argc == 2)
g = game_load(argv[1]);
else{
srand(time(NULL)); // initialize radom seed with current time
g = game_random(7, 7, false, 10, false);
}
assert(g);
game_print(g);
bool win = game_is_over(g);
bool cont = true;
while (!win && cont) {
cont = game_step(g);
win = game_is_over(g);
if (cont) game_print(g);
game_print_errors(g);
}
if (win)
printf("Congratulation, you win :-)\n");
else
printf("What a shame, you gave up :-(\n");
game_delete(g);
return EXIT_SUCCESS;
}