Snoke
Snake_move.h
Go to the documentation of this file.
1 #ifndef SNAKE_MOVE_H
2 #define SNAKE_MOVE_H
3 
12 #include "shared.h"
13 #include "Snake/snake.h"
14 #include "Labyrinth/labyrinth.h"
15 #include <iostream>
16 
20 TEST(Snake_move, test1)
21 {
22  gameFieldSize.x = 40;
23  gameFieldSize.y = 20;
24  Labyrinth labyrinth;
25 
26  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
27  if(!retVal)
28  {
29  FAIL();
30  }
31 
32  Snake snake;
33  Point begin;
34  begin.x = 1;
35  begin.y = 1;
36  short direction = MVRIGHT;
37  int length = 5;
38 
39  retVal = snake.init(begin, direction, length);
40  if(!retVal)
41  {
42  FAIL();
43  }
44 
45  retVal = labyrinth.addSnake(&snake);
46  if(!retVal)
47  {
48  FAIL();
49  }
50 
51  retVal = snake.move(&labyrinth);
52  if(!retVal)
53  {
54  SUCCEED();
55  }
56  else
57  {
58  FAIL();
59  }
60 }
61 
65 TEST(Snake_move, testwall1)
66 {
67  gameFieldSize.x = 40;
68  gameFieldSize.y = 20;
69  Labyrinth labyrinth;
70 
71  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
72  if(!retVal)
73  {
74  FAIL();
75  }
76 
77  Snake snake;
78  Point begin;
79  begin.x = 1;
80  begin.y = 5;
81  short direction = MVUP;
82  int length = 5;
83 
84  retVal = snake.init(begin, direction, length);
85  if(!retVal)
86  {
87  FAIL();
88  }
89 
90  retVal = labyrinth.addSnake(&snake);
91  if(!retVal)
92  {
93  FAIL();
94  }
95 
96  retVal = snake.move(&labyrinth);
97  if(retVal)
98  {
99  FAIL();
100  }
101 
102  Point hCoords = snake.getHeadCoords();
103  Point expCoords;
104  expCoords.x = 1;
105  expCoords.y = 18;
106  ASSERT_EQ(hCoords.x, expCoords.x);
107  ASSERT_EQ(hCoords.y, expCoords.y);
108 }
109 
113 TEST(Snake_move, testwall2)
114 {
115  gameFieldSize.x = 40;
116  gameFieldSize.y = 20;
117  Labyrinth labyrinth;
118 
119  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
120  if(!retVal)
121  {
122  FAIL();
123  }
124 
125  Snake snake;
126  Point begin;
127  begin.x = 34;
128  begin.y = 5;
129  short direction = MVRIGHT;
130  int length = 5;
131 
132  retVal = snake.init(begin, direction, length);
133  if(!retVal)
134  {
135  FAIL();
136  }
137 
138  retVal = labyrinth.addSnake(&snake);
139  if(!retVal)
140  {
141  FAIL();
142  }
143 
144  retVal = snake.move(&labyrinth);
145  if(retVal)
146  {
147  FAIL();
148  }
149 
150  Point hCoords = snake.getHeadCoords();
151  Point expCoords;
152  expCoords.x = 1;
153  expCoords.y = 5;
154  ASSERT_EQ(hCoords.x, expCoords.x);
155  ASSERT_EQ(hCoords.y, expCoords.y);
156 }
157 
161 TEST(Snake_move, testwall3)
162 {
163  gameFieldSize.x = 40;
164  gameFieldSize.y = 20;
165  Labyrinth labyrinth;
166 
167  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
168  if(!retVal)
169  {
170  FAIL();
171  }
172 
173  Snake snake;
174  Point begin;
175  begin.x = 5;
176  begin.y = 1;
177  short direction = MVLEFT;
178  int length = 5;
179 
180  retVal = snake.init(begin, direction, length);
181  if(!retVal)
182  {
183  FAIL();
184  }
185 
186  retVal = labyrinth.addSnake(&snake);
187  if(!retVal)
188  {
189  FAIL();
190  }
191 
192  retVal = snake.move(&labyrinth);
193  if(retVal)
194  {
195  FAIL();
196  }
197 
198  Point hCoords = snake.getHeadCoords();
199  Point expCoords;
200  expCoords.x = 38;
201  expCoords.y = 1;
202  ASSERT_EQ(hCoords.x, expCoords.x);
203  ASSERT_EQ(hCoords.y, expCoords.y);
204 }
205 
209 TEST(Snake_move, testwall4)
210 {
211  gameFieldSize.x = 40;
212  gameFieldSize.y = 20;
213  Labyrinth labyrinth;
214 
215  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
216  if(!retVal)
217  {
218  FAIL();
219  }
220 
221  Snake snake;
222  Point begin;
223  begin.x = 5;
224  begin.y = 14;
225  short direction = MVDOWN;
226  int length = 5;
227 
228  retVal = snake.init(begin, direction, length);
229  if(!retVal)
230  {
231  FAIL();
232  }
233 
234  retVal = labyrinth.addSnake(&snake);
235  if(!retVal)
236  {
237  FAIL();
238  }
239 
240  retVal = snake.move(&labyrinth);
241  if(retVal)
242  {
243  FAIL();
244  }
245 
246  Point hCoords = snake.getHeadCoords();
247  Point expCoords;
248  expCoords.x = 5;
249  expCoords.y = 1;
250  ASSERT_EQ(hCoords.x, expCoords.x);
251  ASSERT_EQ(hCoords.y, expCoords.y);
252 }
253 
257 TEST(Snake_move, testIntersection1)
258 {
259  gameFieldSize.x = 40;
260  gameFieldSize.y = 20;
261  Labyrinth labyrinth;
262 
263  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
264  if(!retVal)
265  {
266  FAIL();
267  }
268 
269  Snake snake;
270  Point begin;
271  begin.x = 1;
272  begin.y = 1;
273  short direction = MVRIGHT;
274  int length = 10;
275 
276  retVal = snake.init(begin, direction, length);
277  if(!retVal)
278  {
279  FAIL();
280  }
281 
282  retVal = labyrinth.addSnake(&snake);
283  if(!retVal)
284  {
285  FAIL();
286  }
287 
288  snake.setDirection(MVDOWN);
289  retVal = snake.move(&labyrinth);
290  if(retVal)
291  {
292  FAIL();
293  }
294 
295  snake.setDirection(MVLEFT);
296  retVal = snake.move(&labyrinth);
297  if(retVal)
298  {
299  FAIL();
300  }
301 
302  snake.setDirection(MVUP);
303  retVal = snake.move(&labyrinth);
304  if(retVal)
305  {
306  SUCCEED();
307  }
308  else
309  {
310  FAIL();
311  }
312 }
313 
317 TEST(Snake_move, testIntersection2)
318 {
319  gameFieldSize.x = 40;
320  gameFieldSize.y = 20;
321  Labyrinth labyrinth;
322 
323  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
324  if(!retVal)
325  {
326  FAIL();
327  }
328 
329  Snake snake;
330  Point begin;
331  begin.x = 1;
332  begin.y = 1;
333  short direction = MVRIGHT;
334  int length = 5;
335 
336  retVal = snake.init(begin, direction, length);
337  if(!retVal)
338  {
339  FAIL();
340  }
341 
342  retVal = labyrinth.addSnake(&snake);
343  if(!retVal)
344  {
345  FAIL();
346  }
347 
348  labyrinth.initBall();
349  labyrinth.rmPoint(labyrinth.ball.getCoords());
350  Point ballCoords;
351  ballCoords.x = 6;
352  ballCoords.y = 1;
353  labyrinth.ball.setCoords(ballCoords);
354  labyrinth.addPoint(labyrinth.ball.getCoords());
355 
356  retVal = snake.move(&labyrinth);
357  if(retVal)
358  {
359  FAIL();
360  }
361 
362 
363  Point hCoords = snake.getHeadCoords();
364  Point expHCoords;
365  expHCoords.x = 6;
366  expHCoords.y = 1;
367  ASSERT_EQ(hCoords.x, expHCoords.x);
368  ASSERT_EQ(hCoords.y, expHCoords.y);
369 
370  std::list<Point> cpBody;
371  snake.getCoords(&cpBody);
372  Point bCoords = cpBody.back();
373  Point expBCoords;
374  expBCoords.x = 1;
375  expBCoords.y = 1;
376  ASSERT_EQ(bCoords.x, expBCoords.x);
377  ASSERT_EQ(bCoords.y, expBCoords.y);
378 }
379 
383 TEST(Snake_move, testIntersection3)
384 {
385  gameFieldSize.x = 40;
386  gameFieldSize.y = 5;
387  Labyrinth labyrinth;
388 
389  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
390  if(!retVal)
391  {
392  FAIL();
393  }
394 
395  Snake snake;
396  Point begin;
397  begin.x = 1;
398  begin.y = 1;
399  short direction = MVRIGHT;
400  int length = 10;
401 
402  retVal = snake.init(begin, direction, length);
403  if(!retVal)
404  {
405  FAIL();
406  }
407 
408  retVal = labyrinth.addSnake(&snake);
409  if(!retVal)
410  {
411  FAIL();
412  }
413 
414  snake.setDirection(MVDOWN);
415  retVal = snake.move(&labyrinth);
416  if(retVal)
417  {
418  FAIL();
419  }
420 
421  retVal = snake.move(&labyrinth);
422  if(retVal)
423  {
424  FAIL();
425  }
426 
427  retVal = snake.move(&labyrinth);
428  if(retVal)
429  {
430  SUCCEED();
431  }
432  else
433  {
434  FAIL();
435  }
436 }
437 
441 TEST(Snake_move, testIntersection4)
442 {
443  gameFieldSize.x = 40;
444  gameFieldSize.y = 5;
445  Labyrinth labyrinth;
446 
447  bool retVal = labyrinth.setLabyrinth(gameFieldSize);
448  if(!retVal)
449  {
450  FAIL();
451  }
452 
453  Snake snake;
454  Point begin;
455  begin.x = 1;
456  begin.y = 3;
457  short direction = MVRIGHT;
458  int length = 10;
459 
460  retVal = snake.init(begin, direction, length);
461  if(!retVal)
462  {
463  FAIL();
464  }
465 
466  retVal = labyrinth.addSnake(&snake);
467  if(!retVal)
468  {
469  FAIL();
470  }
471 
472  labyrinth.initBall();
473  labyrinth.rmPoint(labyrinth.ball.getCoords());
474  Point ballCoords;
475  ballCoords.x = 10;
476  ballCoords.y = 1;
477  labyrinth.ball.setCoords(ballCoords);
478  labyrinth.addPoint(labyrinth.ball.getCoords());
479 
480  snake.setDirection(MVDOWN);
481  retVal = snake.move(&labyrinth);
482  if(retVal)
483  {
484  FAIL();
485  }
486 
487  Point hCoords = snake.getHeadCoords();
488  Point expHCoords;
489  expHCoords.x = 10;
490  expHCoords.y = 1;
491  ASSERT_EQ(hCoords.x, expHCoords.x);
492  ASSERT_EQ(hCoords.y, expHCoords.y);
493 
494  std::list<Point> cpBody;
495  snake.getCoords(&cpBody);
496  Point bCoords = cpBody.back();
497  Point expBCoords;
498  expBCoords.x = 1;
499  expBCoords.y = 3;
500  ASSERT_EQ(bCoords.x, expBCoords.x);
501  ASSERT_EQ(bCoords.y, expBCoords.y);
502 
503 }
504 
505 
506 
507 #endif // SNAKE_MOVE_H
short x
Definition: common.h:55
class for containing and displaying the game field
Definition: labyrinth.h:66
Definitions and prototypes for Labyrinth class.
bool initBall()
Ball initialization with labyrinth updating.
Definition: labyrinth.cpp:71
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
Ball ball
Definition: labyrinth.h:83
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 setLabyrinth(Point dimensions)
Sets the default values fro the variables as well as generating the labyritnh array with borders...
Definition: labyrinth.cpp:17
bool setDirection(int dir)
The method to set the direction where the snake is heading.
Definition: snake.cpp:294
Point getCoords()
Get the ball coords without giving the direct access.
Definition: ball.cpp:37
bool init(Point begin, short direction, int length)
Initializes snake as an entity without ancoring it anywhere.
Definition: snake.cpp:20
const int MVLEFT
snake&#39;s head is moving left
Definition: snake.h:60
shared functions for testing
Point gameFieldSize
Definition: game.cpp:17
bool setCoords(Point p)
Sets coordinates of the ball to given if they are correct.
Definition: ball.cpp:47
bool rmPoint(Point p)
Remove Point from the labyrinth.
Definition: labyrinth.cpp:286
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
const int MVUP
snake&#39;s head is moving up
Definition: snake.h:64
Describes the Snake entity of the game.
Definition: snake.h:79
bool addPoint(Point p)
Adding Point to the labyrinth with the check of correction.
Definition: labyrinth.cpp:271
TEST(Snake_move, test1)
method runs with normal parameters and no collisions
Definition: Snake_move.h:20
bool addSnake(Snake *snake)
Adding snake to the laburinth and getting the host snake(firest added snake)
Definition: labyrinth.cpp:110
const int MVRIGHT
snake&#39;s head is moving right
Definition: snake.h:52
Coordinates and style of the cell in console window.
Definition: common.h:52
Definitions and prototypes for Snake class.
const int MVDOWN
snake&#39;s head is moving down
Definition: snake.h:56