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