-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses_interface.c
207 lines (186 loc) · 5.32 KB
/
curses_interface.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
/*--------------------------------------------------------------------*\
| 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 <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include "curses_interface.h"
#include "game.h"
static void print_grid (game_data *);
static void print_menu ();
static void 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 ();
input = (char) getc (stdin); /* Only need the first character */
while (getc (stdin) != '\n') /* Discard rest of line */
;
switch (input)
{
case 'u':
get_coordinates (&x, &y);
if (uncover_cell (data, x, y) == LOSER)
return LOSER;
break;
case 'f':
get_coordinates (&x, &y);
if (flag_cell (data, x, y) == WINNER)
return WINNER;
break;
case 'g':
get_coordinates (&x, &y);
(void) guess_cell (data, x, y);
break;
case 'U':
get_coordinates (&x, &y);
(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;
}
}
}
/*
* Read and return x and y coordinates
*/
void
get_coordinates (int *x, int *y)
{
char input[80] = "";
printf ("%s", "Coordinates (x, y)? ");
/* TODO: Make this safer!! */
(void) fgets (input, 80, stdin);
*x = atoi (strtok (input, ", "));
*y = atoi (strtok (NULL, " \n"));
}
/*
* 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\
Choice? ");
}
/*
* Print current state of grid
*/
void
print_grid (game_data *data)
{
int x = 0;
int y = 0;
int i = 0;
printf ("%s", " ");
/* Print x-coordinate row */
for (i = 1; i <= data->grid.width; i++)
{
printf ("%c", ' ');
if (i < 100) printf ("%c", ' ');
if (i < 10) printf ("%c", ' ');
printf ("%d ", i);
}
printf ("%s", "\n");
for (y = 1; y < data->grid.height+1; y++)
{
/* Print row's y-coordinate */
if (y < 100) printf ("%c", ' ');
if (y < 10) printf ("%c", ' ');
printf ("%d", y);
/* Print row of cells */
for (x = 1; x < data->grid.width+1; x++)
{
printf (" %c ", print_cell_contents (data, x, y));
}
printf ("%s", "\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;
}