-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
238 lines (198 loc) · 4.2 KB
/
Snake.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include "Snake.h"
#include "SnakeBoard.h"
SDL_Color Snake::SNAKE_COLOR = {0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE}; // green
Snake::Snake(int row, int col) : Snake(DIRECTION_UP, row, col) {}
Snake::Snake(Snake::Direction d, int row, int col) :
head_(new Block(row, col)),
tail_(head_),
toGrow_(0),
direction_(d)
{}
Snake::~Snake()
{
Block *block = tail_;
while (block != nullptr)
{
Block *prev = block->getPrev();
delete block;
block = prev;
}
}
bool Snake::collidesWith(std::pair<int, int> coords) const
{
Block *block = head_;
while (block != nullptr)
{
if (block->collidesWith(coords))
return true;
block = block->getNext_();
}
return false;
}
void Snake::draw(SDL_Renderer *renderer) const
{
Block *block = head_;
while (block != nullptr)
{
SnakeBoard::drawBlock(renderer, Snake::SNAKE_COLOR, block->getRow(), block->getColumn());
block = block->getNext_();
}
}
int Snake::getHeadRow() const
{
return head_->getRow();
}
int Snake::getHeadColumn() const
{
return head_->getColumn();
}
void Snake::grow()
{
grow(1);
}
void Snake::grow(int n)
{
toGrow_ += n;
}
bool Snake::headCollidesWith(std::pair<int, int> coords) const
{
return head_->collidesWith(coords);
}
bool Snake::headCollidesWithSelf() const
{
Block *b = tail_;
while (b != head_)
{
if (b->collidesWith(*head_))
return true;
b = b->getPrev();
}
return false;
}
void Snake::move()
{
newHead();
if (toGrow_) // if we need to grow the Snake, we do so by not deleting the tail
{
toGrow_--;
}
else
{
deleteTail();
}
}
bool Snake::setDirectionIfPossible(Snake::Direction d)
{
if ((direction_ == DIRECTION_UP && d == DIRECTION_DOWN) || (direction_ == DIRECTION_DOWN && d == DIRECTION_UP)
|| (direction_ == DIRECTION_LEFT && d == DIRECTION_RIGHT) || (direction_ == DIRECTION_RIGHT && d == DIRECTION_LEFT))
{
return false;
}
else
{
direction_ = d;
return true;
}
}
int Snake::size() const
{
return head_->size();
}
void Snake::deleteTail()
{
Block *oldTail = tail_;
tail_ = tail_->getPrev();
delete oldTail;
}
std::pair<int, int> Snake::getNewRowAndColumn()
{
int r = head_->getRow();
int c = head_->getColumn();
switch (direction_)
{
case DIRECTION_UP:
r -= 1;
break;
case DIRECTION_DOWN:
r += 1;
break;
case DIRECTION_LEFT:
c -= 1;
break;
case DIRECTION_RIGHT:
c += 1;
break;
}
return std::make_pair(r, c);
}
void Snake::newHead()
{
std::pair<int, int> coords = getNewRowAndColumn();
auto *newHead = new Block(coords.first, coords.second, head_);
head_->setPrev(newHead);
head_ = newHead;
}
Snake::Block::Block(int row, int col) : Block(row, col, nullptr) {}
Snake::Block::Block(int row, int col, Block *next):
row_(row),
col_(col),
prev_(nullptr),
next_(next)
{}
Snake::Block::~Block()
{
if (prev_ != nullptr)
{
prev_->next_ = nullptr;
}
if (next_ != nullptr)
{
next_->prev_ = nullptr;
}
}
// Accessors
int Snake::Block::getRow() const
{
return row_;
}
int Snake::Block::getColumn() const
{
return col_;
}
Snake::Block* Snake::Block::getPrev() const
{
return prev_;
}
/**
* Get the Block ahead of this one (i.e. the one immediately closer to the tail).
*
* @return a pointer to the Block which comes after this one.
*/
Snake::Block* Snake::Block::getNext_() const
{
return next_;
}
void Snake::Block::setPrev(Snake::Block *const pBlock)
{
prev_ = pBlock;
}
void Snake::Block::setNext(Snake::Block *const pBlock)
{
next_ = pBlock;
}
bool Snake::Block::collidesWith(std::pair<int, int> coords) const
{
return (row_ == coords.first && col_ == coords.second);
}
bool Snake::Block::collidesWith(const Block& otherBlock) const
{
return (row_ == otherBlock.row_ && col_ == otherBlock.col_);
}
int Snake::Block::size() const
{
if (next_ == nullptr)
return 1;
else
return 1 + next_->size();
}