Snoke
labyrinth.cpp
Go to the documentation of this file.
1 
11 #include "labyrinth.h"
18 {
19  if(fieldSize.x > 4 && fieldSize.y > 4)
20  {
21  gameFieldSize = fieldSize;
22  /*
23  * Creating the labyrinth and setting up the borders
24  */
25  labyrinth = new char* [fieldSize.y];
26  for(int i = 0; i < fieldSize.y; i++)
27  {
28  labyrinth[i] = new char [fieldSize.x];
29  for(int j = 0; j < fieldSize.x; j++)
30  {
31  labyrinth[i][j] = ' ';
32  }
33  }
34  for(int i = 0; i < fieldSize.x; i++)
35  {
36  labyrinth[0][i] = '-';
37  labyrinth[fieldSize.y - 1][i] = '-';
38  }
39  for(int i = 0; i < fieldSize.y; i++)
40  {
41  labyrinth[i][0] = '|';
42  labyrinth[i][fieldSize.x - 1] = '|';
43  }
44 
45  /*
46  * Setting the default values for the Labyrinth properties
47  */
48  Point leftTop;
49  Point rightBot;
50  leftTop.x = 0;
51  leftTop.y = 0;
52  rightBot = fieldSize;
53  this->start = leftTop;
54  this->end = rightBot;
55  this->prevStart = leftTop;
56  this->prevEnd = rightBot;
57  this->dispHandler.labyrinth = this;
59  this->snake = NULL;
60 
61  change.initQueue();
62  return true;
63  }
64  return false;
65 }
66 
72 {
73  this->ball.init(gameFieldSize);
74  bool flag = this->generateBall();
75  return flag;
76 }
77 
84 {
85  this->change.rmPoint(this->ball.getCoords());
86  Point chance;
87  /*
88  * Generate numbers until u get a free spot
89  */
90  while (1)
91  {
94  if ( (this->isFree(chance) == 1 || this->change.inRmQueue(chance))
95  && !this->change.inAddQueue(chance))
96  {
97  break;
98  }
99  }
100  ball.setCoords(chance);
101  this->change.addPoint(this->ball.getCoords());
102  return true;
103 }
104 
111 {
112  bool brFlag = false;
113  std::list<Point> currBody;
114  aSnake->getCoords(&currBody);
115  auto it = currBody.begin();
116  for(; it != currBody.end(); ++it)
117  {
118  if(isFree(*it) == 1)
119  {
120  addPoint(*it);
121  }
122  else
123  {
124  brFlag = true;
125  break;
126  }
127  }
128 
129  if(brFlag)
130  {
131  for(; it != currBody.begin(); --it)
132  {
133  if(isFree(*it) == 0)
134  {
135  rmPoint(*it);
136  }
137  }
138  return false;
139  }
140 
141  if(this->snake == NULL){
142  this->snake = aSnake;
143  }
144 
145  return true;
146 }
147 
152 void Labyrinth::displayHandler(int displayMethod)
153 {
154  /*
155  * Writing changes to labyrinth
156  */
157  this->updateLabyrinth();
158 
159  /*
160  * Getting start and end values for the displaying of the labyrinth
161  */
162  this->sizeHandler();
163 
164  this->dispHandler.displayHandler(displayMethod);
165 }
166 
172 {
173  Point head = this->snake->getHeadCoords();
174  Point currentSize = getConsoleSize();
175 
176  /*
177  * Deciding how many columns can we display and where should we start
178  */
179  if(currentSize.y < gameFieldSize.y)
180  {
181  start.y = head.y - currentSize.y / 2;
182  end.y = head.y + currentSize.y / 2 + currentSize.y % 2;
183  if(start.y < 0)
184  {
185  start.y = 0;
186  end.y = currentSize.y;
187  }
188  if(end.y >= gameFieldSize.y)
189  {
190  end.y = gameFieldSize.y;
191  if(start.y != 0)
192  {
193  start.y = gameFieldSize.y - currentSize.y;
194  }
195  }
196  } else
197  {
198  start.y = 0;
199  end.y = gameFieldSize.y;
200  }
201 
202  /*
203  * Deciding how many rows can we display and where should we start
204  */
205  if(currentSize.x < gameFieldSize.x)
206  {
207  start.x = head.x - currentSize.x / 2;
208  end.x = head.x + currentSize.x / 2 + currentSize.x % 2;
209  if(start.x < 0)
210  {
211  start.x = 0;
212  end.x = currentSize.x;
213  }
214  if(end.x >= gameFieldSize.x)
215  {
216  end.x = gameFieldSize.x;
217  if(start.x != 0)
218  {
219  start.x = gameFieldSize.x - currentSize.x;
220  }
221  }
222  } else
223  {
224  start.x = 0;
225  end.x = gameFieldSize.x;
226  }
227 }
228 
233 {
234  std::deque<Point> cpChange;
235  change.getRmQueue(&cpChange);
236 
237  for(auto it = cpChange.begin(); it != cpChange.end(); ++it)
238  {
239  rmPoint(*it);
240  mvaddch((*it).y, (*it).x, 'F');
241  }
242  change.getAddQueue(&cpChange);
243  for(auto it = cpChange.begin(); it != cpChange.end(); ++it)
244  {
245  addPoint(*it);
246  }
247 }
248 
255 {
256  if(p.x < gameFieldSize.x && p.y < gameFieldSize.y && p.x > -1 && p.y > -1)
257  {
258  return (this->labyrinth[p.y][p.x] == ' ');
259  }
260  else
261  {
262  return -1;
263  }
264 }
265 
272 {
273  if(this->isFree(p) == 1)
274  {
275  labyrinth[p.y][p.x] = p.style.letter;
276  return true;
277  }
278  return false;
279 }
280 
287 {
288  labyrinth[p.y][p.x] = ' ';
289  return true;
290 }
291 
297 bool Labyrinth::save(char name[MAXLINE])
298 {
299  FILE* file;
300  char strout[MAXLINE];
301  file = fopen(name, "w");
302  if(file)
303  {
304  /*
305  * Printing the dimensions of the gamefield
306  */
307  fprintf(file, "%d %d\n", gameFieldSize.x, gameFieldSize.y);
308 
309  /*
310  * Printing the lines of labyrinth to the file one by one
311  */
312  for(int j = 0; j < gameFieldSize.y; j++)
313  {
314  fputs(labyrinth[j], file);
315  fputc('\n', file);
316  }
317  fclose(file);
318 
319  /*
320  * Putting a message of successful writing
321  */
322  snprintf(strout, sizeof(strout), "Succesfully saved to %s", name);
323  mvaddstr(gameFieldSize.y / 2, 5, strout);
324  return true;
325  }
326  /*
327  * Putting a message of an occured error
328  */
329  snprintf(strout, sizeof(strout), "Couldn't save to %s", name);
330  mvaddstr(gameFieldSize.y / 2, 5, strout);
331  return false;
332 }
333 
339 bool Labyrinth::load(char name[MAXLINE])
340 {
341  FILE* file;
342  Point scannedSize;
343  char strout[MAXLINE];
344  file = fopen(name, "r");
345  if(file)
346  {
347  /*
348  * Getting the dimensions of the labyrinth and creating a temporary labyrinth
349  */
350  fscanf(file, "%hd %hd", &scannedSize.x, &scannedSize.y);
351  char tmpLab[scannedSize.y][scannedSize.x];
352  for(int i = 0; i < scannedSize.x; i++)
353  {
354  for(int j = 0; j < scannedSize.y; j++)
355  {
356  tmpLab[j][i] = ' ';
357  }
358  }
359 
360  /*
361  * Getting lines of the labyrinth
362  */
363  for(int j = 0; j < scannedSize.y; j++)
364  {
365  fgets(tmpLab[j], scannedSize.x, file);
366  }
367  fclose(file);
368 
369  /*
370  * Setting the new gameFieldSize and copying the borders and obstacles from the temporary labyrinth
371  */
372  gameFieldSize = scannedSize;
373  for(int i = 0; i < gameFieldSize.x; i++)
374  {
375  for(int j = 0; j < gameFieldSize.y; j++)
376  {
377  if(reserved.find(tmpLab[j][i]) != std::string::npos)
378  {
379  labyrinth[j][i] = tmpLab[j][i];
380  }
381  }
382  }
383  /*
384  * Putting a message of successful loading
385  */
386  snprintf(strout, sizeof(strout), "Succesfully loaded from %s", name);
387  mvaddstr(gameFieldSize.y / 2, 5, strout);
388  return 1;
389  }
390  /*
391  * Putting message of occured error
392  */
393  snprintf(strout, sizeof(strout), "Couldn't load from %s", name);
394  mvaddstr(gameFieldSize.y / 2, 5, strout);
395  return 0;
396 }
DisplayHandler dispHandler
Definition: labyrinth.h:78
std::string reserved
Definition: labyrinth.h:70
void getRmQueue(std::deque< Point > *cpChange)
method to get a copy of removal array
Definition: change.cpp:98
bool addPoint(Point p)
adds Point to addition array if it fits the criteria
Definition: change.cpp:28
Labyrinth * labyrinth
Definition: labyrinth.h:30
short x
Definition: common.h:55
void initQueue()
Initializes change array, by resetting it.
Definition: change.cpp:17
Point end
Definition: labyrinth.h:73
Definitions and prototypes for Labyrinth class.
bool initBall()
Ball initialization with labyrinth updating.
Definition: labyrinth.cpp:71
bool rmPoint(Point p)
adds Point to removal array if it fits the criteria
Definition: change.cpp:43
std::mt19937 * getRandomEngine()
Definition: ball.cpp:58
const int MAXLINE
Definition: common.h:28
Ball ball
Definition: labyrinth.h:83
void updateLabyrinth()
Updating the labyrinth(changing the values of some Points)
Definition: labyrinth.cpp:232
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
Point start
Definition: labyrinth.h:72
bool generateBall()
Generate the Ball.
Definition: labyrinth.cpp:83
void sizeHandler()
A method, which sets start and end values depending on the position of local_player&#39;s snake head and ...
Definition: labyrinth.cpp:171
bool setLabyrinth(Point dimensions)
Sets the default values fro the variables as well as generating the labyritnh array with borders...
Definition: labyrinth.cpp:17
void getAddQueue(std::deque< Point > *cpChange)
method to get a copy of addition array
Definition: change.cpp:89
const int DISPPART
the whole labyrinth doesn&#39;t fit, so we are displaying it partialy
Definition: labyrinth.h:48
Point getCoords()
Get the ball coords without giving the direct access.
Definition: ball.cpp:37
bool inAddQueue(Point p)
Check if Points is in the addition queue.
Definition: change.cpp:57
Point gameFieldSize
Definition: labyrinth.h:76
bool save(char name[MAXLINE])
Method to save labyritnh(borders and obstacles, defined in reserved string) to the file...
Definition: labyrinth.cpp:297
int prevDisplayMethod
Definition: labyrinth.h:31
bool load(char name[MAXLINE])
Mehod to load labyrinth from the file.
Definition: labyrinth.cpp:339
bool init(Point dim)
Initialization.
Definition: ball.cpp:18
char ** labyrinth
Definition: labyrinth.h:69
Point getConsoleSize()
Gets size of current console screen in symdols.
Definition: common.cpp:100
std::uniform_int_distribution< int > distributionX
Definition: ball.h:40
bool inRmQueue(Point p)
Check if the Point is in the remove queue.
Definition: change.cpp:73
Point prevEnd
Definition: labyrinth.h:75
PointStyle style
Definition: common.h:57
Change change
Definition: labyrinth.h:82
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
char letter
Definition: common.h:40
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
bool addSnake(Snake *snake)
Adding snake to the laburinth and getting the host snake(firest added snake)
Definition: labyrinth.cpp:110
std::uniform_int_distribution< int > distributionY
Definition: ball.h:41
Snake * snake
Definition: labyrinth.h:71
void displayHandler(int displayMethod)
handler for choosing the approriate display method in the current environment
Point prevStart
Definition: labyrinth.h:74
void displayHandler(int displayMethod=-1)
A method which decides what dispaly method will be used in each situation.
Definition: labyrinth.cpp:152
Coordinates and style of the cell in console window.
Definition: common.h:52
short isFree(Point p)
Mark of if the asked Point is free.
Definition: labyrinth.cpp:254