-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.cpp
305 lines (259 loc) · 6.26 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "Board.h"
#include <conio.h>
#include <Windows.h>
Board::Board()
{
topB = new TopBoard();
setBoard();
}
void Board::printFrame()
{
for (int i = 1;i <= COLS; i++) // print frame top & bottom
{
gotoxy(GameFrame::LEFT_F + i, GameFrame::TOP_F);
cout << "*" << endl;
gotoxy(GameFrame::LEFT_F + i, GameFrame::BOTTOM_F);
cout << "*" << endl;
}
for (int i = 0;i <= ROWS+1; i++) // print frame left & right
{
gotoxy(GameFrame::LEFT_F, GameFrame::TOP_F + i);
cout << "|" << endl;
gotoxy(GameFrame::RIGHT_F, GameFrame::TOP_F + i);
cout << "|" << endl;
}
}
void Board::setBoard()
{
printMenu();
printFrame();
topB->printTopBoard();
for (int i =0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (i < 2) {
boardGame[i][j].setPoint(j, i, false, -2, ' ');
turnOffPoint(j + GameZone::LEFT, i + GameZone::TOP);
}
else {
boardGame[i][j].setPoint(j + GameZone::LEFT, i + GameZone::TOP, false, -1, ' ');
turnOffPoint(j + GameZone::LEFT, i + GameZone::TOP);
}
}
}
}
void Board::cleanGameOver()
{
gotoxy(Board::LEFT_F + 2, Board::BOTTOM_F + 1);
cout << " " << endl;
gotoxy(Board::LEFT_F - 2, Board::BOTTOM_F + 3);
cout << " " << endl;
gotoxy(Board::LEFT_F - 2, Board::BOTTOM_F + 5);
cout << " " << endl;
topB->resetTopBoard();
}
int Board::getScore() {
return topB->getScore();
}
//check if the line is full
bool Board::isFullLine (int curLine , bool & isJokerInLine)
{
isJokerInLine = false;
//check FULL line
for (const Point&p : boardGame[curLine - Board::TOP + 3])
{
if (p.getSign() == Sign::J)
isJokerInLine = true;
if (!(p.isBusy()))
return false;
}
// marker the line before crush
for (Point&p : boardGame[curLine - Board::TOP + 3])
{
turnOnPoint(p.getx(), p.gety(),-3,'x');
Sleep(20);
}
// turn OFF line
for (Point&p : boardGame[curLine - Board::TOP + 3])
{
turnOffPoint(p.getx(), p.gety());
}
return true;
}
bool Board::isEmptyLine(int curLine)
{
for (const Point&p : boardGame[curLine - Board::TOP + 3])
{
if ((p.isBusy()))
return false;
}
return true;
}
// clean all the full lines
bool Board::cleanLines(int startLine)
{
int inARow = 0;
bool res = false;
bool checkJ;
for (int i = 0; i < 6 && (startLine - i) >= 5; i++)
{
bool fullLine = isFullLine(startLine - i, checkJ);
if ( fullLine == true && checkJ == false)
{
res = true;
inARow++;
Sleep(200);
}
else if (fullLine == true && checkJ == true)
{
topB->updateScore(SCORE::JOKER_LINE);
res = true;
inARow = 0;
Sleep(200);
}
else if (inARow != 0)
{
if (inARow == 1)
topB->updateScore(SCORE::SINGLE_LINE);
else if (inARow == 2)
topB->updateScore(SCORE::DOUBLE_LINE);
else
topB->updateScore(SCORE::TRIPLE_LINE);
inARow = 0;
}
}
return res;
}
//BOMB explode
int Board::blowUpSquare(int x, int y)
{
int scoreCounter = 0;
for (int i = 0; i < 3; i++) //blow up 3x3 square.
{
HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(color, 12);
// count score
if ((!(*this).isValid(x - 1 + i, y - 1)) && checkInGameZone(x - 1 + i, y - 1))
{
turnOnPoint(x - 1 + i, y - 1, -3, '*');
scoreCounter -= 50;
}
if ((!(*this).isValid(x - 1 + i, y)) && checkInGameZone(x - 1 + i, y))
{
turnOnPoint(x - 1 + i, y, -3, '*');
scoreCounter -= 50;
}
if ((!(*this).isValid(x - 1 + i, y + 1)) && checkInGameZone(x - 1 + i, y + 1))
{
turnOnPoint(x - 1 + i, y +1, -3, '*');
scoreCounter -= 50;
}
Sleep(150);
// turn OFF points relevant
(*this).turnOffPoint(x - 1 + i, y-1);
(*this).turnOffPoint(x - 1 + i, y);
(*this).turnOffPoint(x - 1 + i, y+1);
}
return scoreCounter; // return the score to be reduced
}
bool Board::updateBoard()
{
Point temp[4];
int tempSize;
int row = Board::BOTTOM - 1;
bool endClean = true;
while (row != Board::TOP )
{
for (Point&p : boardGame[row - Board::TOP + 3])
{
if (p.isBusy() == true && isValid(p.getx(), p.gety() + 1))
{
createSerialShape(p.getSerialNumber(), row, tempSize, temp);
moveShape(temp, tempSize);
}
}
row--;
}
return endClean;
}
void Board::updateScoreBoard(int addScore)
{
topB->updateScore(addScore);
topB->printTopBoard();
}
void Board::updateNumOfShapesBoard()
{
topB->updateNumOfShapes();
topB->printTopBoard();
}
Point * Board::createSerialShape(int serial, int row, int & shapeSize, Point * res)
{
shapeSize = 0;
for (int i = 0; i<4; i++)
{
for (Point&p : boardGame[row - i - Board::TOP + 3])
{
if (serial == p.getSerialNumber())
{
res[shapeSize].setPoint(p.getx(),p.gety(),true,p.getSerialNumber(),p.getSign());
shapeSize++;
}
}
}
return res;
}
void Board::moveShape(Point * arr, int size)
{
bool flag = false;
bool flag1 = true;
for (int i = 0;i < size; i++)
{
if (isValid(arr[i].getx(), arr[i].gety() + 1) && arr[i].gety()+1 <= BOTTOM)
flag = true;
else
{
flag1 = false;
for (int j = 0; j < size; j++)
{
if (j != i)
{
if ((arr[i].getx() == arr[j].getx()) && ((arr[i].gety() + 1) == arr[j].gety()))
flag1 = true;
}
}
if (flag1 == false)
break;
}
}
if (flag==true && flag1==true)
{
hardDownShape(arr, size);
for (int i = 0; i < size; i++)
arr[i].setPoint(arr[i].getx(), arr[i].gety() + 1, true, arr[i].getSerialNumber(), arr[i].getSign());
moveShape(arr, size);
}
}
void Board::hardDownShape(Point * arr , int size)
{
for (int i = 0; i < size; i++)
{
turnOffPoint(arr[i].getx(), arr[i].gety());
turnOnPoint(arr[i].getx(), arr[i].gety() + 1, arr[i].getSerialNumber(), arr[i].getSign());
}
}
void Board::printMenu()
{
gotoxy(Board::RIGHT_F + 5, (Board::BOTTOM_F + Board::TOP_F) / 2 - 5);
cout << "Your highest score is :";
gotoxy(Board::RIGHT_F + 5, (Board::BOTTOM_F + Board::TOP_F) / 2);
cout << "Press (1) to START" << endl;
gotoxy(Board::RIGHT_F + 5, ((Board::BOTTOM_F + Board::TOP_F) / 2) + 1);
cout << "Press (2) to PAUSE/RESUME" << endl;
gotoxy(Board::RIGHT_F + 5, ((Board::BOTTOM_F + Board::TOP_F) / 2) + 2);
cout << "Press (3) to FAST SPEED" << endl;
gotoxy(Board::RIGHT_F + 5, ((Board::BOTTOM_F + Board::TOP_F) / 2) + 3);
cout << "Press (4) to NORMAL SPEED" << endl;
gotoxy(Board::RIGHT_F + 5, ((Board::BOTTOM_F + Board::TOP_F) / 2) + 4);
cout << "Press (9) to EXIT" << endl;
}