-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_ai.cpp
222 lines (198 loc) · 6.84 KB
/
simple_ai.cpp
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
/// this just go fisrt to the x or y coordinate and then to the other
#include "simple_ai.h"
SimpleAI::SimpleAI(int speed, int difficulty) : _matrix(){
// Serial.begin(9600);
_matrix.begin();
randomSeed(analogRead(4) + analogRead(5));
_snake_length = 1;
_multiplier = float(1) + difficulty*0.02;
_default_speed = speed;
_speed = speed * float(_multiplier);
_playing = true;
coordinates _snake[96];
_snake[0].x = _screen_width/2;
_snake[0].y = _screen_height/2;
coordinates _food;
generate_food();
get_direction();
}
void SimpleAI::move(){
// Move the body of the snake
get_direction();
for (int i = _snake_length; i > 0; i--){
_snake[i].x = _snake[i-1].x;
_snake[i].y = _snake[i-1].y;
}
// Move the head of the snake
if (_direction == "up"){
_snake[0].y -= 1;
}else if (_direction == "right"){
_snake[0].x += 1;
}else if (_direction == "down"){
_snake[0].y += 1;
}else if (_direction == "left"){
_snake[0].x -= 1;
}
}
void SimpleAI::check_for_collisions(){
// If the snake collides with the food
if (_snake[0].x == _food.x && _snake[0].y == _food.y){
_snake_length++; // Increase the length of the snake
_speed *= float(_multiplier); // Increase the speed of the snake
generate_food(); // Generate a new food
}
// If the snake collides with itself
for (int i = 1; i < _snake_length; i++){
if (_snake[0].x == _snake[i].x && _snake[0].y == _snake[i].y){ // If the head of the snake collides with the body of the snake
_playing = false;
}
}
// If the snake is out of the screen
if (_snake[0].x > _screen_width-1){
_playing = false;
}else if (_snake[0].x < 0){
_playing = false;
}else if (_snake[0].y < 0){
_playing = false;
}else if (_snake[0].y > _screen_height-1){
_playing = false;
}
}
void SimpleAI::render_on_screen(){
// Create clear screen
byte on_screen[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// Render the snake
for (int i = 0; i < _snake_length; i++){
on_screen[_snake[i].y][_snake[i].x] = 1;
}
// Render the food
on_screen[_food.y][_food.x] = 1;
_matrix.renderBitmap(on_screen, 8, 12);
}
void SimpleAI::reset_variables(){
_snake_length = 1;
_speed = _default_speed;
_playing = true;
_snake[0].x = _screen_width/2;
_snake[0].y = _screen_height/2;
generate_food();
get_direction();
}
int SimpleAI::get_score(){
return _snake_length - 1;
}
void SimpleAI::get_direction(){
if (_direction == "up"){
if (_food.y >= _snake[0].y){
if (_food.x < _snake[0].x){
_direction = "left";
}else if (_food.x > _snake[0].x){
_direction = "right";
}else{
// I need to get how close is from left and right wall
int from_left = _snake[0].x;
int from_right = _screen_width-1 - _snake[0].x;
if (from_left > from_right){
_direction = "left";
}else if (from_left < from_right){
_direction = "right";
}else{
_direction = random(0, 2) == 0 ? "left" : "right";
}
}
}
}else if (_direction == "down"){
if (_food.y <= _snake[0].y){
if (_food.x < _snake[0].x){
_direction = "left";
}else if (_food.x > _snake[0].x){
_direction = "right";
}else{
// I need to get how close is from left and right wall
int from_left = _snake[0].x;
int from_right = _screen_width-1 - _snake[0].x;
if (from_left > from_right){
_direction = "left";
}else if (from_left < from_right){
_direction = "right";
}else{
_direction = random(0, 2) == 0 ? "left" : "right";
}
}
}
}else if (_direction == "left"){
if (_food.x >= _snake[0].x){
if (_food.y < _snake[0].y){
_direction = "up";
}else if (_food.y > _snake[0].y){
_direction = "down";
}else{
// I need to get how close is from up and down wall
int from_up = _snake[0].y;
int from_down = _screen_height-1 - _snake[0].y;
if (from_up > from_down){
_direction = "up";
}else if (from_up < from_down){
_direction = "down";
}else{
_direction = random(0, 2) == 0 ? "up" : "down";
}
}
}
}else if (_direction == "right"){
if (_food.x <= _snake[0].x){
if (_food.y < _snake[0].y){
_direction = "up";
}else if (_food.y > _snake[0].y){
_direction = "down";
}else{
// I need to get how close is from up and down wall
int from_up = _snake[0].y;
int from_down = _screen_height-1 - _snake[0].y;
if (from_up > from_down){
_direction = "up";
}else if (from_up < from_down){
_direction = "down";
}else{
_direction = random(0, 2) == 0 ? "up" : "down";
}
}
}
}else{
int x_difference = abs(_food.x - _snake[0].x);
int y_difference = abs(_food.y - _snake[0].y);
if (x_difference > y_difference && x_difference != 0){
if (_food.x < _snake[0].x){
_direction = "left";
}else if (_food.x > _snake[0].x){
_direction = "right";
}
}else if (y_difference > x_difference && y_difference != 0){
if (_food.y < _snake[0].y){
_direction = "up";
}else if (_food.y > _snake[0].y){
_direction = "down";
}
}
}
}
void SimpleAI::generate_food(){
_food.x = random(0, _screen_width); // Generate a random x position for the food
_food.y = random(0, _screen_height); // Generate a random y position for the food
// To avoid the food to be generated on the snake
for (int i = 0; i < _snake_length; i++){
// If the food is generated on the snake, generate a new food
if (_food.x == _snake[i].x && _food.y == _snake[i].y){
generate_food();
}
}
}