-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.c
224 lines (202 loc) · 5.57 KB
/
cli.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*--------------------------------------------------------------------*\
| This file is part of jmines
|
| <+Description+>
|
| Copyright 2012, Jeremy Brubaker <[email protected]>
|---------------------------------------------------------------------*\
| jmines is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| jmines is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with jmines. If not, see <http://www.gnu.org/licenses/>. |
\*--------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "game.h"
static void print_grid (game_data *);
static void print_menu ();
static int get_coordinates (int *x, int *y);
static char print_cell_contents (game_data *, int, int);
/*
* User input loop
*/
int
game_loop (game_data *data)
{
char input = '\0';
int x = 0;
int y = 0;
for (;;)
{
print_grid (data);
print_menu ();
/* TODO: better input validation to prevent crashes */
scanf (" %c", &input); /* get command */
switch (input)
{
case 'u':
if (get_coordinates (&x, &y) != 2) break;
if (uncover_cell (data, x, y) == LOSER)
return LOSER;
break;
case 'f':
if (get_coordinates (&x, &y) != 2) break;
if (flag_cell (data, x, y) == WINNER)
return WINNER;
break;
case 'g':
if (get_coordinates (&x, &y) != 2) break;
(void) guess_cell (data, x, y);
break;
case 'U':
if (get_coordinates (&x, &y) != 2) break;
(void) unmark_cell (data, x, y);
break;
case 'q':
return SUCCESS; /* Must be a cleaner way to quit */
break;
default:
printf ("%s\n", "Invalid choice!");
break;
}
while (getchar () != '\n') /* Discard rest of line */
;
}
}
/*
* Read and return x and y coordinates
*/
int
get_coordinates (int *x, int *y)
{
return scanf (" %d %d", x, y);
}
/*
* Print user choice menu
*/
void
print_menu ()
{
printf ("%s", "\n\
u*) Uncover cell\n\
f*) Flag cell as mine\n\
g*) Mark cell as guess\n\
U*) Unmark cell\n\
q ) Quit\n\
\n\
*: cmd x-coord y-coord\n\
\n\
Choice? ");
}
/*
* Print current state of grid
*/
void
print_grid (game_data *data)
{
int x = 0;
int y = 0;
int i = 0;
/*
* Print x-coordinate
*/
printf (" ");
for (i = 1; i <= data->grid.width; i++)
printf (" %3d ", i);
printf ("\n");
/*
* Print top of box
*/
printf (" +-");
for (i = 1; i <= data->grid.width; i++)
printf ("%s", "-----");
printf ("+\n");
/*
* Print each row
*/
for (y = 1; y < data->grid.height+1; y++)
{
printf ("%3d | ", y); /* y-coord and left of box */
/* Print row of cells */
for (x = 1; x < data->grid.width+1; x++)
printf (" %c ", print_cell_contents (data, x, y));
printf ("| %d\n", y); /* right of box and y-coord */
}
/*
* Print bottom of box
*/
printf (" +-");
for (i = 1; i <= data->grid.width; i++)
printf ("-----");
printf ("+\n");
/*
* Print x-coordinates
*/
printf (" ");
for (i = 1; i <= data->grid.width; i++)
printf (" %3d ", i);
printf ("\n");
}
/*
* Convert struct game_data cell contents to a printable character and return it
*/
char
print_cell_contents (game_data *data, int x, int y)
{
char printable = '\0';
/* First check for markers or if cell is covered */
if (data->grid.cell[x][y].marker == FLAG)
printable = 'F';
else if (data->grid.cell[x][y].marker == GUESS)
printable = 'G';
else if (data->grid.cell[x][y].is_covered == true)
printable = '.';
/* If cell is uncovered and unmarked, print its contents */
else
{
switch (data->grid.cell[x][y].contents)
{
case EMPTY:
printable = ' ';
break;
case ONE:
printable = '1';
break;
case TWO:
printable = '2';
break;
case THREE:
printable = '3';
break;
case FOUR:
printable = '4';
break;
case FIVE:
printable = '5';
break;
case SIX:
printable = '6';
break;
case SEVEN:
printable = '7';
break;
case EIGHT:
printable = '8';
break;
case MINE:
printable = 'X';
break;
}
}
return printable;
}