aboutsummaryrefslogtreecommitdiffstats
path: root/snake.c
blob: 019228af23da2fd219ce0f07e81fef01a18f5050 (plain)
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
#include <ncurses.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

enum colorpair {
	C_BLANK = 1,
	C_WALL,
	C_SEGMENT0,
	C_SEGMENT1,
	C_APPLE
};

enum direction {
	D_UP,
	D_DOWN,
	D_LEFT,
	D_RIGHT
};

enum state {
	S_NORMAL,
	S_WON,
	S_FAIL
};

struct segment {
	uint32_t x, y;
};

struct snake {
	struct segment segments[UINT16_MAX];
	uint16_t head, tail;
	enum direction direction;
} snake;

struct apple {
	uint32_t x, y;
};

struct {
	struct snake snake;
	struct apple apple;
	uint32_t width, height;
	bool running;
	enum state state;
} game;

void init_game(void);
void init_ncurses(void);
void main_loop(void);

int main(void)
{
	init_ncurses();
	init_game();
	main_loop();

	return EXIT_SUCCESS;
}

void init_ncurses(void)
{
	initscr();
	cbreak();
	noecho();
	nonl();
	start_color();

	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	nodelay(stdscr, TRUE);

	init_pair(C_BLANK, COLOR_BLACK, COLOR_BLACK);
	init_pair(C_WALL, COLOR_WHITE, COLOR_WHITE);
	init_pair(C_SEGMENT0, COLOR_YELLOW, COLOR_YELLOW);
	init_pair(C_SEGMENT1, COLOR_GREEN, COLOR_GREEN);
	init_pair(C_APPLE, COLOR_RED, COLOR_RED);

	bkgd(COLOR_PAIR(C_BLANK));
}

void init_game(void)
{
	game.running = true;
	game.state = S_NORMAL;

	game.snake.direction = D_UP;

	getmaxyx(stdscr, game.height, game.width);

	game.snake.head = 1;
	game.snake.tail = 0;

	game.snake.segments[game.snake.tail].x = game.width / 2;
	game.snake.segments[game.snake.tail].y = game.height / 2;

	game.snake.segments[game.snake.head].x = game.width / 2;
	game.snake.segments[game.snake.head].y = game.height / 2 - 1;

	game.apple.x = game.width / 2;
	game.apple.y = game.height / 2 + 1;
}

void print_board(void);
void poll_input(void);
void simulate(void);

void main_loop(void)
{
	while (true) {
		poll_input();
		if (!game.running)
			break;
		simulate();
		if (!game.running)
			break;
		print_board();
		usleep(100000);
	}
}


void print_board(void)
{
	erase();

	for (uint16_t i = game.snake.tail; i <= game.snake.head; i++) {
		chtype ch;

		if (!!(i % 2))
			ch = ACS_BLOCK | COLOR_PAIR(C_SEGMENT0);
		else
			ch = ACS_BLOCK | COLOR_PAIR(C_SEGMENT1);
		mvaddch(game.snake.segments[i].y, game.snake.segments[i].x, ch);
	}


	mvaddch(game.apple.y, game.apple.x, ACS_BLOCK | COLOR_PAIR(C_APPLE));

	refresh();
}

void redirect(enum direction desired_dir);

void poll_input(void)
{
	int c;

	while ((c = getch()) != ERR)
		switch (c) {
		case 'h': redirect(D_LEFT); break;
		case 'j': redirect(D_DOWN); break;
		case 'k': redirect(D_UP); break;
		case 'l': redirect(D_RIGHT); break;
		case 'q': game.running = false; break;
		}
}

void redirect(enum direction desired_dir)
{
	struct segment *current = &game.snake.segments[game.snake.head];
	struct segment *previous = &game.snake.segments[game.snake.head - 1];

	if (desired_dir == game.snake.direction)
		return;

	if (current->x == previous->x)
		if (current->y > previous->y) {
			if (desired_dir == D_UP)
				return;
		} else {
			if (desired_dir == D_DOWN)
				return;
		}
	else if (current->y == previous->y)
		if (current->x > previous->x) {
			if (desired_dir == D_LEFT)
				return;
		} else {
			if (desired_dir == D_RIGHT)
				return;
		}

	game.snake.direction = desired_dir;
}

inline uint16_t rand_to_max(uint16_t max)
{
	double random;

	while ((random = rand()) == (double)RAND_MAX);
	return random / (double)RAND_MAX * (double)max;
}

bool part_of_snake(uint16_t x, uint16_t y);

void replace_apple(void)
{
	uint16_t x, y;

	while (true) {
		x = rand_to_max(game.width);
		y = rand_to_max(game.height);

		if (part_of_snake(x, y))
			continue;

		game.apple.x = x;
		game.apple.y = y;

		break;
	}
}

void simulate(void)
{
	struct segment *current = &game.snake.segments[game.snake.head];
	int64_t dx = 0, dy = 0, nx, ny;

	switch (game.snake.direction) {
	case D_UP: dy = -1; break;
	case D_DOWN: dy = 1; break;
	case D_LEFT: dx = -1; break;
	case D_RIGHT: dx = 1; break;
	}

	nx = (int64_t)current->x + dx;
	ny = (int64_t)current->y + dy;

	if (nx < 0 || nx >= game.width || ny < 0 || ny >= game.height) {
		game.running = false;
		game.state = S_FAIL;
		return;
	}

	if (nx == game.apple.x && ny == game.apple.y) {
		game.snake.head++;
		game.snake.segments[game.snake.head].x = nx;
		game.snake.segments[game.snake.head].y = ny;

		replace_apple();
		return;
	}

	if (part_of_snake(nx, ny)) {
		game.running = false;
		game.state = S_FAIL;
		return;
	}

	game.snake.head++;
	game.snake.segments[game.snake.head].x = nx;
	game.snake.segments[game.snake.head].y = ny;
	game.snake.tail++;
}

bool part_of_snake(uint16_t x, uint16_t y)
{
	for (uint16_t i = game.snake.tail; i != game.snake.head; i++)
		if (game.snake.segments[i].x == x && game.snake.segments[i].y == y)
			return true;

	return false;
}