-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
145 lines (120 loc) · 3.81 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
#include "snake.h"
char screen[8] = {
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b0000000,
};
void print_snake(Snake *snake){
int head_x = snake->body[0].x;
int head_y = snake->body[0].y;
int length = snake->length;
printf("\nSnake:\n--------------\nX: %d\nY: %d\nLen: %d\n", head_x, head_y, length);
};
// Ta funkcja ma za zadanie zaktualizować pozycje ogona po przesunięciu głowy
// kolejne elementy przyjmują wartości, elementu je poprzedzającego
void update_tail(Snake *snake) {
printf("Tail:\n-----------------\n");
for(int i=snake->length; i>0; i--) {
snake->body[i].x = snake->body[i-1].x;
snake->body[i].y = snake->body[i-1].y;
printf("%d, %d\n", snake->body[i].x, snake->body[i].y);
}
printf("-----------------\n");
};
void clear_display(char display[]){
for (int i=0 ; i<8; i++) display[i] = 0b0;
};
// Sprawdza czy sam na siebie nie wchodzi
int check_if_not_overlapping(Snake *snake, int x, int y){
int result = 1;
for (int i=0; i<snake->length; i++) {
// printf("%d %d %d %d\n", snake->body[i].x, x, snake->body[i].y, y);
if(snake->body[i].x == x && snake->body[i].y == y) result = 0;
}
return result;
};
int overlap_check(Snake *snake, int x, int y) {
for (int i=0; i<10; i++) {
if (snake->body[i].x == x && snake->body[i].y == y) {
return 1;
}
}
return 0;
}
// AUTOPILOT FUNCTION - needed??? Only for testing... So, yeah...
int move_head(Snake *snake, Food *food, int current_target){
int result = 0;
int sbx = snake->body[0].x, sby = snake->body[0].y;
int fpx = food->position[current_target].x, fpy = food->position[current_target].y;
if ((sbx > fpx) && (overlap_check(snake, sbx-1, sby) == 0)) {
snake->body[0].x --;
printf("UP\n");
}
else if ((sbx < fpx) && (overlap_check(snake, sbx+1, sby) == 0)) {
snake->body[0].x ++;
printf("DOWN\n");
}
else if ((sby > fpy) && (overlap_check(snake, sbx, sby-1) == 0)) {
snake->body[0].y --;
printf("LEFT\n");
}
else if ((sby < fpy) && (overlap_check(snake, sbx, sby+1) == 0)) {
snake->body[0].y ++;
printf("RIGHT\n");
}
return result;
};
void render_screen(char my_screen[]){
printf("\n----------------\n");
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
if (my_screen[i] & (1 << j)) printf(" *");
else printf(" ");
}
printf("\n");
}
printf("----------------\n");
};
void set_screen(Food *food, Snake *snake, int current_target){
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
if (food->position[current_target].x == i && food->position[current_target].y == j)
{
char row = screen[i];
row = 1 << j;
screen[i] |= row;
}
for (int k=0; k<10; k++)
{
if (snake->body[k].x == i && snake->body[k].y == j)
{
char row = screen[i];
row = 1 << j;
screen[i] |= row;
}
}
}
}
};
void print_tail_info(Snake *snake){
for (int i=0; i<10; i++) {
printf("#%d X: %d, Y: %d\n", i, snake->body[i].x, snake->body[i].y);
}
};
int food_reached(Snake *snake, Coord *food){
int result = 0;
for (int i=0; i<snake->length+1; i++) {
if(snake->body[i].x == food->x && snake->body[i].y == food->y) result = 1;
// else printf("%d", i);
}
return result;
}