-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
194 lines (166 loc) · 3.19 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
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CH_BODY '*'
#define CH_HEAD '@'
#define CH_FOOD 'x'
#define MAX_LEN 1000
#define START_LEN 10 /* < MIN_COL */
#define MIN_COL 20
#define MIN_ROW 20
#define SLEEP_NS 5e7
#define IS_HEAD(_y,_x) ((_y) == S.y[S.ihead] && (_x) == S.x[S.ihead])
#define TAIL(r) ((MAX_LEN + S.ihead + -1 * (r)) % MAX_LEN)
#define IS_OPPOSITE(d1,d2) (-(d1)==(d2))
enum dir { LEFT = -2, DOWN = -1, UP = 1, RIGHT = 2 };
int row, col;
struct food { int y; int x; } F;
struct snake {
int len;
int ihead;
enum dir dir;
int x[MAX_LEN];
int y[MAX_LEN];
} S;
int is_inside_snake(int y, int x);
void init_snake();
void mv_snake(enum dir dir);
int check_crash();
void draw_snake();
void rand_food();
int check_food();
void draw_food();
int getch_last();
int
is_inside_snake(int y, int x)
{
/* or should we ask ncurses? */
for (int i = 1; i < S.len; i++)
if (x == S.x[TAIL(i)] && y == S.y[TAIL(i)])
return 1;
return 0;
}
void
init_snake()
{
int i;
int x = (col - START_LEN) / 2;
int y = row / 2;
for (i = 0; i < START_LEN; i++) {
S.x[i] = x + i;
S.y[i] = y;
}
S.ihead = i-1;
S.len = START_LEN;
S.dir = RIGHT;
}
void
mv_snake(enum dir dir)
{
int y = S.y[S.ihead];
int x = S.x[S.ihead];
if (!IS_OPPOSITE(dir,S.dir)) /* prevent movement inside oneself */
S.dir = dir;
switch(S.dir) {
case UP : y--; break;
case DOWN : y++; break;
case LEFT : x--; break;
case RIGHT: x++; break;
}
S.ihead++;
S.ihead = TAIL(0); /* prevent overflow */
S.y[S.ihead] = y;
S.x[S.ihead] = x;
}
int
check_crash()
{
if (S.x[S.ihead] < 0 || S.x[S.ihead] >= col ||
S.y[S.ihead] < 0 || S.y[S.ihead] >= row)
return 1;
return is_inside_snake(S.y[S.ihead], S.x[S.ihead]);
}
void
draw_snake()
{
for (int i = 0; i < S.len; i++)
mvaddch(S.y[TAIL(i)], S.x[TAIL(i)], (i==0)?CH_HEAD:CH_BODY);
}
void
rand_food()
{
F.x = rand() % col;
F.y = rand() % row;
do { /* infinite loop if snake fills the whole screen? */
F.x = ++F.x % col;
if (F.x == 0)
F.y = ++F.y % row;
} while (is_inside_snake(F.y,F.x) || IS_HEAD(F.y, F.x));
}
int
check_food()
{
return IS_HEAD(F.y,F.x);
}
void
draw_food()
{
mvaddch(F.y, F.x, CH_FOOD);
}
int
getch_last()
{
int b, c;
b = ERR;
while ((c = getch()) != ERR)
b = c;
return b;
}
int
main()
{
int ch;
struct timespec slp = { .tv_sec = 0, .tv_nsec = SLEEP_NS };
srand((unsigned) time(NULL));
initscr();
timeout(0);
noecho();
cbreak();
keypad(stdscr, TRUE);
curs_set(0);
getmaxyx(stdscr,row,col);
if (row < MIN_ROW || col < MIN_COL) {
endwin();
fprintf(stderr, "Screen should be at least %d x %d\n", MIN_COL, MIN_ROW);
exit(1);
}
init_snake();
rand_food();
while ((ch = getch_last()) != 'q') {
enum dir dir = S.dir;
switch(ch) {
case 'w': case KEY_UP : dir = UP ; break;
case 'a': case KEY_LEFT : dir = LEFT ; break;
case 's': case KEY_DOWN : dir = DOWN ; break;
case 'd': case KEY_RIGHT: dir = RIGHT; break;
}
mv_snake(dir);
if (check_crash())
break;
if (check_food()) {
if (++S.len == MAX_LEN)
break;
rand_food();
}
erase();
draw_snake();
draw_food();
refresh();
nanosleep(&slp, NULL);
}
endwin();
if (S.len == MAX_LEN)
printf("You won!\n");
return 0;
}