-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
165 lines (143 loc) · 3.94 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
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define board_height 35
#define board_width 90
struct List {
int pos_x;
int pos_y;
struct List *next;
};
int get_food_x() {
int x = rand() % board_width;
if (x % 2) {
x++;
x %= board_width;
}
return x;
}
int get_food_y() {
return rand() % board_height;
}
bool check_food_on_snake(struct List *positions, int food_x, int food_y) {
struct List *it = positions;
while (it) {
if (food_x == it->pos_x && food_y == it->pos_y) {
return true;
}
it = it->next;
}
return false;
}
void draw_borders() {
char lower[board_width] = {[0 ... board_width-2] = '-'};
mvaddstr(board_height, 0, lower);
for (int y = 0; y < board_height; y++) {
mvaddch(y, board_width, '|');
}
}
void draw_snake(struct List *positions) {
struct List *it = positions;
while (it) {
mvaddch(it->pos_y, it->pos_x, '*');
it = it->next;
}
}
int main() {
initscr();
cbreak();
noecho();
intrflush(stdscr, false);
keypad(stdscr, true);
nodelay(stdscr, true);
struct List *positions = (struct List*)malloc(sizeof(struct List));
positions->pos_x = 0;
positions->pos_y = 0;
positions->next = NULL;
int dir_x = 0;
int dir_y = 0;
int food_x = get_food_x();
int food_y = get_food_y();
while (true) {
int pressed = getch();
if (pressed == KEY_LEFT && dir_x == 0) {
dir_x = -2;
dir_y = 0;
}
if (pressed == KEY_RIGHT && dir_x == 0) {
dir_x = 2;
dir_y = 0;
}
if (pressed == KEY_UP && dir_y == 0) {
dir_x = 0;
dir_y = -1;
}
if (pressed == KEY_DOWN && dir_y == 0) {
dir_x = 0;
dir_y = 1;
}
// update position
struct List *new_pos = (struct List*)malloc(sizeof(struct List));
new_pos->pos_x = positions->pos_x + dir_x + board_width;
new_pos->pos_y = positions->pos_y + dir_y + board_height;
new_pos->next = positions;
new_pos->pos_x %= board_width;
new_pos->pos_y %= board_height;
positions = new_pos;
const int current_x = positions->pos_x;
const int current_y = positions->pos_y;
// look if we hit food
if (current_x == food_x && current_y == food_y) {
food_x = get_food_x();
food_y = get_food_y();
while (check_food_on_snake(positions, food_x, food_y)) {
food_x = get_food_x();
food_y = get_food_y();
}
} else {
struct List *it = positions;
while (it->next->next) {
it = it->next;
}
free(it->next);
it->next = NULL;
}
// look for game over
struct List *it = positions->next;
int length = 1;
while (it) {
if (current_x == it->pos_x && current_y == it->pos_y) {
while (it) {
length++;
it = it->next;
}
char snake_length_str[50] = "SNAKE LENGTH: ";
char str[30];
snprintf(str, 30, "%d", length);
strcat(snake_length_str, str);
mvaddstr(board_height/2-3, board_width/2-7, "GAME OVER");
mvaddstr(board_height/2-1, board_width/2-10, snake_length_str);
refresh();
usleep(3e6);
goto end;
}
length++;
it = it->next;
}
// draw everything again
erase();
draw_borders();
draw_snake(positions);
mvaddch(food_y, food_x, '@');
usleep(50e3);
}
end:
while (positions) {
struct List *next = positions->next;
free(positions);
positions = next;
}
endwin();
return 0;
}