-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.c
293 lines (246 loc) · 7.6 KB
/
board.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "keys.h"
typedef int bool;
#define true 1
#define false 0
#define OUTLINE_TOP_LEFT 0
#define OUTLINE_HORIZONTAL 1
#define OUTLINE_TOP_RIGHT 2
#define OUTLINE_VERTICAL 3
#define OUTLINE_BOTTOM_LEFT 4
#define OUTLINE_BOTTOM_RIGHT 5
#define BOARD_WIDTH_DEFAULT 50;
#define BOARD_HEIGHT_DEFAULT 20;
#define MANUAL_DESCRIPTION "Usage: cSnake [options]\n\
\n\
Options:\n\
-h, --height <lines> The height of the game (bounding box)\n\
-w, --width <columns> The width of the game (bounding box)\n\
-a, --wrap-around Whether the snake can run through the walls\n\
-e, --help Displays this help\n"
struct Board {
bool wrapAround;
int width;
int innerWidth;
int height;
int innerHeight;
int cellsCount;
struct {
int top;
int bottom;
int left;
int right;
} padding;
};
void board_setup(struct Board *board, int width, int height, bool wrapAround)
{
board->width = width;
board->height = height;
board->wrapAround = wrapAround;
board->padding.top = 1;
board->padding.bottom = 1;
board->padding.left = 1;
board->padding.right = 1;
board->innerWidth = board->width - board->padding.left - board->padding.right;
board->innerHeight = board->height - board->padding.top - board->padding.bottom;
board->cellsCount = board->innerWidth * board->innerHeight;
}
//int is_snake_body(int row, int col, int colCount, int *snakePieces, int snakeLength)
int is_snake_body(int *snakePieces, int snakeLength, int cell)
{
for (int i = 0; i < snakeLength; i++) {
if (snakePieces[i] == cell) {
return true;
}
}
return false;
}
void destroy_board(int width, int height) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf(" ");
}
printf("\n");
}
}
key lastDirection;
bool snake_is_try_to_reverse(key direction)
{
if (lastDirection == KEY_UP && direction == KEY_DOWN) {
return true;
}
if (lastDirection == KEY_DOWN && direction == KEY_UP) {
return true;
}
if (lastDirection == KEY_LEFT && direction == KEY_RIGHT) {
return true;
}
if (lastDirection == KEY_RIGHT && direction == KEY_LEFT) {
return true;
}
return false;
}
bool snake_hit_wall(struct Board board, key direction, int snakeHead)
{
// direction == KEY_UP
if (snakeHead < 0) {
return true;
}
// KEY_DOWN
if (snakeHead > board.innerWidth * board.innerHeight) {
return true;
}
if (direction == KEY_RIGHT && snakeHead % board.innerWidth == 0) {
return true;
}
if (direction == KEY_LEFT && (snakeHead + 1) % board.innerWidth == 0) {
return true;
}
return false;
}
/*
* Returns true to indicate that the game is not over after this move
*/
bool snake_move(int *snakePieces, int *snakeLength, key direction, struct Board board, int foodPosition, bool *snakeGrew)
{
switch (direction) {
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:
case KEY_RIGHT:
if (!snake_is_try_to_reverse(direction)) {
lastDirection = direction;
break;
}
default: // not matched - or trying to reverse
direction = lastDirection;
}
int headMoveBy;
switch (direction) {
case KEY_UP:
headMoveBy = - board.innerWidth;
break;
case KEY_DOWN:
headMoveBy = board.innerWidth;
break;
case KEY_RIGHT:
headMoveBy = 1;
break;
case KEY_LEFT:
headMoveBy = - 1;
break;
}
int newHeadPosition = snakePieces[0] + headMoveBy;
if (foodPosition == newHeadPosition) {
*snakeLength = *snakeLength + 1;
*snakeGrew = true;
}
for (int i = *snakeLength - 1; i; i--) {
snakePieces[i] = snakePieces[i - 1];
}
if (foodPosition != newHeadPosition) {
bool hitTail = is_snake_body(
snakePieces,
*snakeLength,
newHeadPosition
);
if (hitTail) {
return false;
}
}
snakePieces[0] = newHeadPosition;
if (snake_hit_wall(board, direction, snakePieces[0])) {
// If we hit a wall and this is a wrap-around game
// then the game isn't over, the below code will figure out where to draw the head
if (!board.wrapAround) {
return false;
}
switch (direction) {
case KEY_UP:
snakePieces[0] = snakePieces[0] + (board.innerWidth * board.innerHeight);
break;
case KEY_DOWN:
snakePieces[0] = snakePieces[0] - (board.innerWidth * board.innerHeight);
break;
case KEY_RIGHT:
snakePieces[0] = snakePieces[0] - board.innerWidth;
break;
case KEY_LEFT:
snakePieces[0] = snakePieces[0] + board.innerWidth;
break;
}
}
return true;
}
int board_create_food_position(struct Board board, int *snakePieces, int snakeLength)
{
// Seems to get a nice random-ish seed, better than time
srand (clock());
for (int i = 0; i < 1000000; i++) {
int position = rand() % (board.cellsCount + 1);
bool isSnake = is_snake_body(
snakePieces,
snakeLength,
position
);
if (!isSnake) {
return position;
}
}
return 0;
}
/*
* Called for each tick of the main draw loop
* will redraw the board and will also put food and snake in
* Does not store a 1:1 view of the output, it just prints each char by
* calculating what each grid square should be
*/
void board_draw(struct Board board, int *snakePieces, int snakeLength, int foodPosition)
{
// Non wrap around
char outline[6][4] = {
"\u2554", // |`` // OUTLINE_TOP_LEFT
"\u2550", // -- // OUTLINE_HORIZONTAL
"\u2557", // ``| // OUTLINE_TOP_RIGHT
"\u2551", // | // OUTLINE_VERTICAL
"\u255A", // |_ // OUTLINE_BOTTOM_LEFT
"\u255D" // _| // OUTLINE_BOTTOM_RIGHT
};
if (board.wrapAround) {
strcpy(outline[0], "\u256D"); // |`` // OUTLINE_TOP_LEFT
strcpy(outline[1], "\u2500"); // -- // OUTLINE_HORIZONTAL
strcpy(outline[2], "\u256E"); // ``| // OUTLINE_TOP_RIGHT
strcpy(outline[3], "\u2502"); // | // OUTLINE_VERTICAL
strcpy(outline[4], "\u2570"); // |_ // OUTLINE_BOTTOM_LEFT
strcpy(outline[5], "\u256F"); // _| // OUTLINE_BOTTOM_RIGHT
}
char *body = "\u2588";
char *food = "\u25AA";
printf("%s", outline[OUTLINE_TOP_LEFT]);
for (int i = 0; i < board.innerWidth; i++) {
printf("%s", outline[OUTLINE_HORIZONTAL]);
}
printf("%s\n", outline[OUTLINE_TOP_RIGHT]);
for (int i = 0; i < board.innerHeight; i++) {
printf("%s", outline[OUTLINE_VERTICAL]);
for (int j = 0; j < board.innerWidth; j++) {
bool isSnake = is_snake_body(
snakePieces,
snakeLength,
((i * board.innerWidth) + j)
);
if (isSnake) {
printf("%s", body);
} else if ((i * board.innerWidth) + j == foodPosition) {
printf("%s", food);
} else {
printf(" ");
}
}
printf("%s\n", outline[OUTLINE_VERTICAL]);
}
printf("%s", outline[OUTLINE_BOTTOM_LEFT]);
for (int i = 0; i < board.innerWidth; i++) {
printf("%s", outline[OUTLINE_HORIZONTAL]);
}
printf("%s\n", outline[OUTLINE_BOTTOM_RIGHT]);
}