-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
194 lines (164 loc) · 3.86 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 <unistd.h>
#include <stdlib.h>
#include <time.h>
#define DELAY 130000
#define SNAKE "O"
#define FOOD "X"
typedef struct {
int x;
int y;
} Direction;
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point location;
bool available;
} Food;
typedef struct {
Point *body;
int length;
Direction direction;
bool dead;
} Snake;
typedef struct {
int x;
int y;
} WindowSize;
void MoveSnake(const WindowSize *, Snake *, Food *);
void GenerateFood(const WindowSize *, const Snake *, Food *);
void UpdateDirection(char, Snake *);
int main()
{
/*
* initialize ncurses screen
*/
initscr();
noecho();
curs_set(FALSE);
nodelay(stdscr, TRUE); // don't block on getch() call
WindowSize window_size = {0, 0};
getmaxyx(stdscr, window_size.y, window_size.x); // get window size
window_size.x--;
window_size.y--;
/*
* initialize game
*/
srand(time(NULL));
Snake snake = {(Point *)malloc(sizeof(Point)), 1, {0, 0}, false};
snake.body[0].x = 0;
snake.body[0].y = 0;
Food food = {{0, 0}, false};
/*
* main loop
*/
window_size.y--; // last line reserved for the below instruction
mvprintw(window_size.y + 1, 0, "Quit the game with 'q'");
mvprintw(snake.body[0].y, snake.body[0].x, SNAKE);
GenerateFood(&window_size, &snake, &food);
mvprintw(food.location.y, food.location.x, FOOD);
refresh();
while (1)
{
char key_stroke = getch();
switch (key_stroke)
{
case 'q':
goto END;
default:
UpdateDirection(key_stroke, &snake);
}
clear();
mvprintw(window_size.y + 1, 0, "Quit the game with 'q'");
MoveSnake(&window_size, &snake, &food);
if (snake.dead)
break;
if (!food.available)
GenerateFood(&window_size, &snake, &food);
mvprintw(food.location.y, food.location.x, FOOD);
refresh();
usleep(DELAY);
}
/*
* finalize ncurses screen
*/
END:
endwin();
free(snake.body);
return EXIT_SUCCESS;
}
void MoveSnake(const WindowSize *window_size, Snake *snake, Food *food)
{
int old_head_x = snake->body[0].x, old_head_y = snake->body[0].y;
int new_head_x, new_head_y;
new_head_x = old_head_x + snake->direction.x;
new_head_y = old_head_y + snake->direction.y;
// detect collision
if (new_head_x < 0 || new_head_x > window_size->x || new_head_y < 0 || new_head_y > window_size->y)
{
snake->dead = true;
return;
}
for (int i = 1; i < snake->length; ++i)
if (new_head_x == snake->body[i].x && new_head_y == snake->body[i].y)
{
snake->dead = true;
return;
}
// detect whether food is eaten by next move
if (new_head_x == food->location.x && new_head_y == food->location.y)
{
food->available = false;
snake->length++;
snake->body = realloc(snake->body, snake->length * sizeof(Point)); // extend the snake by 1
}
// move snake
for (int i = snake->length - 1; i > 0; --i)
{
snake->body[i].x = snake->body[i-1].x;
snake->body[i].y = snake->body[i-1].y;
}
snake->body[0].x = new_head_x;
snake->body[0].y = new_head_y;
for (int i = 0; i < snake->length; ++i)
mvprintw(snake->body[i].y, snake->body[i].x, SNAKE);
}
void GenerateFood(const WindowSize *window_size, const Snake *snake, Food *food)
{
bool food_on_snake = true;
// generate a food which is not on the body of snake
while (food_on_snake)
{
food->location.x = rand() % window_size->x;
food->location.y = rand() % window_size->y;
for (int i = 0; i < snake->length; ++i)
if (food->location.x == snake->body[i].x && food->location.y == snake->body[i].y)
food_on_snake = true;
food_on_snake = false;
}
food->available = true;
}
void UpdateDirection(char key_stroke, Snake *snake)
{
switch(key_stroke)
{
case 'h':
snake->direction.x = -1;
snake->direction.y = 0;
break;
case 'j':
snake->direction.x = 0;
snake->direction.y = 1;
break;
case 'k':
snake->direction.x = 0;
snake->direction.y = -1;
break;
case 'l':
snake->direction.x = 1;
snake->direction.y = 0;
break;
}
}