-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
275 lines (231 loc) · 7.76 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
/*********************************************************************************************************************/
/*!
@file Board.cpp
@author sbmrgd
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, sbmrgd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This entire game is based on a tutorial by Javier López López
Link: http://javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/
*/
/*********************************************************************************************************************/
#include "Board.h"
/*
==================
Init
==================
*/
Board::Board(int pScreenHeight)
{
// Get the screen height
mScreenHeight = pScreenHeight;
// Get the pointer to the pieces class
//mPieces = &pPieces;
//Init the board blocks with free positions
InitBoard();
}
/*
======================================
Init the board blocks with free positions
======================================
*/
void Board::InitBoard()
{
for (int i = 0; i < boardWidth; i++)
for (int j = 0; j < boardHeight; j++)
{
//mBoard[i][j] = POS_FREE;
mBoardcolor[i][j] = color::Black;
}
}
/*
======================================
Store a piece in the board by filling the blocks
Parameters:
>> pX: Horizontal position in blocks
>> pY: Vertical position in blocks
>> pPiece: Piece to draw
>> pRotation: 1 of the 4 possible rotations
======================================
*/
void Board::StorePiece(int pX, int pY, int pPiece, int pRotation)
{
// Store each block of the piece into the board
//const color mColor = mPieces->GetPieceColor(pPiece);
const color mColor = Pieces::GetPieceColor(pPiece);
for (int i1 = pX, i2 = 0; i1 < pX + pieceBlocks; i1++, i2++)
{
for (int j1 = pY, j2 = 0; j1 < pY + pieceBlocks; j1++, j2++)
{
// Store only the blocks of the piece that are not holes
//if (mPieces->GetBlockType(pPiece, pRotation, j2, i2) != 0)
if (Pieces::GetBlockType(pPiece, pRotation, j2, i2) != 0)
{
//mBoard[i1][j1] = POS_FILLED;
mBoardcolor[i1][j1] = mColor;
}
}
}
}
/*
======================================
Check if the game is over becase a piece have achived the upper position
Returns true or false
======================================
*/
bool Board::IsGameOver()
{
//If the first line has blocks, then, game over
for (int i = 0; i < boardWidth; i++)
{
// if (mBoard[i][0] == POS_FILLED) return true;
if (mBoardcolor[i][0] != color::Black) return true;
}
return false;
}
/*
======================================
Delete a line of the board by moving all above lines down
Parameters:
>> pY: Vertical position in blocks of the line to delete
======================================
*/
void Board::DeleteLine(int pY)
{
// Moves all the upper lines one row down
for (int j = pY; j > 0; j--)
{
for (int i = 0; i < boardWidth; i++)
{
//mBoard[i][j] = mBoard[i][j - 1];
mBoardcolor[i][j] = mBoardcolor[i][j - 1];
}
}
}
/*
======================================
Delete all the lines that should be removed
======================================
*/
int Board::DeletePossibleLines()
{
int nr_of_lines = 0;
for (int j = 0; j < boardHeight; j++)
{
int i = 0;
while (i < boardWidth)
{
//if (mBoard[i][j] != POS_FILLED) break;
if (mBoardcolor[i][j] == color::Black) break;
i++;
}
if (i == boardWidth) {
// for (int k = 0; k < BOARD_WIDTH; k++){
// mBoardcolor[k][j] = BLUE;
// }
DeleteLine(j);
nr_of_lines++;
}
}
return nr_of_lines;
}
/*
======================================
Returns 1 (true) if the this block of the board is empty, 0 if it is filled
Parameters:
>> pX: Horizontal position in blocks
>> pY: Vertical position in blocks
======================================
*/
bool Board::IsFreeBlock(int pX, int pY)
{
//if (mBoard[pX][pY] == POS_FREE) return true; else return false;
if (mBoardcolor[pX][pY] == color::Black) return true; else return false;
}
color Board::GetColor(int pX, int pY)
{
return mBoardcolor[pX][pY];
}
/*
======================================
Returns the horizontal position (isn pixels) of the block given like parameter
Parameters:
>> pPos: Horizontal position of the block in the board
======================================
*/
int Board::GetXPosInPixels(int pPos)
{
return ((boardPosition - (blockSize * (boardWidth / 2))) + (pPos * blockSize));
}
/*
======================================
Returns the vertical position (in pixels) of the block given like parameter
Parameters:
>> pPos: Horizontal position of the block in the board
======================================
*/
int Board::GetYPosInPixels(int pPos)
{
return ((mScreenHeight - (blockSize * boardHeight)) + (pPos * blockSize));
}
/*
======================================
Check if the piece can be stored at this position without any collision
Returns true if the movement is possible, false if it not possible
Parameters:
>> pX: Horizontal position in blocks
>> pY: Vertical position in blocks
>> pPiece: Piece to draw
>> pRotation: 1 of the 4 possible rotations
======================================
*/
bool Board::IsPossibleMovement(int pX, int pY, int pPiece, int pRotation)
{
// Checks collision with pieces already stored in the board or the board limits
// This is just to check the 5x5 blocks of a piece with the appropiate area in the board
for (int i1 = pX, i2 = 0; i1 < pX + pieceBlocks; i1++, i2++)
{
for (int j1 = pY, j2 = 0; j1 < pY + pieceBlocks; j1++, j2++)
{
// Check if the piece is outside the limits of the board
if (i1 < 0 ||
i1 > boardWidth - 1 ||
j1 > boardHeight - 1)
{
//if (mPieces->GetBlockType(pPiece, pRotation, j2, i2) != 0)
if (Pieces::GetBlockType(pPiece, pRotation, j2, i2) != 0)
return 0;
}
// Check if the piece have collisioned with a block already stored in the map
if (j1 >= 0)
{
//if ((mPieces->GetBlockType(pPiece, pRotation, j2, i2) != 0) && (!IsFreeBlock(i1, j1)))
if ((Pieces::GetBlockType(pPiece, pRotation, j2, i2) != 0) && (!IsFreeBlock(i1, j1)))
return false;
}
}
}
// No collision
return true;
}