-
Notifications
You must be signed in to change notification settings - Fork 46
/
snake.c
411 lines (356 loc) · 9.54 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <signal.h>
#include <sys/times.h>
#include "../../include_winix/winix/list.h"
#define NUM_COLS (50)
#define NUM_ROWS (15)
#define INITIAL_SNAKE_LEN (3)
#define INITAL_FOOD_NUM (10)
#define SNAKE_CHAR ('x')
#define FOOD_CHAR ('o')
#define GET_SNAKE_HEAD(board) (list_first_entry(&board->snake, struct point, list))
#define GET_SNAKE_TAIL(board) (list_last_entry(&board->snake, struct point, list))
#define draw_point(p, c) draw_coordinate(p->x, p->y, c)
#define clear_point(p) draw_point(p, ' ')
#define SUCCESS ( 0)
#define FAIL (-1)
#define FAIL_LEFT (-2)
#define FAIL_RIGHT (-3)
#define FAIL_DOWN (-4)
#define FAIL_UP (-5)
#define FAIL_EAT_SELF (-6)
#define FAIL_QUIT (-7)
void quit();
struct termios prev_termios;
bool tty_inited = false;
struct point{
int x;
int y;
struct list_head list;
};
enum direction {left, up, right, down};
struct board_struct{
struct list_head snake;
struct list_head foods;
enum direction dir;
};
struct point* new_point(int x, int y){
struct point* po = (struct point*)malloc(sizeof(struct point));
po->x = x;
po->y = y;
return po;
}
struct point* new_food(){
int x = (rand() % (NUM_COLS -2 )) + 1;
int y = (rand() % (NUM_ROWS -1 )) + 1;
return new_point(x, y);
}
// byte for \e[?25l
static char cursor_str[] = {0x1b, 0x5b ,0x3f ,0x32 ,0x35 ,0x6c, 0};
void disable_cursor(){
printf("%s", cursor_str);
fflush(stdout);
}
void enable_cursor(){
cursor_str[5] = 'h';
printf("%s", cursor_str);
fflush(stdout);
}
void int2str(int value, int i, char* output){
int j;
while(i){
j = (value / i) % 10;
*output++ = '0' + j;
i /= 10;
}
}
// byte stream for \e[000;000H
static char draw_pos[] = {0x1b, 0x5b, 0x30, 0x30, 0x30, 0x3b, 0x30, 0x30, 0x30, 0x48, 0};
static char coordinate_format[] = "%s ";
void draw_coordinate(int x, int y, char character){
char *draw_format = draw_pos;
int2str(y, 100, draw_format + 2);
int2str(x, 100, draw_format + 6);
coordinate_format[2] = character;
printf(coordinate_format, draw_pos);
fflush(stdout);
// fprintf(stderr, "Draw %c at x %d y %d | ", character, pos->x, pos->y);
}
void clear_screen(){
// byte stream for \e[0;0H\e[2J
static char cls[] = {0x1b, 0x5b, 0x30, 0x3b, 0x30, 0x48, 0x1b, 0x5b, 0x32, 0x4a, 0};
printf("%s", cls);
fflush(stdout);
}
void print_to_central_screen(int y, char* str){
int len = strlen(str);
int x = (NUM_COLS / 2) - (len / 2) - 1;
draw_coordinate(x, y, ' ');
printf("%s", str);
fflush(stdout);
}
void print_instruction(){
int y = NUM_ROWS / 2;
print_to_central_screen(y++, "Use WASD to move");
print_to_central_screen(y++, "Press SPACE to start");
print_to_central_screen(y, "Press q to exit");
}
void print_border(struct board_struct* board){
char *buffer = malloc(NUM_COLS);
char *buf = buffer;
int i, end = NUM_COLS - 2;
for(i = 0; i < end; i++){
*buf++ = '-';
}
*buf = '\0';
draw_coordinate(0, 0, '-');
printf("%s", buffer);
draw_coordinate(0, NUM_ROWS, '-');
printf("%s", buffer);
fflush(stdout);
for (i = 1; i < NUM_ROWS; i++){
draw_coordinate(0, i, '|');
}
for (i = 1; i < NUM_ROWS; i++){
draw_coordinate(NUM_COLS - 1, i, '|');
}
free(buffer);
}
void init_tty(){
struct termios termios;
tcgetattr(STDIN_FILENO, &prev_termios);
termios = prev_termios;
termios.c_lflag &= (~ICANON & ~ECHO);
termios.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &termios);
tty_inited = true;
}
void restore_tty(int signum){
if (tty_inited){
tcsetattr(STDIN_FILENO, TCSANOW, &prev_termios);
enable_cursor();
}
}
void board_init(struct board_struct *board){
int fd, i;
clock_t clo = times(NULL);
srand(clo);
INIT_LIST_HEAD(&board->foods);
INIT_LIST_HEAD(&board->snake);
fd = open("/var/log/snake.log", O_RDWR | O_CREAT | O_TRUNC, 0644);
dup2(fd, STDERR_FILENO);
close(fd);
init_tty();
disable_cursor();
for(i = 0; i < _NSIG; i++){
signal(i, restore_tty);
}
}
void init_snake_food(struct board_struct *board){
int i;
struct point* food_pos, *snake_pos;
for(i = 0; i < INITAL_FOOD_NUM; i++){
food_pos = new_food();
list_add(&food_pos->list, &board->foods);
draw_point(food_pos, FOOD_CHAR);
}
for(i = 1; i <= INITIAL_SNAKE_LEN; i++){
snake_pos = new_point(i, 1);
list_add(&snake_pos->list, &board->snake);
draw_point(snake_pos, SNAKE_CHAR);
// fprintf(stderr, "add x %d y %d\n", i, 1);
}
}
void debug_board(struct board_struct* board){
struct point* p;
fprintf(stderr, "points: ");
list_for_each_entry(struct point, p, &board->snake, list){
fprintf(stderr, "x %d y %d; ", p->x, p->y);
}
fprintf(stderr, "\n");
}
#define INPUT_SIZ (12)
enum direction get_direction(struct board_struct* board, char c){
enum direction dir = board->dir;
//non blocking
switch (c)
{
case 'w':
if(dir == down)
return down;
return up;
case 'a':
if(dir == right)
return right;
return left;
case 's':
if(dir == up)
return up;
return down;
case 'd':
if(dir == left)
return left;
return right;
case 'q':
quit();
default:
break;
}
return dir;
}
struct point* get_point_at_coordinate(struct list_head* head, int x, int y){
struct point* p1;
list_for_each_entry(struct point, p1, head, list){
if(p1->x == x && p1->y == y){
return p1;
}
}
return NULL;
}
int _refresh(struct board_struct* board){
enum direction dir = board->dir;
struct point* head = GET_SNAKE_HEAD(board), *newpos, *pos;
int x = head->x, y = head->y;
// fprintf(stderr, "dir %d | ", dir);
switch (dir)
{
case left:
if(x == 1)
return FAIL_LEFT;
x--;
break;
case up:
if(y == 1)
return FAIL_UP;
y--;
break;
case right:
if(x >= NUM_COLS - 2)
return FAIL_RIGHT;
x++;
break;
case down:
if(y >= NUM_ROWS - 1)
return FAIL_DOWN;
y++;
break;
default:
return FAIL;
}
pos = get_point_at_coordinate(&board->foods, x, y);
if(!pos){
pos = get_point_at_coordinate(&board->snake, x, y);
if(pos){
// fprintf(stderr, "eat self x %d y %d\n");
return FAIL_EAT_SELF;
}
pos = GET_SNAKE_TAIL(board);
clear_point(pos);
}else{
// if snake has eaten food, get a new random food
newpos = new_food();
list_add(&newpos->list, &board->foods);
draw_point(newpos, FOOD_CHAR);
}
list_del(&pos->list);
pos->x = x;
pos->y = y;
list_add(&pos->list, &board->snake);
draw_point(pos, SNAKE_CHAR);
return SUCCESS;
}
int refresh(struct board_struct* board){
int ret = _refresh(board);
if(ret){
print_to_central_screen(NUM_ROWS / 2 - 1, "You lost :(");
// printf("%d", ret);
}
return ret;
}
void draw(struct board_struct *board){
struct point* p;
list_for_each_entry(struct point, p, &board->snake, list){
draw_point(p, SNAKE_CHAR);
}
list_for_each_entry(struct point, p, &board->foods, list){
draw_point(p, FOOD_CHAR);
}
}
char get_chr(){
char c;
int ret = read(STDIN_FILENO, &c, 1 * sizeof(char));
if(ret > 0)
return c;
return 0;
}
void reset_board(struct board_struct* board){
INIT_LIST_HEAD(&board->snake);
INIT_LIST_HEAD(&board->foods);
board->dir = right;
}
void quit(){
restore_tty(0);
clear_screen();
exit(0);
}
int main(int argc, char** argv){
static char input[INPUT_SIZ];
static struct board_struct board;
int ret, i;
struct board_struct* bp = &board;
struct timespec ts;
bool is_snake_alive = true, is_game_running = true, waiting_for_command = true;
memset(&ts, 0, sizeof(struct timespec));
board_init(bp);
clear_screen();
print_border(bp);
ts.tv_nsec = 125000000;
while(is_game_running){
print_instruction();
while(waiting_for_command){
char c = get_chr();
if(c == 32)
break;
if(c == 'q'){
quit();
}
}
reset_board(bp);
clear_screen();
print_border(bp);
init_snake_food(bp);
is_snake_alive = true;
while(is_snake_alive){
char prev = '\0';
nanosleep(&ts, NULL);
ret = read(STDIN_FILENO, input, INPUT_SIZ * sizeof(char));
if (ret == 0){
if (refresh(bp)){
is_snake_alive = false;
break;
}
}
for(i = 0; i < ret; i++){
if (i && input[i] == prev)
continue;
prev = input[i];
bp->dir = get_direction(bp, input[i]);
ret = refresh(bp);
if(ret){
is_snake_alive = false;
break;
}
}
}
}
quit();
return 0;
}