-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.hh
164 lines (135 loc) · 3.63 KB
/
snake.hh
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
#include <random>
#include <deque>
#include <algorithm>
struct Pos {
int x,y;
bool operator==(const Pos&that) const {
return x == that.x && y == that.y;
}
};
class Snake {
std::deque<Pos> body;
bool dead;
Pos food; // Maybe body[0] should be the food not the head?
int BOARD_SIZE;
public:
enum direction_t {
Up, Down, Left, Right
};
enum content_t {
Corpse, Body, Food, Empty, Head
};
int get_board_size() {
return BOARD_SIZE;
}
private:
direction_t new_direction;
direction_t direction;
void spawn_food() {
static std::uniform_int_distribution<int> randomLocationRange(0, BOARD_SIZE-1);
static std::random_device rd;
static std::mt19937 randomNumbers(rd());
do {
food.x = randomLocationRange( randomNumbers );
food.y = randomLocationRange( randomNumbers );
} while ( (std::find(body.begin(), body.end(), Pos{food.x,food.y}) != body.end() ));
}
public:
void setDirection( direction_t dir ) {
if ( dir == Left && direction != Right) {
new_direction = Left;
}
else if ( dir == Right && direction != Left) {
new_direction = Right;
}
else if ( dir == Up && direction != Down) {
new_direction = Up;
}
else if ( dir == Down && direction != Up){
new_direction = Down;
}
}
void reset() {
dead=false;
body.clear();
body.push_front(Pos{ BOARD_SIZE / 2, BOARD_SIZE / 2});
direction=Left;
new_direction=Left;
spawn_food();
}
Snake( int board_size ) {
BOARD_SIZE = board_size;
reset();
}
bool pulse() {
if (dead)
return false;
direction = new_direction;
int head_x = body.front().x;
int head_y = body.front().y;
switch (direction) {
case Left:
head_x--; break;
case Right:
head_x++;break;
case Up:
head_y--;break;
case Down:
head_y++;break;
}
if (head_x < 0 || head_y < 0 || head_x == BOARD_SIZE || head_y == BOARD_SIZE) {
dead = true;
} else if ( std::find(body.begin(), body.end(), Pos{head_x,head_y}) != body.end() ) {
dead = true;
} else {
body.push_front(Pos{head_x,head_y});
if ( head_x == food.x && head_y == food.y) {
spawn_food();
} else {
body.pop_back();
}
}
return true;
}
//
// Iteration logic
//
struct contents_t {
int x, y;
content_t content;
};
class iterator {
Snake *parent;
int index; // Maybe we could the the deque iterator internally.
public:
iterator( Snake* _parent) : parent{ _parent }, index( 0 ) {}
bool operator!=(const iterator &that) const {
return this->parent != that.parent || index != that.index;
}
iterator& operator++() {
++index;
return *this;
}
contents_t operator*() const {
if (index == 0) {
return contents_t{parent->food.x, parent->food.y, Food};
} else if (index == 1 && !parent->dead) {
return contents_t{parent->body[0].x, parent->body[0].y, Head};
} else {
if (parent->dead)
return contents_t{parent->body[index - 1].x, parent->body[index - 1].y, Corpse};
else
return contents_t{parent->body[index - 1].x, parent->body[index - 1].y, Body};
}
}
friend Snake;
};
iterator begin() {
return iterator(this);
}
iterator end() {
auto i = iterator(this);
i.index = body.size() + 1;
return i;
}
};