-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
316 lines (267 loc) · 6.32 KB
/
snake.cpp
File metadata and controls
316 lines (267 loc) · 6.32 KB
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
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include "snake.h"
SnakeBody::SnakeBody()
{
}
SnakeBody::SnakeBody(int x, int y): mX(x), mY(y)
{
}
int SnakeBody::getX() const
{
return mX;
}
int SnakeBody::getY() const
{
return mY;
}
bool SnakeBody::operator==(const SnakeBody& other) const
{
return mX == other.mX && mY == other.mY;
}
Snake::Snake(int gameBoardWidth, int gameBoardHeight, int initialSnakeLength): mGameBoardWidth(gameBoardWidth), mGameBoardHeight(gameBoardHeight), mInitialSnakeLength(initialSnakeLength)
{
this->initializeSnake();
this->setRandomSeed();
}
void Snake::setRandomSeed()
{
// use current time as seed for random generator
std::srand(std::time(nullptr));
}
void Snake::initializeSnake()
{
// Instead of using a random initialization algorithm
// We always put the snake at the center of the game mWindows
mSnake.clear();
int centerX = this->mGameBoardWidth / 2;
int centerY = this->mGameBoardHeight / 2;
for (int i = 0; i < this->mInitialSnakeLength; i ++)
{
this->mSnake.push_back(SnakeBody(centerX, centerY + i));
}
this->mDirection = Direction::Up;
}
bool Snake::isPartOfSnake(int x, int y)
{
for (const SnakeBody& bodyPart : this->mSnake){
if (bodyPart.getX() == x && bodyPart.getY() == y){
return true;
}
}
return false;
}
/*
* Assumption:
* Only the head would hit wall.
*/
bool Snake::hitWall()
{
// TODO check if the snake has hit the wall
int xhead = this-> mSnake[0].getX();
int yhead = this-> mSnake[0].getY();
if (xhead <= 0 || xhead >= this->mGameBoardWidth-1 ||
yhead <= 0 || yhead >= this->mGameBoardHeight-1){
return true;
}
return false;
}
/*
* The snake head is overlapping with its body
*/
bool Snake::hitSelf()
{
SnakeBody& head = this->mSnake[0];
for (size_t i = 1; i < this->mSnake.size(); ++i)
{
// check if bite itself
if (head == this->mSnake[i])
{
return true;
}
}
return false;
}
bool Snake::touchFood()
{
return !mSnake.empty() && (mSnake[0] == mFood);
}
void Snake::senseFood(SnakeBody food)
{
this->mFood = food;
}
void Snake::sensePortalFood(SnakeBody portalFood)
{
this->mPortalFood = portalFood;
}
bool Snake::touchPortalFood()
{
return !mSnake.empty() && (mSnake[0] == mPortalFood);
}
void Snake::grow() {
if (!mSnake.empty())
mSnake.push_back(mSnake.back());
}
void Snake::teleportSnake()
{
if (mSnake.empty()) return;
int attempts = 0;
int newX, newY;
do {
newX = std::rand() % mGameBoardWidth;
newY = std::rand() % mGameBoardHeight;
attempts++;
} while (isPartOfSnake(newX, newY) && attempts < 100);
if (attempts < 100) {
mSnake[0] = SnakeBody(newX, newY);
}
}
const std::vector<SnakeBody>& Snake::getSnake()
{
return this->mSnake;
}
bool Snake::changeDirection(Direction newDirection)
{
switch (this->mDirection)
{
case Direction::Up:
{
if (newDirection == Direction::Down){
return false;
}
break;
}
case Direction::Down:
{
if (newDirection == Direction::Up){
return false;
} break;
}
case Direction::Left:
{
if (newDirection == Direction::Right){
return false;
}
break;
}
case Direction::Right:
{
if (newDirection == Direction::Left){
return false;
}
break;
}
}
this->mDirection = newDirection;
return true;
}
SnakeBody Snake::createNewHead()
{
if (mSnake.empty()) return SnakeBody(0, 0); // 判空保护
int headX = this->mSnake[0].getX();
int headY = this->mSnake[0].getY();
// Modify according to current direction
switch (this->mDirection)
{
case Direction::Up:
headY -= 1;
break;
case Direction::Down:
headY += 1;
break;
case Direction::Left:
headX -= 1;
break;
case Direction::Right:
headX += 1;
break;
}
SnakeBody newHead(headX, headY);
return newHead;
}
/*
* If eat food, return true, otherwise return false
*/
bool Snake::moveFoward()
{
if (mSnake.empty()) return false;
SnakeBody newHead = this->createNewHead();
this->mSnake.insert(this->mSnake.begin(), newHead); // Add new head
if (this->touchFood())
{
return true; // Eat food
}
else
{
// Not eat food
this->mSnake.pop_back();
return false;
}
}
bool Snake::checkCollision()
{
if (this->hitWall() || this->hitSelf())
{
return true;
}
else
{
return false;
}
}
int Snake::getLength()
{
return this->mSnake.size();
}
void Snake::setSpeedMultiplier(float multiplier) {
if (multiplier < 0.5f) multiplier = 0.5f;
if (multiplier > 3.0f) multiplier = 3.0f;
mSpeedMultiplier = multiplier;
}
void Snake::decreaseHitPoints(int amount) {
mHitPoints -= amount;
if (mHitPoints < 0) mHitPoints = 0;
if (mHitPoints > 0) {
mHitEffectTimer = 0.5f; // 0.5秒闪烁效果
}
}
void Snake::resetToInitial() {
initializeSnake();
mDirection = Direction::Up;
}
void Snake::shrink() {
if (mSnake.size() > 1) {
mSnake.pop_back();
}
}
void Snake::teleportToPosition(int x, int y)
{
if (mSnake.empty()) return;
if (x >= 0 && x < mGameBoardWidth && y >= 0 && y < mGameBoardHeight) {
mSnake[0] = SnakeBody(x, y);
}
}
void Snake::increaseHitPoints(int amount) {
mHitPoints += amount;
if (mHitPoints > 3) mHitPoints = 3;
}
void Snake::rollback(int steps) {
for (int s = 0; s < steps; ++s) {
if (mSnake.size() <= 1) break;
SnakeBody tail = mSnake.back();
mSnake.pop_back();
mSnake.insert(mSnake.begin(), tail);
}
}
void Snake::reverseDirection()
{
switch (mDirection) {
case Direction::Up: mDirection = Direction::Down; break;
case Direction::Down: mDirection = Direction::Up; break;
case Direction::Left: mDirection = Direction::Right; break;
case Direction::Right: mDirection = Direction::Left; break;
}
}