-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
226 lines (201 loc) · 4.2 KB
/
Board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "Board.h"
#include "AssetManager.h"
Board::Board(int xPos, int yPos) : Board()
{
this->xPos = xPos;
this->yPos = yPos;
}
Board::Board()
{
cellImage.setTexture(AssetManager::getInstance()->getTexture("tiles"));
}
Board::~Board()
{
}
void Board::render(sf::RenderWindow& window)
{
using namespace sf;
RectangleShape frame(Vector2f(boardSquareSize * boardWidth, boardSquareSize * boardHeight));
frame.setOutlineThickness(10);
frame.setOutlineColor(Color(255, 255, 255, 150));
frame.setPosition(xPos, yPos);
frame.setFillColor(Color(0,0,0, 100));
window.draw(frame);
for (int i = 0; i < boardHeight; i++)
{
for (int j = 0; j < boardWidth; j++)
{
RectangleShape rect(Vector2f(boardSquareSize, boardSquareSize));
rect.setFillColor(Color(0, 0, 0, 150));
rect.setPosition(xPos + boardSquareSize * j, yPos + boardSquareSize * i);
rect.setOutlineColor(Color(50, 50, 50));
rect.setOutlineThickness(5);
window.draw(rect);
if (board[i][j] % 9 != 0)
{
cellImage.setTextureRect(IntRect((board[i][j] % 9 - 1) * 45, 0, 45, 45));
cellImage.setScale(Vector2f(3, 3));
cellImage.setPosition(xPos + boardSquareSize * j, yPos + boardSquareSize * i);
window.draw(cellImage);
}
}
}
}
ClearingInfo Board::clearLines()
{
int linesCleared = 0;
bool isPC = true;
for (int i = 0; i < boardHeight; i++)
{
bool lineIsFilled = true;
bool linePC = true;
// check if line is filled
for (int j = 0; j < boardWidth; j++)
{
if (board[i][j] == 0)
{
lineIsFilled = false;
}
// in a line, if tiles switch from filled and empty -> not clearing -> not pc
if (j != 0 && board[i][j] != board[i][j - 1] && (board[i][j] == 0 || board[i][j - 1] == 0))
{
isPC = false;
break;
}
}
// if line is filled, clear
if (lineIsFilled)
{
linesCleared++;
for (int k = i; k >= 0; k--)
{
for (int j = 0; j < boardWidth; j++)
{
if (k == 0)
{
board[k][j] = 0;
}
else
{
board[k][j] = board[k - 1][j];
}
}
}
}
}
struct ClearingInfo result;
result.isPC = isPC;
result.linesCleared = linesCleared;
return result;
}
void Board::setBoard(std::array<std::array<short, boardHeight>, boardWidth> board)
{
this->board = board;
}
void Board::clearBoard()
{
for (int i = 0; i < boardHeight; i++)
{
for (int j = 0; j < boardWidth; j++)
{
board[i][j] = 0;
}
}
}
void Board::enforceGravity()
{
// check each collumn
for (int j = 0; j < boardWidth; j++)
{
// while the current cell is empty, shift all the cells above down
for (int k = boardHeight - 1; k >= 0; k--)
{
int linesShift = 0;
// count number of empty cells right above the current cell
for (int t = k; t > 0; t--)
{
if (board[t][j] > 0)
{
break;
}
linesShift++;
}
for (int t = k; linesShift > 0 && t - linesShift >= 0; t--)
{
board[t][j] = board[t - linesShift][j];
board[t - linesShift][j] = 0;
}
}
}
}
// TODO: check if this return a copy or reference
std::array<std::array<short, boardWidth>, boardHeight> Board::getBoard()
{
return board;
}
void Board::setCell(int x, int y, int value)
{
board[x][y] = value;
}
int Board::getCell(int x, int y)
{
return board[x][y];
}
bool Board::createGarbageLine(int holePos)
{
// move up the board
for (int i = 0; i < boardHeight - 1; i++)
{
for (int k = 0; k < boardWidth; k++)
{
// if the top has block, can't create a garbage line
if (i == 0 && board[i][k] != 0)
{
return false;
}
else if (i == 0)
{
continue;
}
board[i][k] = board[i + 1][k];
}
}
// create garbage line
for (int k = 0; k < boardWidth; k++)
{
if (k == holePos)
{
board[boardHeight - 1][k] = 0;
}
else
{
board[boardHeight - 1][k] = 8;
}
}
return true;
}
void Board::print()
{
using namespace std;
cout << "{{" << std::endl;
for (int i = 0; i < board.size(); i++)
{
cout << "\t{{";
for (int j = 0; j < board[0].size(); j++)
{
string divider = j == board[0].size() - 1 ? " " : ", ";
cout << board[i][j] << divider;
}
string divider = i == board.size() - 1 ? " " : ", ";
cout << "}}" << divider << std::endl;
}
cout << "}}" << std::endl;
}
int Board::getXPos()
{
return xPos;
}
int Board::getYPos()
{
return yPos;
}