-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.h
118 lines (80 loc) · 2.31 KB
/
Snake.h
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
#ifndef SNAKE_SNAKE_H
#define SNAKE_SNAKE_H
#include <utility>
#include <SDL2/SDL.h>
class Snake
{
public:
/**
* An enum representing the four directions in which a Snake can travel.
*/
enum Direction
{
DIRECTION_NONE = -1,
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_LEFT,
DIRECTION_RIGHT
};
Snake(int row, int col);
Snake(Direction, int row, int col);
~Snake();
bool collidesWith(std::pair<int, int>) const;
void draw(SDL_Renderer *) const;
int getHeadRow() const;
int getHeadColumn() const;
void grow();
void grow(int);
bool headCollidesWith(std::pair<int, int>) const;
bool headCollidesWithSelf() const;
void move();
bool setDirectionIfPossible(Direction);
int size() const;
private:
class Block;
static SDL_Color SNAKE_COLOR;
Block *head_;
Block *tail_;
int toGrow_;
Direction direction_;
void deleteTail();
std::pair<int, int> getNewRowAndColumn();
void newHead();
class Block
{
public:
Block(int row, int col);
Block(int row, int col, Block *next);
~Block();
// Accessors
int getRow() const;
int getColumn() const;
Block *getPrev() const;
/**
* Get the SnakeBlock ahead of this one (i.e. the one immediately closer to the tail).
* @return a pointer to the SnakeBlock which comes after this one.
*/
Block *getNext_() const;
void setPrev(Block *const pBlock);
void setNext(Block *const pBlock);
/**
* Checks if this Block collides with the given coordinates.
*
* @param the coordinates (row, column) to check
* @return true if this Block overlaps with the given coordinates, or false otherwise
*/
bool collidesWith(std::pair<int, int>) const;
bool collidesWith(const Block& otherBlock) const;
int size() const;
private:
// The 0th row is at the top of the window
int row_;
// The 0th column is at the left of the window
int col_;
// prev_ is the Block immediately closer to the head
Block *prev_;
// next_ is the Block immediately closer to the tail
Block *next_;
};
};
#endif //SNAKE_SNAKE_H