-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.h
330 lines (301 loc) · 5.88 KB
/
snake.h
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
324
325
326
327
328
329
330
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <unistd.h>
#include <sys/time.h>
#include <thread>
#include <mutex>
#define DELAY_TIME 1000
#define SCREEN 0
#define RPI true
#define RED 0
#define GREEN 2
#define BLUE 1
#define LINE 3
#define nullptr NULL
#ifndef RPI
#include <windows.h>
#endif
using namespace std;
mutex m;
int map[8][8];
//empty:0
//snake No.1 : 1
//snake No.2 : 2
//apple:3
struct pos
{
int x, y;
};
void print_snake();
void print_map()
{
system("clear");
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
class Snake
{
public:
class Node
{
public:
pos node_pos;
Node *next;
int stat;
Node(int input_x, int input_y);
void left() { node_pos.y = (node_pos.y - 1 + 8) % 8; };
void right() { node_pos.y = (node_pos.y + 1 + 8) % 8; };
void up() { node_pos.x = (node_pos.x - 1 + 8) % 8; };
void down() { node_pos.x = (node_pos.x + 1 + 8) % 8; };
void update();
};
Node *head;
//snake's direction
int head_dir;
int food_x;
int food_y;
//Direction{UP,DOWN,LEFT,RIGHT};
Snake();
void update();
void setDir(int dir);
void move();
void start();
void getFoodLocation();
void createFood();
bool isTouchFood();
void eatFood();
bool isGameOver();
void initMap();
};
Snake::Node::Node(int input_x, int input_y) : node_pos({input_x, input_y}), stat(1)
{
next = nullptr;
}
void Snake::Node::update()
{
if (stat)
{
map[node_pos.x][node_pos.y] = 1;
}
else
{
map[node_pos.x][node_pos.y] = 0;
}
}
//birth point:(0,0) direction:RIGHT
Snake::Snake()
{
head = new Node(0, 2);
Node *mid = new Node(0, 1);
Node *tail = new Node(0, 0);
head_dir = 0;
food_x = 4;
food_y = 4;
head->next = mid;
mid->next = tail;
tail->next = nullptr;
m.lock();
head->update();
mid->update();
tail->update();
m.unlock();
}
void Snake::start()
{
update();
//direction from joystick
int dir;
srand((unsigned)time(NULL));
createFood();
while (1)
{
m.lock();
dir = rand() % 4;
switch (dir)
{
case 0:
case 1:
if (head_dir == 2 || head_dir == 3)
head_dir = dir;
break;
case 2:
case 3:
if (head_dir == 0 || head_dir == 1)
head_dir = dir;
break;
}
move();
if (isTouchFood())
{
eatFood();
createFood();
}
if (SCREEN)
print_map();
m.unlock();
#ifdef RPI
delay(DELAY_TIME);
#else
Sleep(DELAY_TIME);
#endif
}
}
void Snake::setDir(int dir)
{
head_dir = dir;
}
void Snake::update()
{
Node *p = head;
do
{
p = p->next;
} while (p != nullptr);
}
void Snake::move()
{
Node *p = head;
Node *newtail = nullptr;
//find tail
if (p->next != nullptr)
{
while (p->next->next != nullptr)
{
p = p->next;
}
}
newtail = p;
p = p->next;
newtail->next = nullptr;
p->stat = 0;
p->update();
p->node_pos = head->node_pos;
p->next = head->next;
head->next = p;
switch (head_dir)
{
case 0:
head->up();
break;
case 1:
head->down();
break;
case 2:
head->left();
break;
case 3:
head->right();
break;
}
if (isGameOver())
{
//cout << "game over";
//system("pause");
}
head->update();
}
void Snake::getFoodLocation()
{
srand((unsigned)time(NULL));
food_x = rand() % 8;
food_y = rand() % 8;
while (map[food_x][food_y] != 0)
{
food_x = rand() % 8;
food_y = rand() % 8;
}
}
void Snake::createFood()
{
getFoodLocation();
map[food_x][food_y] = 3;
}
bool Snake::isTouchFood()
{
Node *p = head;
if (p->node_pos.x == food_x && p->node_pos.y == food_y)
{
return true;
}
return false;
}
void Snake::eatFood()
{
Node *p = head;
while (p->next != nullptr)
{
p = p->next;
}
Node *newnode = new Node(p->node_pos.x, p->node_pos.y);
p->next = newnode;
newnode->next = nullptr;
}
bool Snake::isGameOver()
{
Node *p = head;
if (map[p->node_pos.x][p->node_pos.y] == 1 || map[p->node_pos.x][p->node_pos.y] == 2)
{
return true;
}
return false;
}
void Snake::initMap()
{
memset(map, 0, sizeof(map));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void print_snake()
{
wiringPiSetup();
wiringPiSPISetup(0, 500000);
while(1){
uint8_t data[4] = {0x0, 0x0, 0x0, 0x0};
uint8_t d[3][8] = {0};
m.lock();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
switch (map[i][j])
{
// snake RED
case 1:
d[0][i] += 0x01 << 7 - j;
break;
// snake BLUE
case 2:
d[2][i] += 0x01 << 7 - j;
break;
// apple GREEN
case 3:
d[1][i] += 0x01 << 7 - j;
break;
}
}
}
//print_map();
m.unlock();
// iterate every line
for (int i = 0; i < 8; i++)
{
data[RED] = ~d[0][i];
data[GREEN] = ~d[1][i];
data[BLUE] = ~d[2][i];
data[LINE] = 0x01 << 7 - i;
// send data SPI
wiringPiSPIDataRW(0, data, sizeof(data));
delay(2);
}
//delay(10);
}
}