-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsnake.hpp
155 lines (145 loc) · 3.23 KB
/
csnake.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef SNAKE_GAME
#define SNAKE_GAME
#ifndef CSNAKE_NO_TUI
#include <ncurses.h>
#endif
#include <vector>
namespace csnake {
/**
* Represents 2-dimensional point/vector.
* Two instances of vec2 can be added with + operator or compared with == operator.
*/
struct vec2 {
int x, y;
vec2 operator+(vec2 v);
bool operator==(vec2 v);
};
class SnakeGame {
private:
/**
* The direction in which snake's head will go; eg. { 1, 0 } means up and { 0, 1 } means right
*/
vec2 direction;
/**
* Position of food.
*/
vec2 food;
/**
* Current score (snake's length - 1)
*/
int score;
/**
* Height and width of board.
*/
int height, width;
/**
* Are walls impenetrable?
* @see #SnakeGame(int height, int width, WINDOW *win, bool impenetrable_walls)
*/
bool impenetrable_walls;
/**
* Move food to new random position. Is called automatically when snake collects food.
*/
void moveFood(void);
public:
/**
* Contains positions of all snake's nodes.
*/
std::vector<vec2> snake;
/**
* Constructor.
* Note: PRNG should be initialized using srand function before any instances of SnakeGame are created.
*
* @param height game board height
* @param width game board width
* @param impenetrable_walls upon hitting walls snake:
* dies if true
* moves to the other side if false
*/
SnakeGame(int width, int height, bool impenetrable_walls);
/**
* Change movement direction if possible ie. when direction is changed by 90 deg.
*
* @param new_direction new movement direction:
* w - up
* d - right
* s - down
* a - left
*/
void changeDirection(char new_direction);
/**
* Get movement direvtion.
*
* @return movement direction
*/
vec2 getDirection(void);
/**
* Get food position.
*
* @return food position
*/
vec2 getFood(void);
/**
* Get board height.
*
* @return board height
*/
int getHeight(void);
/**
* Get current score.
*
* @return current score
*/
int getScore(void);
/**
* Get board width.
*
* @return board width
*/
int getWidth(void);
/**
* Move snake by one tile. If food is collected score and snake's length get increased and #_move_food(void) is called.
*
* @return false if snake hits its own body, true otherwise.
*/
bool update(void);
};
#ifndef CSNAKE_NO_TUI
class Renderer {
private:
/**
* Pointer to ncurses window.
*/
WINDOW *win;
/**
* Pointer to game drawn by renderer.
*/
SnakeGame *game;
/**
* Used for rendering board to screen.
*/
std::vector<std::vector<char>> board;
public:
Renderer(SnakeGame *game, WINDOW *win);
/**
* Call #SnakeGame::changeDirection(char new_direction) with wgetch(win) as argument
*/
void handleInput(void);
/**
* Draw board to ncurses window. Shouldn't be called before #initRendering(void).
*/
void render(void);
/**
* Change rendered game.
*
* @param game new game
*/
void setGame(SnakeGame *game);
};
/**
* Run new game as TUI application.
*/
void run(void);
#endif
}
#endif