-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
286 lines (224 loc) · 5.24 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
/******************************************************************************
Snake Game (w/User-level threads)
Copyright (c) 2014, Kendall Stewart
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*******************************************************************************/
#include <stdio.h> /* printf */
#include <string.h> /* memset */
#include <unistd.h> /* close, usleep */
#include <stdlib.h> /* rand */
#include <sys/ioctl.h> /* ioctl, TIOCGWINSZ */
#include <fcntl.h> /* open */
#include <termios.h> /* tcgetattr, tcsetattr */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <aio.h>
// uthread
#include "uthread.h"
#include "uthread_aio.h"
/* Terminal State
* see termios(3) for info
*/
struct termios old, new;
void set_term_state()
{
tcgetattr(0, &old);
new = old;
new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &new);
}
void restore_term_state()
{
tcsetattr(0, TCSANOW, &old);
}
/* Get screen height
* see ioctl(2) for info
* also: http://rosettacode.org/wiki/Terminal_control/Dimensions#C
*/
int screen_height;
void set_screensize()
{
struct winsize ws;
int term = open("/dev/tty", O_RDWR);
ioctl(term, TIOCGWINSZ, &ws);
close(term);
screen_height = ws.ws_row;
}
/* Game State */
#define WIDTH 50
#define HEIGHT 25
char board[HEIGHT][WIDTH];
typedef struct {
int x;
int y;
} point;
#define MAX_SNAKE_LENGTH 1250
point snake[MAX_SNAKE_LENGTH];
int snake_head = 0;
int snake_tail = 0;
void set_point(point p)
{
board[p.y][p.x] = '@';
}
void unset_point(point p)
{
board[p.y][p.x] = ' ';
}
int game_over = 0;
void initialize_board()
{
memset(board, ' ', HEIGHT * WIDTH);
memset(snake, 0, MAX_SNAKE_LENGTH * sizeof(point));
point initial_point = { WIDTH / 2, HEIGHT / 2 };
snake[0] = initial_point;
set_point(snake[0]);
}
void print_board(void)
{
int i, j;
for (i = 0; i < screen_height - (HEIGHT + 2); ++i) {
printf("\n");
}
for (i = 0; i < WIDTH + 2; ++i) {
printf("*");
}
printf("\n");
for (i = 0; i < HEIGHT; ++i) {
printf("*");
for (j = 0; j < WIDTH; ++j) {
printf("%c", board[i][j]);
}
printf("*\n");
}
for (i = 0; i < WIDTH + 2; ++i) {
printf("*");
}
printf("\n");
}
typedef enum {
LEFT,
UP,
DOWN,
RIGHT
} direction;
direction current_direction = UP;
/* Manipulate Game State */
void move_snake()
{
point head = snake[snake_head];
int r;
switch (current_direction) {
case UP:
head.y = (r = head.y - 1) < 0 ? HEIGHT - 1 : r;
break;
case DOWN:
head.y = (head.y + 1) % HEIGHT;
break;
case LEFT:
head.x = (r = head.x - 1) < 0 ? WIDTH - 1 : r;
break;
case RIGHT:
head.x = (head.x + 1) % WIDTH;
break;
}
snake_head = (snake_head + 1) % MAX_SNAKE_LENGTH;
snake[snake_head] = head;
if (board[head.y][head.x] != '#') {
unset_point(snake[snake_tail]);
snake_tail = (snake_tail + 1) % MAX_SNAKE_LENGTH;
}
if (board[head.y][head.x] == '@') {
board[head.y][head.x] = 'x';
game_over = 1;
}
else {
set_point(snake[snake_head]);
}
}
/* Keypress Listener */
int waiting_for_key;
int listen_for_keypress(void)
{
waiting_for_key = 1;
char buf[3];
async_read(0, buf, 3);
current_direction = buf[0] & buf[1] & buf[2];
waiting_for_key = 0;
return 0;
}
/* Generate random #s (snake food) */
void generate_bites(void)
{
unsigned int biteX = rand() % WIDTH;
unsigned int biteY = rand() % HEIGHT;
board[biteY][biteX] = '#';
/* generate a bite every 50 game cycles */
int i;
for (i = 0; i < 50; ++i) {
uthread_yield();
}
}
/* Slows game rate down, makes it playable */
void delay()
{
usleep(100000);
}
/* continuously perform a game function
until the game is over */
int game_loop(void *arg)
{
void (*func) () = arg;
while (!game_over) {
func();
uthread_yield();
}
return 0;
}
/* play the game! */
int begin_game(void *arg)
{
set_term_state();
set_screensize();
initialize_board();
struct uthread_attr a = UT_DEF_ATTR;
uthread_create(a, game_loop, listen_for_keypress);
uthread_create(a, game_loop, generate_bites);
uthread_create(a, game_loop, print_board);
uthread_create(a, game_loop, move_snake);
uthread_create(a, game_loop, delay);
/* wait until game is over */
while (!game_over) {
uthread_yield();
}
print_board();
printf("game over! your score: %d\n"
"press any key to exit\n", snake_head - snake_tail);
/* might already be waiting for a keypress.. if not, wait now */
if (!waiting_for_key) {
uthread_create(a, listen_for_keypress, 0);
}
while (waiting_for_key) {
uthread_yield();
}
restore_term_state();
return 0;
}
/* Main */
int main(void)
{
uthread_create(UT_DEF_ATTR, begin_game, 0);
return uthread_joinall();
}