Snoke
snake.cpp
Go to the documentation of this file.
1 
11 #include "snake.h"
12 
20 bool Snake::init(Point begin, short dir, int length)
21 {
22  begin.style.letter = '*';
23  if (dir == MVLEFT || dir == MVRIGHT || dir == MVUP || dir == MVDOWN)
24  {
25  /*
26  * Pointer to the variable we want to change, depending on the dir variable
27  * @type {short*}
28  */
29  short* changable;
30  if (dir == MVLEFT || dir == MVRIGHT)
31  {
32  changable = &begin.x;
33  }
34  else
35  {
36  changable = &begin.y;
37  }
38 
39  /*
40  * The amount we will change the changable variable, depending on the dir variable
41  * @type {short}
42  */
43  short value;
44  if (dir == MVRIGHT || dir == MVDOWN)
45  {
46  value = 1;
47  }
48  else
49  {
50  value = -1;
51  }
52 
53  /*
54  * Snake can not exist in non-positive coords
55  */
56  if (*changable + value * length + 1 < 1 || begin.x < 1 || begin.y < 1)
57  {
58  return false;
59  }
60 
61  for (int i = 0; i < length; i++)
62  {
63  snakeBody.push_front(begin);
64  *changable += value;
65  }
66  direction = dir;
67  return true;
68  }
69  return false;
70 }
71 
80 bool Snake::move(Labyrinth* labyrinth)
81 {
82  Point frCoords = snakeBody.front();
83  Point bCoords = snakeBody.back();
84 
85  /*
86  * Getting the future head coords to check intersections
87  */
88  switch (direction)
89  {
90  case MVRIGHT:
91  {
92  frCoords.x++;
93  break;
94  }
95  case MVLEFT:
96  {
97  frCoords.x--;
98  break;
99  }
100  case MVDOWN:
101  {
102  frCoords.y++;
103  break;
104  }
105  case MVUP:
106  {
107  frCoords.y--;
108  break;
109  }
110  default:
111  {
112  return false;
113  }
114  }
115 
116  /*
117  * Flag of whether there was a wall collision and therefor
118  * we need to teleport snake's head to the opposite side of the labyrinth
119  */
120  bool wallFlag = 0;
121 
122  /*
123  * Checking intersections with borders, obstacles and Ball and deciding where to put the head of the snake
124  */
125  switch (checkIntersection(frCoords, labyrinth))
126  {
127  case WALLUP:
128  {
129  frCoords.y = gameFieldSize.y - 2;
130  wallFlag = 1;
131  break;
132  }
133  case WALLBOT:
134  {
135  frCoords.y = 1;
136  wallFlag = 1;
137  break;
138  }
139  case WALLLEFT:
140  {
141  frCoords.x = gameFieldSize.x - 2;
142  wallFlag = 1;
143  break;
144  }
145  case WALLRIGHT:
146  {
147  frCoords.x = 1;
148  wallFlag = 1;
149  break;
150  }
151  case COLL:
152  {
153  moveBack(bCoords, &labyrinth->change);
154  return true;
155  }
156  case NOCOLL:
157  {
158  break;
159  }
160  case BALL:
161  {
162  bCoords.x = -1;
163  bCoords.y = -1;
164  //Change to a single call to labyrinth(like labyrinth->newBall())
165  labyrinth->change.rmPoint(labyrinth->ball.getCoords());
166  labyrinth->generateBall();
167  labyrinth->change.addPoint(labyrinth->ball.getCoords());
168  break;
169  }
170  default:
171  {
172  break;
173  }
174  }
175 
176  /*
177  * If there was a wall collision, then we need to check
178  * the new head position of the snake for colliding with Ball or obstacles
179  */
180  if(wallFlag)
181  {
182  switch (checkIntersection(frCoords, labyrinth))
183  {
184  case COLL:
185  {
186  moveBack(bCoords, &labyrinth->change);
187  return true;
188  }
189  case NOCOLL:
190  {
191  break;
192  }
193  case BALL:
194  {
195  bCoords.x = -1;
196  bCoords.y = -1;
197  labyrinth->generateBall();
198  break;
199  }
200  default:
201  {
202  break;
203  }
204  }
205  }
206 
207  /*
208  * If there was no obstacle collision then proceed and add Points to the
209  * change array, staging them for addition/removal to/from labyrinth
210  */
211  moveHead(frCoords, &labyrinth->change);
212  moveBack(bCoords, &labyrinth->change);
213  return false;
214 
215 }
216 
222 void Snake::moveHead(Point p, Change* change)
223 {
224  change->addPoint(p);
225  snakeBody.push_front(p);
226 }
227 
233 void Snake::moveBack(Point p, Change* change)
234 {
235  if(change->rmPoint(p))
236  {
237  snakeBody.pop_back();
238  }
239 }
240 
248 short Snake::checkIntersection(Point check, Labyrinth* labyrinth)
249 {
250  if ((!labyrinth->isFree(check)) == 1)
251  {
252  return checkWisely(check, labyrinth->ball.getCoords());
253  }
254  return NOCOLL;
255 
256 }
257 
264 short Snake::checkWisely(Point check, Point bCoords)
265 {
266  if(check.x == bCoords.x && check.y == bCoords.y)
267  {
268  return BALL;
269  }
270  else if (check.x == gameFieldSize.x - 1)
271  {
272  return WALLRIGHT;
273  }
274  else if (check.x == 0)
275  {
276  return WALLLEFT;
277  }
278  else if (check.y == gameFieldSize.y - 1)
279  {
280  return WALLBOT;
281  }
282  else if (check.y == 0)
283  {
284  return WALLUP;
285  }
286  return COLL;
287 }
288 
295 {
296  if (direction == MVRIGHT || direction == MVLEFT || direction == MVUP || direction == MVDOWN){
297  /*
298  * If directions are opposite, do nothing
299  */
300  if (this->direction + direction == 0)
301  {
302  return false;
303  }
304 
305  this->direction = direction;
306  }
307  return true;
308 }
309 
314 {
315  return this->direction;
316 }
317 
322 {
323  Point headCoords = snakeBody.front();
324  return headCoords;
325 }
326 
331 void Snake::getCoords(std::list<Point>* currBody)
332 {
333  currBody->assign(snakeBody.begin(), snakeBody.end());
334 }
short checkIntersection(Point check, Labyrinth *laryrinth)
Checking the given point for intersections with Ball, borders, obstacles.
Definition: snake.cpp:248
bool addPoint(Point p)
adds Point to addition array if it fits the criteria
Definition: change.cpp:28
short x
Definition: common.h:55
class for containing and displaying the game field
Definition: labyrinth.h:66
std::deque< Point > snakeBody
Definition: snake.h:84
bool move(Labyrinth *labyrinth)
The snake movement on the game field(should be called in each iteration of the game cycle...
Definition: snake.cpp:80
bool rmPoint(Point p)
adds Point to removal array if it fits the criteria
Definition: change.cpp:43
const int NOCOLL
the tested segment is NOT intersecting with any Point of the game labyrinth
Definition: snake.h:43
short checkWisely(Point coords, Point bcoords)
If the Point of the labyrinth we are checking is not free then decide the type of collision...
Definition: snake.cpp:264
Ball ball
Definition: labyrinth.h:83
void moveHead(Point p, Change *change)
Moving snake head to a described by parameters position and updating the addition to the labyrinth...
Definition: snake.cpp:222
void getCoords(std::list< Point > *currBody)
The method to get the snake whole body coordinates(x ,y) without giving the direct access...
Definition: snake.cpp:331
bool generateBall()
Generate the Ball.
Definition: labyrinth.cpp:83
const int BALL
the tested segment is intersecting with the Ball
Definition: snake.h:47
short getDirection()
The method to get the current direction without giving the direct access.
Definition: snake.cpp:313
A Support Change class.
Definition: change.h:21
bool setDirection(int dir)
The method to set the direction where the snake is heading.
Definition: snake.cpp:294
const int WALLLEFT
the tested segment is intersecting with the left boundary of the game field
Definition: snake.h:31
Point getCoords()
Get the ball coords without giving the direct access.
Definition: ball.cpp:37
const int WALLUP
the tested segment is intersecting with the upper boundary of the game field
Definition: snake.h:23
bool init(Point begin, short direction, int length)
Initializes snake as an entity without ancoring it anywhere.
Definition: snake.cpp:20
const int WALLBOT
the tested segment is intersecting with the bottom boundary of the game field
Definition: snake.h:27
const int MVLEFT
snake&#39;s head is moving left
Definition: snake.h:60
const int COLL
the tested segment is intersecting with a non-boundary Point of the game field
Definition: snake.h:39
void moveBack(Point p, Change *change)
Checking if we need to remove the back of the snake from the labyrinth(we don&#39;t in case it has eaten ...
Definition: snake.cpp:233
Point gameFieldSize
Definition: game.cpp:17
PointStyle style
Definition: common.h:57
Change change
Definition: labyrinth.h:82
Point getHeadCoords()
The method to get the coordinates(x, y) of the snake&#39;s head without giving the direct access...
Definition: snake.cpp:321
short y
Definition: common.h:56
char letter
Definition: common.h:40
const int MVUP
snake&#39;s head is moving up
Definition: snake.h:64
short direction
Definition: snake.h:82
const int MVRIGHT
snake&#39;s head is moving right
Definition: snake.h:52
const int WALLRIGHT
the tested segment is intersecting with the right boundary of the game field
Definition: snake.h:35
Coordinates and style of the cell in console window.
Definition: common.h:52
Definitions and prototypes for Snake class.
short isFree(Point p)
Mark of if the asked Point is free.
Definition: labyrinth.cpp:254
const int MVDOWN
snake&#39;s head is moving down
Definition: snake.h:56