summaryrefslogtreecommitdiff
path: root/snek.c
blob: cbe8dd61f2711698496c4f5bcf0c7e402b8537b3 (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
#include <SDL2/SDL.h>
#include <stdlib.h>

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

int pos(int x, int y, int width, int height) {
	return (x % width) + (y % height) * width;
}

int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *win = SDL_CreateWindow("Snek automata", 0, 0, 800, 600, 0);
	SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

	int width = 40;
	int height = 20;
	int *grid = calloc(width * height, sizeof(int));
	int direction = RIGHT;
	int snake_length = 3;
	grid[pos(10, 10, width, height)] = 1;
	grid[pos(15, 15, width, height)] = 1 << 15;

	SDL_Event e = {0};
	int quit = 0;
	while (e.type != SDL_QUIT && !quit) {
		SDL_PollEvent(&e);
		int state_len = -1;
		const Uint8 *state = SDL_GetKeyboardState(&state_len);
		if (state[SDL_SCANCODE_UP] && direction != DOWN) {
			direction = UP;
		}
		if (state[SDL_SCANCODE_DOWN] && direction != UP) {
			direction = DOWN;
		}
		if (state[SDL_SCANCODE_LEFT] && direction != RIGHT) {
			direction = LEFT;
		}
		if (state[SDL_SCANCODE_RIGHT] && direction != LEFT) {
			direction = RIGHT;
		}
		SDL_SetRenderDrawColor(ren, 20, 20, 20, 255);
		SDL_RenderClear(ren);
		for (int i = 0; i < width * height; i++) {
			int cell = grid[i] & 0xffff;
			if (cell == (1 << 15) || cell == 0) {
				int x = 0;
				int y = 0;
				switch (direction) {
					case UP: y++; break;
					case DOWN: y+=height-1; break;
					case LEFT: x++; break;
					case RIGHT: x+=width-1; break;
				}
				int peek = grid[pos(i % width + x, i / width + y, width, height)] & 0xffff;
				grid[i] = (cell << 16) | cell;
				if (peek == 1) {
					grid[i] = (1 << 16) | cell;
					if (cell == (1 << 15)) {
						snake_length++;
						int new_pos = pos(rand(), rand(), width, height);
						while (grid[new_pos] & 0xffff != 0) {
							new_pos = pos(rand(), rand(), width, height);
						}
						grid[new_pos] |= ((1 << 15) << 16) | (1 << 15);
					}
				}
			}
			if (cell > 0 && cell < (1 << 15)) {
				if (cell <= snake_length) {
					grid[i] = ((cell + 1) << 16) | cell;
				}
			}
		}
		for (int i = 0; i < width * height; i++) {
			grid[i] = grid[i] >> 16;

			int cell = grid[i] & 0xffff;
			if (cell == (1 << 15)) {
				SDL_SetRenderDrawColor(ren, 200, 200, 0, 255);
			} else if (cell < (1 << 15) && cell > 0) {
				SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
			} else {
				SDL_SetRenderDrawColor(ren, 40, 40, 40, 255);
			}
			int y = (i / width) * 11;
			int x = (i % width) * 11;
			int w = 10;
			int h = 10;
			SDL_Rect r = { x, y, w, h };
			SDL_RenderFillRect(ren, &r);
		}

		SDL_RenderPresent(ren);
		SDL_Delay(50);
	}
	SDL_Quit();
	return 0;
}