-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
53 lines (47 loc) · 1.23 KB
/
Copy pathgame.cpp
File metadata and controls
53 lines (47 loc) · 1.23 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
#include <game.hpp>
void game::move_snake() {
if (gotfood == true) {
snakelist.push_back(food);
gotfood = false;
}
// Calculate snake
datapoint next;
if (movement == 0) {
next = {snakelist.front().x,
(gridheight + snakelist.front().y - 1) % (gridheight)};
} else if (movement == 1) {
next = {(snakelist.front().x + 1) % gridwidth, snakelist.front().y};
} else if (movement == 2) {
next = {snakelist.front().x, (snakelist.front().y + 1) % gridheight};
} else if (movement == 3) {
next = {(gridwidth + snakelist.front().x - 1) % gridwidth,
snakelist.front().y};
}
snakelist.push_front(next);
snakelist.pop_back();
}
bool game::gamebreak() {
auto iter = snakelist.begin();
++iter;
for (; iter != snakelist.end(); ++iter) {
if (snakelist.front() == *iter) return true;
}
return false;
}
bool game::foodeaten() {
for (const auto p : snakelist) {
if (p == food) {
gotfood = true;
food = {distx(rng), disty(rng)};
return true;
}
}
// for (auto it = snakelist.begin(); it != snakelist.end(); ++it) {
// if (*it == food) {
// gotfood = true;
// food = {distx(rng), disty(rng)};
// return true;
// }
// }
return false;
}