Snoke
game.cpp
Go to the documentation of this file.
1 
11 #include "game.h"
12 
18 
23 void resizeHandler(int sig)
24 {
25  endwin();
26  sleepHandler();
27  refresh();
28  addSleep();
29 }
30 
38 bool Game::init(int size, int speed, int loops)
39 {
40  if(size > 10)
41  {
42  gameFieldSize.x = size;
43  gameFieldSize.y = size/2;
44  this->labyrinth.setLabyrinth(gameFieldSize);
45  this->setSpeed(speed);
46  this->setLoops(loops);
47 
48  return true;
49  }
50  return false;
51 }
52 
58 {
59  this->loops = loops > 0 ? loops : -1;
60 }
61 
62 
63 
68 int Game::run()
69 {
70  signal(SIGWINCH, resizeHandler);
71 
72  /*
73  * Initializing snake by giving it the starting position,
74  * direction of growth and starting length
75  */
76  Point startPos;
77  startPos.x = 1;
78  startPos.y = 1;
79  int dir = 1;
80  int len = 5;
81  if(!initSnake(startPos, dir, len))
82  {
83  return 1;
84  }
85 
86  /*
87  * Initializing the Ball and generating it on the field
88  */
90 
91  /*
92  * displaying the starting state of labyrinth(forcing to display it Full if possible)
93  */
95 
96  bool flag = false;
97  while (!flag && loops != 0)
98  {
99  int command = getch();
100  switch (command)
101  {
102  case ERR:
103  {
104  break;
105  }
106  case KEY_UP:
107  {
109  break;
110  }
111  case KEY_DOWN:
112  {
114  break;
115  }
116  case KEY_LEFT:
117  {
119  break;
120  }
121  case KEY_RIGHT:
122  {
124  break;
125  }
126  case 'q':
127  {
128  flag = true;
129  break;
130  }
131  default:
132  {
133  break;
134  }
135  }
136  if (flag)
137  {
138  break;
139  }
141 
142  /*
143  * Setting the flag for the next iteration check
144  * Moving the snake and getting the Points of a
145  * labyrinth which are to be changed in change array
146  */
147  flag = snake.move(&labyrinth) == 1;
148 
149  /*
150  * Updating labyrinth with change array Points
151  * and displaying/deleting added/removed Points
152  */
154 
155  mSleep(speed);
156  if(loops > 0)
157  {
158  loops--;
159  }
160  }
161  return 0;
162 }
163 
171 bool Game::initSnake(Point begin, short dir, int length)
172 {
173  bool initFlag = snake.init(begin, dir, length);
174  bool addFlag = initFlag ? labyrinth.addSnake(&snake) : false;
175  return initFlag && addFlag;
176 }
177 
184 {
185  if(speed > 100)
186  {
187  if(speed < 1000)
188  {
189  this->speed = speed;
190  }
191  else
192  {
193  this->speed = 1000;
194  }
195  }
196  else
197  {
198  this->speed = 100;
199  }
200 }
void setSpeed(int sp)
The method to change the game speed without giving the direct access and some error handling...
Definition: game.cpp:183
void mSleep(int time)
Cross-platform sleep function cover.
Definition: common.cpp:49
short x
Definition: common.h:55
void initQueue()
Initializes change array, by resetting it.
Definition: change.cpp:17
bool init(int size=20, int sp=100, int loops=-1)
Initializes game.
Definition: game.cpp:38
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
Definitions and prototypes for Game class.
int speed
Definition: game.h:33
void resizeHandler(int sig)
Definition: game.cpp:23
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
Labyrinth labyrinth
Definition: game.h:37
void sleepHandler()
setting new values for sleep function on signal interrupt
Definition: common.cpp:78
int run()
Main game loop.
Definition: game.cpp:68
bool init(Point begin, short direction, int length)
Initializes snake as an entity without ancoring it anywhere.
Definition: snake.cpp:20
const int DISPFULL
the whole labyrinth can fit and was redrawed
Definition: labyrinth.h:44
bool initSnake(Point begin, short dir, int length)
Snake initialization with labyrinth updating.
Definition: game.cpp:171
const int MVLEFT
snake&#39;s head is moving left
Definition: snake.h:60
Change change
Definition: labyrinth.h:82
void setLoops(int loops)
sets loops with some error holding
Definition: game.cpp:57
short y
Definition: common.h:56
const int MVUP
snake&#39;s head is moving up
Definition: snake.h:64
Point gameFieldSize
Definition: game.cpp:17
bool addSnake(Snake *snake)
Adding snake to the laburinth and getting the host snake(firest added snake)
Definition: labyrinth.cpp:110
void addSleep()
For unix-based systems interrupted sleep must be continued.
Definition: common.cpp:90
int loops
Definition: game.h:34
const int MVRIGHT
snake&#39;s head is moving right
Definition: snake.h:52
void displayHandler(int displayMethod=-1)
A method which decides what dispaly method will be used in each situation.
Definition: labyrinth.cpp:152
Snake snake
Definition: game.h:35
Coordinates and style of the cell in console window.
Definition: common.h:52
const int MVDOWN
snake&#39;s head is moving down
Definition: snake.h:56