-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
77 lines (62 loc) · 1.55 KB
/
Copy pathsnake.cpp
File metadata and controls
77 lines (62 loc) · 1.55 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
#include "snake.h"
void Snake::advance() {
for (vector<Vertex>::reverse_iterator i = chain.rbegin(); i != chain.rend(); ++i) {
i->step();
if (i + 1 != chain.rend())
i->dir = (i+1)->dir;
}
}
void Snake::changeDirection(direction new_direction) {
if (chain.empty()) return;
chain[0].dir = new_direction;
}
direction Snake::getDirection() const {
if (chain.empty()) throw EmptySnake();
return chain[0].dir;
}
Snake& Snake::operator+=(const Vertex& vertex) {
chain.push_back(vertex);
score++;
return *this;
}
Snake& Snake::operator-=(int disqualifications) {
assert(disqualifications >= 0);
for (int i = 0; i < disqualifications; ++i) {
lives--;
if (lives < 0)
throw NoLivesLeft();
}
return *this;
}
Snake& Snake::operator--() {
return this->operator-=(1);
}
Vertex& Snake::operator[](int index){
if (index < 0 || index >= (int)chain.size()) throw OutOfRange();
return chain[index];
}
int Snake::getScore() const{
return score;
}
int Snake::getLives() const {
return lives;
}
int Snake::length() const {
return chain.size();
}
Snake& Snake::reset() {
chain = vector<Vertex>(1, Vertex(default_x, default_y, default_dir));
return *this;
}
vector<Vertex>::iterator Snake::begin() {
return chain.begin();
}
vector<Vertex>::iterator Snake::end() {
return chain.end();
}
vector<Vertex>::const_iterator Snake::begin() const {
return chain.begin();
}
vector<Vertex>::const_iterator Snake::end() const {
return chain.end();
}