-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.hpp
106 lines (85 loc) · 1.49 KB
/
Snake.hpp
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
#ifndef SNAKE_HPP
# define SNAKE_HPP
# include <ncurses.h>
# include "Drawable.hpp"
# include <queue>
enum Direction {
UP = -1,
DOWN = 1,
LEFT = -2,
RIGHT = 2
};
class SnakePiece : public Drawable
{
private:
public:
SnakePiece() {
this->x = this->y = 0;
this->icon = '#';
};
SnakePiece(int y, int x) {
this->x = x;
this->y = y;
this->icon = '#';
};
};
class Snake
{
private:
std::queue<SnakePiece> snake;
Direction direction;
public:
Snake();
~Snake();
void add_piece(SnakePiece piece);
void remove_piece();
SnakePiece tail() const;
SnakePiece head() const;
Direction get_direction() const;
void set_direction(Direction d);
SnakePiece next_head();
};
Snake::Snake() {
this->direction = RIGHT;
}
Snake::~Snake() {
}
void Snake::add_piece(SnakePiece piece) {
this->snake.push(piece);
}
void Snake::remove_piece() {
this->snake.pop();
}
SnakePiece Snake::tail() const {
return this->snake.front();
}
SnakePiece Snake::head() const {
return this->snake.back();
}
Direction Snake::get_direction() const {
return this->direction;
}
void Snake::set_direction(Direction d) {
if (this->direction + d != 0)
this->direction = d;
}
SnakePiece Snake::next_head() {
int nextY = this->head().get_y();
int nextX = this->head().get_x();
switch(this->direction) {
case UP:
nextY--;
break;
case DOWN:
nextY++;
break;
case LEFT:
nextX--;
break;
case RIGHT:
nextX++;
break;
}
return SnakePiece(nextY, nextX);
}
#endif