-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
107 lines (92 loc) · 3.41 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
//
// Created by Daniel Eidlin on 17/07/2020.
//
#include "Snake.h"
#include "BodyPart.h"
Snake::Snake() : length(0), movable(false), apple(false) {}
Snake::Snake(int length, std::pair<int, int> spawnCoordinates) : length(length), movable(false),
previousTailCoordinates(spawnCoordinates),
apple(false) {
for (int i = 0; i < this->length - 1; i++) {
BodyPart bodyPart = BodyPart(std::pair<int, int>(spawnCoordinates.first + i, spawnCoordinates.second),
Direction::Right);
bodyParts.push_back(bodyPart);
}
BodyPart bodyPart = BodyPart(
std::pair<int, int>(spawnCoordinates.first + this->length - 1, spawnCoordinates.second), Direction::Right);
bodyParts.push_back(bodyPart);
}
Snake::~Snake() = default;
BodyPart Snake::getHeadPart() const {
return bodyParts.back();
}
std::vector<BodyPart> Snake::getBodyParts() const {
return bodyParts;
}
void Snake::move() {
std::pair<int, int> headCoordinates = getHeadPart().getCoordinates();
Direction headDirection = getHeadPart().getDirection();
switch (headDirection) {
case Direction::Right:
// move right
bodyParts.push_back(
BodyPart(std::pair<int, int>(headCoordinates.first + 1, headCoordinates.second), Direction::Right));
if (!hasApple()) {
previousTailCoordinates = bodyParts.front().getCoordinates();
bodyParts.erase(bodyParts.begin());
}
setApple(false);
break;
case Direction::Left:
// move left
bodyParts.push_back(
BodyPart(std::pair<int, int>(headCoordinates.first - 1, headCoordinates.second), Direction::Left));
if (!hasApple()) {
previousTailCoordinates = bodyParts.front().getCoordinates();
bodyParts.erase(bodyParts.begin());
}
setApple(false);
break;
case Direction::Up:
// move up
bodyParts.push_back(
BodyPart(std::pair<int, int>(headCoordinates.first, headCoordinates.second - 1), Direction::Up));
if (!hasApple()) {
previousTailCoordinates = bodyParts.front().getCoordinates();
bodyParts.erase(bodyParts.begin());
}
setApple(false);
break;
case Direction::Down:
// move down
bodyParts.push_back(
BodyPart(std::pair<int, int>(headCoordinates.first, headCoordinates.second + 1), Direction::Down));
if (!hasApple()) {
previousTailCoordinates = bodyParts.front().getCoordinates();
bodyParts.erase(bodyParts.begin());
}
setApple(false);
break;
default:
// do nothing
break;
}
}
std::pair<int, int> Snake::getPreviousTailCoordinates() const {
return previousTailCoordinates;
}
bool Snake::CanMove() const {
return movable;
}
void Snake::setCanMove(bool canMove) {
movable = canMove;
}
bool Snake::hasApple() const {
return apple;
}
void Snake::setApple(bool hasApple) {
apple = hasApple;
}
void Snake::changeHeadDirection(Direction newDirection) {
bodyParts.back().setDirection(newDirection);
}