nsnake
Classic snake game for the terminal
Board.cpp
1 #include <Entities/Board.hpp>
2 #include <Entities/Player.hpp>
3 #include <Engine/EngineGlobals.hpp>
4 #include <Engine/Helpers/Utils.hpp>
5 
6 int Board::small_width = 40;
7 int Board::small_height = 10;
8 
9 int Board::medium_width = 55;
10 int Board::medium_height = 14;
11 
12 int Board::large_width = 78;
13 int Board::large_height = 21;
14 
15 Board::Board(int width, int height, Style style):
16  style(style),
17  start_x(BOARD_DEFAULT_PLAYER_X),
18  start_y(BOARD_DEFAULT_PLAYER_Y)
19 {
20  this->board = new Array2D<bool>(width, height);
21 
22  this->clear();
23 }
24 Board::~Board()
25 {
26  delete this->board;
27 }
28 bool Board::isBorder(int x, int y)
29 {
30  if ((x == 0) || (x == (int)this->board->width() - 1) ||
31  (y == 0) || (y == (int)this->board->height() - 1))
32  return true;
33 
34  return false;
35 }
36 bool Board::isWall(int x, int y)
37 {
38  if (isBorder(x, y))
39  {
40  // If we can teleport, the borders are
41  // not collidable - we'll just walk through them.
42  return (this->style == Board::SOLID);
43  }
44 
45  return (this->board->at(x, y));
46 }
47 int Board::getW()
48 {
49  return this->board->width();
50 }
51 int Board::getH()
52 {
53  return this->board->height();
54 }
55 void Board::draw(Window* win)
56 {
57  int teleport_appearance = '\'';
58  int solid_appearance = ((EngineGlobals::Screen::fancy_borders) ?
59  ACS_CKBOARD :
60  '#');
61 
62  for (size_t i = 0; i < (this->board->width()); i++)
63  {
64  for (size_t j = 0; j < (this->board->height()); j++)
65  {
66  if (this->isBorder(i, j))
67  {
68  win->printChar(((this->style == Board::TELEPORT) ?
69  teleport_appearance :
70  solid_appearance),
71  i, j);
72  continue;
73  }
74  else if (this->isWall(i, j))
75  win->printChar(solid_appearance,
76  i, j);
77  }
78  }
79 }
80 void Board::randomlyFillExceptBy(int x, int y)
81 {
82  for (size_t i = 0; i < (this->board->width()); i++) {
83  for (size_t j = 0; j < (this->board->height()); j++) {
84 
85  // The frequency of random Walls (cute PI)
86  if (Utils::Random::booleanWithChance(0.031415923))
87  this->board->set(i, j, true);
88  }
89  }
90 
91  // Clearing some space for #x and #y
92  for (int i = -2; i != 7; i++)
93  this->board->set(x + i, y, false);
94 }
95 void Board::teleport(Player* player)
96 {
97  // If we don't teleport,
98  // at least keep it on the current position
99  int newx = player->getX();
100  int newy = player->getY();
101 
102  // Where we'll place the player
103  int left = 1;
104  int right = this->board->width() - 2;
105 
106  if (player->getX() < left)
107  {
108  newx = right;
109  }
110  else if (player->getX() > right)
111  {
112  newx = left;
113  }
114 
115  int top = 1;
116  int bottom = this->board->height() - 2;
117 
118  if (player->getY() < top)
119  {
120  newy = bottom;
121  }
122  else if (player->getY() > bottom)
123  {
124  newy = top;
125  }
126 
127  player->moveTo(newx, newy);
128 }
130 {
131  // Making it empty
132  for (size_t i = 0; i < this->board->width(); i++)
133  for (size_t j = 0; j < this->board->height(); j++)
134  this->board->set(i, j, false);
135 }
136 void Board::setBoard(std::vector<std::vector<bool> >& newBoard)
137 {
138  // Making it empty
139  for (size_t i = 0; i < this->board->width(); i++)
140  for (size_t j = 0; j < this->board->height(); j++)
141  this->board->set(i, j, newBoard[j][i]);
142 }
143 
144 int Board::getStartX()
145 {
146  return this->start_x;
147 }
148 int Board::getStartY()
149 {
150  return this->start_y;
151 }
152 
153 void Board::setStartX(int x)
154 {
155  this->start_x = x;
156 }
157 void Board::setStartY(int y)
158 {
159  this->start_y = y;
160 }
161 
162 void Board::setMetadata(std::string name, std::string value)
163 {
164  this->metadata[name] = value;
165 }
166 std::string Board::getMetadata(std::string name)
167 {
168  if (! this->hasMetadata(name))
169  return "";
170 
171  return this->metadata[name];
172 }
173 bool Board::hasMetadata(std::string name)
174 {
175  return (this->metadata.find(name) != this->metadata.end());
176 }
177 void Board::scrollLeft()
178 {
179  // Going line by line from top to bottom
180  for (size_t j = 0; j < this->board->height() - 1; j++)
181  {
182  // Get first left element from this line
183  bool tmp = this->board->at(1, j);
184 
185  // Shifting all elements one block left
186  for (size_t i = 0; i < (this->board->width() - 1); i++)
187  this->board->set(i, j, this->board->at(i + 1, j));
188 
189  // Putting the first element on the last place
190  this->board->set(this->board->width() - 2, j, tmp);
191  }
192 }
193 void Board::scrollRight()
194 {
195  // Going line by line from top to bottom
196  for (size_t j = 0; j < this->board->height() - 1; j++)
197  {
198  // Get first right element from this line
199  bool tmp = this->board->at(this->board->width() - 2, j);
200 
201  // Shifting all elements one block right
202  for (size_t i = (this->board->width() - 1); i > 0; i--)
203  this->board->set(i, j, this->board->at(i - 1, j));
204 
205  // Putting the first element on the last place
206  this->board->set(1, j, tmp);
207  }
208 }
209 void Board::scrollUp()
210 {
211  // Going line by line from left to right
212  for (size_t j = 0; j < this->board->width() - 1; j++)
213  {
214  // Get first top element from this line
215  bool tmp = this->board->at(j, 1);
216 
217  // Shifting all elements one block up
218  for (size_t i = 0; i < (this->board->height() - 1); i++)
219  this->board->set(j, i, this->board->at(j, i + 1));
220 
221  // Putting the first element on the last place
222  this->board->set(j, this->board->height() - 2, tmp);
223  }
224 }
225 void Board::scrollDown()
226 {
227  // Going line by line from left to right
228  for (size_t j = 0; j < this->board->width() - 1; j++)
229  {
230  // Get first bottom element from this line
231  bool tmp = this->board->at(j, this->board->height() - 2);
232 
233  // Shifting all elements one block down
234  for (size_t i = this->board->height() - 2; i > 0; i--)
235  this->board->set(j, i, this->board->at(j, i - 1));
236 
237  // Putting the first element on the last place
238  this->board->set(j, 1, tmp);
239  }
240 }
241 
void randomlyFillExceptBy(int x, int y)
Places random walls all over the Board except by #x and #y, allowing the Player to move a little bit ...
Definition: Board.cpp:80
bool hasMetadata(std::string name)
Tells if this level has a specific information attached.
Definition: Board.cpp:173
std::string getMetadata(std::string name)
Gets a meta information from this level.
Definition: Board.cpp:166
Style
If the player will teleport when reaching the Board's limits or not.
Definition: Board.hpp:46
Style style
Tells if the player will teleport when reaching the Board's limits or not.
Definition: Board.hpp:90
Board(int width, int height, Style style)
Creates a new Board.
Definition: Board.cpp:15
void setMetadata(std::string name, std::string value)
Sets a meta information from this level.
Definition: Board.cpp:162
bool isWall(int x, int y)
Tells if there's a wall at #x #y.
Definition: Board.cpp:36
void setBoard(std::vector< std::vector< bool > > &newBoard)
Sets the whole level content.
Definition: Board.cpp:136
void teleport(Player *player)
Makes the Player teleport if it's on a border.
Definition: Board.cpp:95
void clear()
Makes the whole level empty.
Definition: Board.cpp:129
int getX()
Returns the head's x position.
Definition: Player.cpp:26
int getY()
Returns the head's y position.
Definition: Player.cpp:30