about summary refs log tree commit diff
path: root/snake.c
blob: b0d7705806e1d4b868328481ef4425e32ec5bf15 (plain) (blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include <ncurses.h>
#include "linked_list.h"

/*
 * SORTA WORKS
 * BUG IN WHICH SNAKE NOT DRAWN WELL?
 * ADD BORDERS AND SCORE, HIGHSCORE AND TIME AT TOP
 */

void playGame(LinkedList* snake, int* apple, int* stats);
void drawSnake(LinkedList* snake, int* apple);
void init_snake(LinkedList* snake, int number_nodes);
int check_collision(LinkedList* snake, int* apple);
void move_left(LinkedList* snake, int* new, int* status);
void move_right(LinkedList* snake, int* new, int* status);
void move_up(LinkedList* snake, int* new, int* status);
void move_down(LinkedList* snake, int* new, int* status);
void follow(int* tmp, int* prev, LinkedList* snake, int* new);
void newApple(LinkedList* snake, int* apple);

int fps = 10;

int main()
{
	WINDOW* windw;
	LinkedList* snake;
	int stats[4], apple[2];
	int duration, score, win, highscore;
	
	srand(time(NULL));
	windw = initscr();
	clear();
	noecho();
	timeout(0);
	curs_set(0);

	snake = initialise_linked_list();
	apple[0] = 60;
	apple[1] = 15;

	/* add initial nodes */
	init_snake(snake, 5);

	/* set highscore */
	stats[3] = 1;

	playGame(snake, apple, stats);

	duration = stats[0];
	score = stats[1];
	win = stats[2];
	highscore = stats[3];
	
	endwin();

	if(win)
		printf("You won!\n");
	else
		printf("You died!\n");
	
	printf("Playtime: %ds\n", duration);
	printf("Score: %d\n", score);
	printf("Highscore: %d\n", highscore);

	return 0;
}

void playGame(LinkedList* snake, int* apple, int* stats)
{
	int start = (int)time(NULL);
	int score, win, new[0], highscore, real_fps;
	char c = 'a', temp;
	int status[0];
	score = win = *new = 0;
	highscore = stats[3];
	
	move_left(snake, new, status);
	while(1)
	{
		real_fps = fps + (score / 5);
		temp = c;
		c = getch();
		if(c != 'q' && c != 'w' && c != 'a' && c != 's' && c != 'd')
			c = temp;
		switch(c)
		{
			case 'q':
				goto end;
			case 'w':
				move_up(snake, new, status);
				break;
			case 'a':
				move_left(snake, new, status);
				break;
			case 's':
				move_down(snake, new, status);
				break;
			case 'd':
				move_right(snake, new, status);
				break;
			default:
				break;
		}

		switch(check_collision(snake, apple))
		{
			//apple
			case 1:
				score++;
				newApple(snake, apple);
				//new node
				*new = 1;
				break;
			//self
			case 2:
				win = 0;
				goto end;
			//wall
			case 3:
				win = 0;
				goto end;
			default:
				break;
		}
		clear();
		drawSnake(snake, apple);
                refresh();

		if(score >= highscore)
		{
			win = 1;
			highscore = score;
		}

		usleep(1000000 / real_fps);
	}
	
	end:
	// time
	*(stats + 0) = (int) (time(NULL) - start);
	// score
	*(stats + 1) = score;
	// win
	*(stats + 2) = win;
	//highscore
	*(stats + 3) = highscore;
}

void drawSnake(LinkedList* snake, int* apple)
{
	Node* element;

	mvaddstr(snake->head->y, snake->head->x, "o");

	element = snake->head->next;
	while(element)
	{
		mvaddstr(element->y, element->x, "*");
		element = element->next;
	}

	mvaddstr(apple[1], apple[0], "=");
}

void init_snake(LinkedList* snake, int number_nodes)
{
	int midx = COLS / 2; 
	int midy = LINES / 2;
	int start_x = midx - (number_nodes / 2);

    for(int i = 0; i < number_nodes; i++)
    {
        append_linked_list(snake, start_x + i, midy);
    }
}

int check_collision(LinkedList* snake, int* apple)
{
	Node* element;

	/* check collision with apple */
        if(snake->head->x == apple[0] && snake->head->y == apple[1])
                return 1;

	/* check collision with self */
	element = snake->head->next;
	while(element)
	{
		if(snake->head->x == element->x && snake->head->y == element->y)
			return 2;
		element = element->next;
	}

	/* check collision with wall */
	if((snake->head->y == 0 || snake->head->y == LINES) || (snake->head->x == 0 || snake->head->x == COLS))
		return 3;

	/* no collision */
	return 0;
}

void move_left(LinkedList* snake, int* new, int* status)
{
	/* You cant move left if youre going right */
	if(*status == 1)
	{
		move_right(snake, new, status);
		return;
	}
	int tmp[2];
        int prev[2];

        *(prev + 0) = snake->head->x;
        *(prev + 1) = snake->head->y;
        snake->head->x = snake->head->x - 1;

        follow(tmp, prev, snake, new);
	*status = 0;
}

void move_right(LinkedList* snake, int* new, int* status)
{
	if(*status == 0)
	{
		move_left(snake, new, status);
		return;
	}
	int tmp[2];
        int prev[2];

        *(prev + 0) = snake->head->x;
        *(prev + 1) = snake->head->y;
        snake->head->x = snake->head->x + 1;

        follow(tmp, prev, snake, new);
	*status = 1;
}

void move_up(LinkedList* snake, int* new, int* status)
{
	if(*status == 3)
	{
		move_down(snake, new, status);
		return;
	}
	int tmp[2];
	int prev[2];

        *(prev + 0) = snake->head->x;
	*(prev + 1) = snake->head->y;
        snake->head->y = snake->head->y - 1;
	
	follow(tmp, prev, snake, new);
	*status = 2;
}

void move_down(LinkedList* snake, int* new, int* status)
{
	if(*status == 2)
	{
		move_up(snake, new, status);
		return;
	}
	int tmp[2];
        int prev[2];

        *(prev + 0) = snake->head->x;
        *(prev + 1) = snake->head->y;
        snake->head->y = snake->head->y + 1;

        follow(tmp, prev, snake, new);
	*status = 3;
}

void follow(int* tmp, int* prev, LinkedList* snake, int* new)
{
        Node* element;
        element = snake->head->next;
        while(element)
        {
                *(tmp + 0) = element->x;
                *(tmp + 1) = element->y;
		
                element->x = *(prev + 0);
                element->y = *(prev + 1);

                *(prev + 0) = *(tmp + 0);
                *(prev + 1) = *(tmp + 1);

                element = element->next;
        }

	if(*new)
	{
		append_linked_list(snake,  *(prev + 0),  *(prev + 1));
		*new = 0;
	}
}

void newApple(LinkedList* snake, int* apple)
{
	Node* element;

	apple[0] = (rand() % (COLS - 0 + 1)) + 0;
        apple[1] = (rand() % (LINES - 0 + 1)) + 0;
	
	element = snake->head;
	if(element)
	{
		if(element->x == apple[0] && element->y == apple[1])
			newApple(snake, apple);
		element = element->next;
	}
}