-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
361 lines (271 loc) · 7.32 KB
/
snake.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <curses.h>
#define GRID_WIDTH 50
#define GRID_HEIGHT 20
struct point {
int x;
int y;
};
enum direction { LEFT, RIGHT, UP, DOWN };
enum grid_val { EMPTY, SNAKE, APPLE, WALL };
// A grid represents the field (map of "used" cells)
struct grid {
int width;
int height;
int *g;
};
// A linked list of points making up the snake
struct snake_point {
struct point p;
struct snake_point *next;
};
// A snake is only updated in head and tail,
// and is present in a grid.
struct snake {
struct snake_point *head;
struct snake_point *tail;
struct grid *grid;
enum direction dir;
// Some stats
int length;
int apples_eaten;
};
void grid_set( struct grid *grid, int x, int y, int val )
{
*( grid->g + (y * grid->width + x) ) = val;
}
int grid_get( const struct grid *grid, int x, int y )
{
return *( grid->g + (y * grid->width + x) );
}
void grid_clear( struct grid *grid )
{
int x, y;
memset( grid->g, EMPTY, grid->width * grid->height );
// Add a wall around the entire border
for ( y = 0; y < grid->height; y++ ) {
for ( x = 0; x < grid->width; x++ ) {
if (
x == 0 || x == grid->width - 1 ||
y == 0 || y == grid->height - 1
) {
grid_set( grid, x, y, WALL );
}
}
}
// Add an interesting wall in the middle of the grid
for ( y = grid->height / 4; y < grid->height - grid->height / 4; y++ )
grid_set( grid, 24, y, WALL );
}
void grid_clear_cell( struct grid *grid, int x, int y )
{
grid_set( grid, x, y, EMPTY );
}
void init_grid( struct grid *grid, int width, int height )
{
grid->g = malloc( sizeof width * width * height );
assert(grid->g);
grid->width = width;
grid->height = height;
grid_clear( grid );
}
// Randomly places the apple in an empty cell in the grid
// and returns the coordinates
void place_apple( struct grid *grid )
{
int x, y;
do {
x = rand() % grid->width;
y = rand() % grid->height;
} while ( grid_get( grid, x, y ) != EMPTY );
grid_set( grid, x, y, APPLE );
}
// Adds (pushes) a point to the head of the snake
void push_point( struct snake *snake, struct point p )
{
struct snake_point *new;
new = (struct snake_point *) malloc( sizeof *new );
new->p = p;
new->next = NULL;
grid_set( snake->grid, p.x, p.y, SNAKE );
if ( snake->head != NULL )
snake->head->next = new;
if ( snake->tail == NULL )
snake->tail = new;
snake->head = new;
}
// Removes (shifts) off a point from the tail of the snake
// and places the value in @p (if defined). Returns non-zero
// if a point was actually removed.
int shift_point( struct snake *snake, struct point *p )
{
struct snake_point *next;
if ( snake->tail != NULL ) {
if ( p != NULL )
*p = snake->tail->p;
grid_clear_cell( snake->grid, snake->tail->p.x, snake->tail->p.y );
next = snake->tail->next;
free( snake->tail );
snake->tail = next;
return 1;
}
return 0;
}
void init_snake( struct snake *snake, struct point head, struct point tail, struct grid *grid )
{
int i;
snake->head = NULL;
snake->tail = NULL;
snake->grid = grid;
snake->dir = RIGHT;
snake->length = 4;
snake->apples_eaten = 0;
// Add the snake to the grid
for ( i = tail.x; i <= head.x; i++ ) {
push_point( snake, (struct point) { i, head.y } );
}
}
void move_snake( struct snake *snake, enum direction dir )
{
int got_apple = 0;
enum grid_val val;
struct point p;
p = snake->head->p;
// A change of motion can only be orthogonal to current
// direction of motion.
switch (dir) {
case LEFT:
if ( snake->dir == RIGHT ) return;
p.x -= 1;
break;
case RIGHT:
if ( snake->dir == LEFT ) return;
p.x += 1;
break;
case UP:
if ( snake->dir == DOWN ) return;
p.y -= 1;
break;
case DOWN:
if ( snake->dir == UP ) return;
p.y += 1;
break;
}
if (
p.x < 0 || p.x >= snake->grid->width ||
p.y < 0 || p.y >= snake->grid->height
) {
return;
}
snake->dir = dir;
switch ( grid_get( snake->grid, p.x, p.y ) ) {
case APPLE:
got_apple = 1;
break;
case EMPTY:
break;
// "Die" when hitting yourself or a wall
default:
return;
}
// Add new point at snake head
push_point( snake, p );
// Remove last point in the snake, unless we got the apple
// in which case we'd like to place a new one.
if ( !got_apple ) {
shift_point( snake, NULL );
} else {
place_apple( snake->grid );
snake->length++;
snake->apples_eaten++;
}
}
void dump_grid( const struct grid *grid )
{
int w = grid->width;
int h = grid->height;
int x, y;
for ( y = 0; y < h; y++ ) {
for ( x = 0; x < w; x++ ) {
char c;
int val = grid_get( grid, x, y );
switch ( grid_get( grid, x, y ) ) {
case EMPTY: c = ' '; break;
case SNAKE: c = 'o'; break;
case APPLE: c = '@'; break;
case WALL: c = '#'; break;
}
//printf( "%c", c );
addch( c );
}
//printf( "\n" );
addch( '\n' );
}
}
void show_stats( const struct snake *snake )
{
char str[100];
static int start = 0;
if ( !start ) start = time( NULL );
sprintf( str, "\nLength: %d\nApples eaten: %d\nTime: %d\n\n",
snake->length, snake->apples_eaten, (int) time( NULL ) - start );
addstr( str );
}
int main(void)
{
int done = 0;
int g_width = GRID_WIDTH;
int g_height = GRID_HEIGHT;
struct point head_start = { 6, 5 };
struct point tail_start = { 3, 5 };
struct snake snake, *sp = &snake;
struct grid grid, *gp = &grid;
srand( time( NULL ) );
// Initialize grid and snake, and place initial
// apple on the grid
init_grid( gp, g_width, g_height );
init_snake( sp, head_start, tail_start, gp );
place_apple( gp );
// Setup curses
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
// Mainloop
while ( !done ) {
int c;
// Tenths of a second
halfdelay( 2 );
if ( ( c = getch() ) != ERR ) {
switch ( c ) {
case 'q':
done = 1;
break;
case KEY_LEFT:
move_snake( sp, LEFT );
break;
case KEY_RIGHT:
move_snake( sp, RIGHT );
break;
case KEY_UP:
move_snake( sp, UP );
break;
case KEY_DOWN:
move_snake( sp, DOWN );
break;
}
} else {
move_snake( sp, sp->dir );
}
clear();
dump_grid( gp );
show_stats( sp );
}
endwin();
}