-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris_tests.cpp
272 lines (230 loc) · 6.91 KB
/
tetris_tests.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
#include "unit_test_framework.h"
#include "grid.hpp"
using std::operator""s;
TEST(TestGameBoardConstructor)
{
int height = std::rand() % 25 + 5;
int width = std::rand() % 25 + 5;
tetris::GameBoard board(height, width);
ASSERT_EQUAL(board.getHeight(), height);
ASSERT_EQUAL(board.getWidth(), width);
ASSERT_EQUAL(board.get_score(), 0); // Using score variable directly
ASSERT_EQUAL(board.lines_cleared_count(), 0); // Using lines_cleared variable directly
}
TEST(TestGenerateNewPiece)
{
int height = std::rand() % 25 + 5;
int width = std::rand() % 25 + 5;
tetris::GameBoard board(height, width);
board.generate_new_piece();
auto piece = board.getBlock();
// Assert that the block type is within a valid range 1 to 7
ASSERT_TRUE(piece >= 1 && piece <= 7);
int b_x = board.b_x;
int b_y = board.b_y;
ASSERT_TRUE(b_x >= 0 && b_x < width);
ASSERT_TRUE(b_y >= 0 && b_y < height);
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
ASSERT_EQUAL(tetris::shapes.at(piece)[y][x], board.get_current_shape()[y][x]);
}
}
}
TEST(TestInBounds)
{
int height = std::rand() % 25 + 5;
int width = std::rand() % 40 + 5;
tetris::GameBoard board(height, width);
board.generate_new_piece();
for (int b_y = 0; b_y < height - 3; ++b_y)
{
for (int b_x = 0; b_x < width - 3; ++b_x)
{
// Set the position of the current piece
board.b_x = b_x;
board.b_y = b_y;
bool isWithinBounds = board.in_bounds();
ASSERT_EQUAL(isWithinBounds, (b_x >= 0 && b_x + 3 < width) &&
(b_y >= 0 && b_y + 3 < height));
}
}
}
TEST(TestHasHitPile)
{
int height = std::rand() % 40 + 5;
int width = std::rand() % 45 + 5;
tetris::GameBoard board(height, width);
board.generate_new_piece(); // Ensure there's a current piece to test
// Iterate through different positions of the current piece
for (int b_y = -4; b_y < height; ++b_y)
{
for (int b_x = -4; b_x < width; ++b_x)
{
// Set the position of the current piece
board.b_x = b_x;
board.b_y = b_y;
// Check if the current piece has hit the pile using the has_hit_pile() function
bool hasHitPile = board.has_hit_pile();
// Calculate the expected result based on position
bool expected = false;
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (board.get_current_shape()[y][x])
{
int grid_row = b_y + y;
int grid_col = b_x + x;
if (grid_row < 0 || grid_row >= height || grid_col < 0 || grid_col >= width)
{
expected = true;
break;
}
if (board.getGameState()[grid_row][grid_col])
{
expected = true;
break;
}
}
}
if (expected)
{
break;
}
}
// Result matches expected
ASSERT_EQUAL(hasHitPile, expected);
}
}
}
TEST(TestShiftDown)
{
int height = std::rand() % 25 + 5;
int width = std::rand() % 40 + 5;
tetris::GameBoard board(height, width);
for (int y = height / 2 - 1; y <= height / 2; ++y)
{
for (int x = 0; x < width; ++x)
{
board.getGameState()[y][x] = 1;
}
}
int initialNumLines = 0;
for (int y = 0; y < height; ++y)
{
bool hasLine = true;
for (int x = 0; x < width; ++x)
{
if (board.getGameState()[y][x] == 0)
{
hasLine = false;
break;
}
}
if (hasLine)
{
++initialNumLines;
}
}
board.shift_down();
int finalNumLines = 0;
for (int y = 0; y < height; ++y)
{
bool hasLine = true;
for (int x = 0; x < width; ++x)
{
if (board.getGameState()[y][x] == 0)
{
hasLine = false;
break;
}
}
if (hasLine)
{
++finalNumLines;
}
}
ASSERT_EQUAL(finalNumLines, 0);
}
TEST(TestRotate)
{
int height = std::rand() % 40 + 5;
int width = std::rand() % 35 + 5;
tetris::GameBoard board(height, width);
// Set up the game board with a piece
board.generate_new_piece();
// Get the initial orientation of the piece
// the O is always the same, so handling that case
if (board.getBlock() == 1)
{
board.get_current_shape() = tetris::L_SHAPE;
}
auto initialPiece = board.get_current_shape();
// Rotate the piece
board.rotate();
board.rotate();
// Get the new orientation of the piece after rotation
auto rotatedPiece = board.get_current_shape();
// Lambda function to compare two shapes (vectors)
auto shapes_equal = [](const std::vector<std::vector<int>> &shape1,
const std::vector<std::vector<int>> &shape2)
{
if (shape1.size() != shape2.size())
{
return false;
}
for (size_t i = 0; i < shape1.size(); ++i)
{
for (size_t j = 0; j < shape1[i].size(); ++j)
{
if (shape1[i][j] != shape2[i][j])
{
return false;
}
}
}
return true;
};
// Check if the rotated piece is not the same
ASSERT_FALSE(shapes_equal(initialPiece, rotatedPiece));
board.rotate();
board.rotate();
// Rotate it back
auto restoredPiece = board.get_current_shape();
// Checking that the it is back to its original spot
ASSERT_TRUE(shapes_equal(initialPiece, restoredPiece));
}
TEST(TestGetScore)
{
int height = 20;
int width = 10;
tetris::GameBoard board(height, width);
for (int x = 0; x < width; ++x)
{
board.getGameState()[width - 1][x] = 1;
board.getGameState()[width - 2][x] = 1;
}
board.shift_down();
// Get the score from the board
int actualScore = board.get_score();
ASSERT_EQUAL(actualScore, 400);
}
TEST(TestCheckLinesCleared)
{
int height = 43;
int width = 22;
tetris::GameBoard board(height, width);
for (int x = 0; x < width; ++x)
{
board.getGameState()[width - 1][x] = 1;
board.getGameState()[width - 3][x] = 1;
}
board.shift_down();
// Get the score from the board
int linesCleared = board.lines_cleared_count();
ASSERT_EQUAL(linesCleared, 2);
}
// Define main function to run tests
TEST_MAIN()