-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
223 lines (184 loc) · 5.74 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
#include "Board.h"
Board::Board(void)
{
}
Board::Board (Pieces *pPieces, int pScreenHeight)
{
// Get the screen height
mScreenHeight = pScreenHeight;
// Get the pointer to the pieces class
mPieces = pPieces;
particleEngines.resize(10);
for(int i = 0; i < particleEngines.size(); i++)
{
particleEngines[i].mEmitterLocation = sf::Vector2f(200 + (i * BLOCK_SIZE),470);
}
//Init the board blocks with free positions
buffer.loadFromFile("Bam.wav");
sound.setBuffer(buffer);
InitBoard();
}
Board::~Board(void)
{
}
void Board::InitBoard()
{
for (int i = 0; i < BOARD_WIDTH; i++)
for (int j = 0; j < BOARD_HEIGHT; j++)
mBoard[i][j] = POS_FREE;
mScore = 0;
}
/*
======================================
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
for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
{
for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
{
// Store only the blocks of the piece that are not holes
if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0)
mBoard[i1][j1] = pPiece + 1;
}
}
}
/*
======================================
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 < BOARD_WIDTH; i++)
{
if (mBoard[i][0] != 0) 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 < BOARD_WIDTH; i++)
{
mBoard[i][j] = mBoard[i][j-1];
particleEngines[i].mEmitterLocation = sf::Vector2f(200 + (i * BLOCK_SIZE), GetYPosInPixels(pY));
}
}
mScore++;
sound.play();
for(int i = 0; i < particleEngines.size(); i++)
{
particleEngines[i].Refill(particleEngines.size());
}
}
/*
======================================
Delete all the lines that should be removed
======================================
*/
void Board::DeletePossibleLines ()
{
for (int j = 0; j < BOARD_HEIGHT; j++)
{
int i = 0;
while (i < BOARD_WIDTH)
{
if (mBoard[i][j] == 0) break;
i++;
}
if (i == BOARD_WIDTH) DeleteLine (j);
}
}
/*
======================================
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;
}
/*
======================================
Returns the horizontal position (in pixels) of the block given like parameter
Parameters:
>> pPos: Horizontal position of the block in the board
======================================
*/
int Board::GetXPosInPixels (int pPos)
{
return ( ( BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) );
}
/*
======================================
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 - (BLOCK_SIZE * BOARD_HEIGHT)) + (pPos * BLOCK_SIZE) );
}
/*
======================================
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 appropriate area in the board
for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
{
for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
{
// Check if the piece is outside the limits of the board
if ( i1 < 0 ||
i1 > BOARD_WIDTH - 1 ||
j1 > BOARD_HEIGHT - 1)
{
if (mPieces->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)) )
return false;
}
}
}
// No collision
return true;
}