-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
201 lines (173 loc) · 3.85 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
#include <stddef.h>
#include "snake.h"
Snake* snake_init()
{
Snake *snake = (Snake*) malloc(sizeof(Snake));
if (snake == NULL) return NULL;
snake->head = NULL;
snake->tail = NULL;
snake->size = 0;
snake->direction = RIGHT;
return snake;
}
void snake_free(Snake *snake)
{
if (snake == NULL) return;
Node *current_node = snake->head;
while (current_node != NULL)
{
Node *node_to_free = current_node;
current_node = current_node->next;
free(node_to_free);
}
free(snake);
}
bool snake_dequeue(Snake *snake)
{
if (snake == NULL || snake->head == NULL) return false;
Node *old_head = snake->head;
snake->head = snake->head->next;
free(old_head);
snake->size--;
return true;
}
bool snake_enqueue(Snake *snake, int y, int x)
{
Node *new_tail = (Node*) malloc(sizeof(Node));
if (snake == NULL || new_tail == NULL) return false;
new_tail->y = y;
new_tail->x = x;
new_tail->next = NULL;
if (snake->size == 0) {
snake->tail = new_tail;
snake->head = new_tail;
snake->size++;
} else {
snake->tail->next = new_tail;
snake->tail = new_tail;
snake->size++;
}
return true;
}
int snake_size(Snake *snake)
{
if (snake == NULL) return 0;
return snake->size;
}
bool snake_alive(Snake *snake, int y_limit, int x_limit)
{
Node *current_node = snake->head;
int head_x = current_node->x;
int head_y = current_node->y;
// Check that the snake's head is within the game screen
if (
head_x > x_limit || head_x < 0 ||
head_y > y_limit || head_y < 0
) {
return false;
}
// Check that the snake has not bitten itself
current_node = current_node->next;
while (current_node != NULL)
{
int current_x = current_node->x;
int current_y = current_node->y;
if (current_x == head_x && current_y == head_y) return false;
current_node = current_node->next;
}
return true;
}
void grow_snake(Snake *snake)
{
int direction = snake->direction;
Node *tail = snake->tail;
switch (direction)
{
case UP:
snake_enqueue(snake, tail->y - 1, tail->x);
break;
case DOWN:
snake_enqueue(snake, tail->y + 1, tail->x);
break;
case LEFT:
snake_enqueue(snake, tail->y, tail->x - 1);
break;
case RIGHT:
snake_enqueue(snake, tail->y, tail->x + 1);
break;
default:
break;
}
}
void feed_snake(Snake *snake, Food *food, int *score, int y_limit, int x_limit)
{
int snake_x = snake->head->x;
int snake_y = snake->head->y;
int direction = snake->direction;
switch (direction)
{
case UP:
snake_y--;
break;
case DOWN:
snake_y++;
break;
case LEFT:
snake_x--;
break;
case RIGHT:
snake_x++;
default:
break;
}
Node *current_node = food->head;
while (current_node != NULL)
{
int food_x = current_node->x;
int food_y = current_node->y;
if (snake_x == food_x && snake_y == food_y) {
food_remove(food, food_y, food_x);
food_add(food, rand() % y_limit, rand() % x_limit);
grow_snake(snake);
int score_int = *score;
score_int++;
*score = score_int;
return;
}
current_node = current_node->next;
}
}
void render_snake(Snake *snake)
{
Node *current_node = snake->head;
while (current_node != NULL)
{
attron(COLOR_PAIR(SNAKE_PAIR));
mvaddch(current_node->y, current_node->x, SNAKE_CHAR);
attroff(COLOR_PAIR(SNAKE_PAIR));
current_node = current_node->next;
}
}
void update_snake(Snake *snake)
{
snake_dequeue(snake);
int current_y = snake->tail->y;
int current_x = snake->tail->x;
switch (snake->direction)
{
case UP:
snake_enqueue(snake, current_y - 1, current_x);
break;
case DOWN:
snake_enqueue(snake, current_y + 1, current_x);
break;
case LEFT:
snake_enqueue(snake, current_y, current_x - 1);
break;
case RIGHT:
snake_enqueue(snake, current_y, current_x + 1);
break;
default:
break;
}
}