-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex.cpp
63 lines (55 loc) · 1.52 KB
/
vertex.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
#include "vertex.h"
bool Vertex::rangeCheck(int x, int y){
return x >= width || y >= height || x < 0 || y < 0;
}
direction Vertex::getDirectionFromChar(char c) {
switch (c) {
case 'w': return UP;
case 's': return DOWN;
case 'd': return RIGHT;
case 'a': return LEFT;
case 'q': return EXIT;
default: return NONE;
}
}
bool Vertex::oppositeDirections(direction first, direction second) {
return (first == UP && second == DOWN) || \
(first == DOWN && second == UP) || \
(first == RIGHT && second == LEFT) || \
(first == LEFT && second == RIGHT);
}
Vertex& Vertex::operator+=(const Vertex& other) {
int new_x = x + other.x, new_y = y + other.y;
if (rangeCheck(new_x ,new_y)) throw OutOfRange();
x = new_x, y = new_y;
return *this;
}
int Vertex::getX() const {
return this->x;
}
int Vertex::getY() const {
return this->y;
}
Vertex& Vertex::step() {
switch (dir) {
case DOWN:
y = (y + jump < height) ? y + jump : throw OutOfRange();
break;
case UP:
y = y - jump > 0 ? y - jump : throw OutOfRange();
break;
case RIGHT:
x = (x + jump < width) ? x + jump : throw OutOfRange();
break;
case LEFT:
x = x - jump > 0 ? x - jump : throw OutOfRange();
break;
default: //nothing
break;
}
return *this;
}
Vertex Vertex::peekStep() const {
Vertex next = Vertex(*this);
return next.step();
}