-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
342 lines (256 loc) · 6.36 KB
/
Copy pathengine.cpp
File metadata and controls
342 lines (256 loc) · 6.36 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "engine.hh"
#include <ctime>
#include "tetromino_rotation_offsets.hh"
const int points_for_lines[] = { 0, 40, 100, 300, 1200 };
const int lines_per_level = 6;
Engine::Engine(const int& width, const int& height)
: width_(width), height_(height)
{
// Setting random engine ready for the first real call.
int seed = time(nullptr); // You can change seed value for testing purposes
randomEng_.seed(seed);
distr_ = std::uniform_int_distribution<int>(0, NUMBER_OF_TETROMINOS - 1);
distr_(randomEng_); // Wiping out the first random number (which is almost always 0)
current_tetromino_ = nullptr;
hold_tetromino_ = nullptr;
}
Engine::~Engine()
{
clear();
}
void Engine::start()
{
clear();
score = 0;
tetrises = 0;
lines_removed = 0;
level = 0;
next_tetromino_ = NUMBER_OF_TETROMINOS;
hold_tetromino_ = nullptr;
can_hold = true;
set_new_tetromino();
}
Engine::Game_state Engine::tick()
{
if (!current_tetromino_->move(DOWN))
{
int removed = remove_full_lines();
lines_removed += removed;
if (removed == 4)
{
tetrises++;
}
if (lines_removed / lines_per_level > level)
{
level++;
game_speed -= game_speed / 10;
}
score += points_for_lines[removed] * (level + 1);
if (check_gameover())
{
return Game_state::GAMEOVER;
}
set_new_tetromino();
if (removed)
{
return Game_state::LINE_REMOVED;
}
return Game_state::NEW_BLOCK;
}
return Game_state::PLAYING;
}
void Engine::move_horizontal(const int& direction)
{
current_tetromino_->move({ direction, 0 });
}
void Engine::drop()
{
while (current_tetromino_->move(DOWN)) {}
}
bool Engine::rotate(const bool& clockwise)
{
return current_tetromino_->rotate(clockwise);
}
void Engine::hold()
{
if (!can_hold)
{
return;
}
can_hold = false;
for (Block* block : current_tetromino_->blocks_)
{
blocks_.erase(std::find(blocks_.begin(), blocks_.end(), block));
}
Tetromino* temp = current_tetromino_;
current_tetromino_ = hold_tetromino_;
hold_tetromino_ = temp;
if (current_tetromino_)
{
Point start = { width_ / 2, height_ + 1 };
Point move = start - current_tetromino_->blocks_.at(0)->position;
for (Block* block : current_tetromino_->blocks_)
{
block->position += move;
blocks_.push_back(block);
}
}
else
{
set_new_tetromino();
}
}
Block* Engine::get_drop_position()
{
int count = 0;
while (current_tetromino_->move(DOWN))
{
count++;
}
Block* drop_blocks = new Block[4];
for (unsigned int i = 0; i < 4; i++)
{
drop_blocks[i].position = current_tetromino_->blocks_.at(i)->position;
drop_blocks[i].color = QColor(255, 255, 255);
}
for (int i = 0; i < count; i++)
{
current_tetromino_->move(UP);
}
return drop_blocks;
}
std::vector<Block*> Engine::get_next_tetromino()
{
Tetromino* next = new_tetromino(next_tetromino_, false);
std::vector<Block*> blocks = next->blocks_;
delete next;
return blocks;
}
std::vector<Block*> Engine::get_hold_tetromino()
{
if (!hold_tetromino_)
{
return {};
}
std::vector<Block*> blocks = hold_tetromino_->blocks_;
return blocks;
}
void Engine::clear()
{
if (current_tetromino_)
{
delete current_tetromino_;
current_tetromino_ = nullptr;
}
if (hold_tetromino_)
{
for (Block* block : hold_tetromino_->blocks_)
{
delete block;
}
delete hold_tetromino_;
}
for (Block* block : blocks_)
{
delete block;
}
blocks_.clear();
}
void Engine::set_new_tetromino()
{
can_hold = true;
Tetromino_kind kind = next_tetromino_ == NUMBER_OF_TETROMINOS ? (Tetromino_kind)distr_(randomEng_)
: next_tetromino_;
delete current_tetromino_;
current_tetromino_ = new_tetromino(kind);
next_tetromino_ = (Tetromino_kind)distr_(randomEng_);
}
Tetromino* Engine::random_tetromino()
{
return new_tetromino((Tetromino_kind)distr_(randomEng_), true);
}
Tetromino* Engine::new_tetromino(const Tetromino_kind& kind, const bool& add_to_blocks)
{
switch (kind)
{
case HORIZONTAL:
return create_tetromino(QColor(0, 255, 255), { { 0, 0 }, { -1, 0 }, { 1, 0 }, { 2, 0 } }, I_OFFSETS, add_to_blocks);
case LEFT_CORNER:
return create_tetromino(QColor(0, 0, 255), { { 0, 0 }, { -1, 1 }, { -1, 0 }, { 1, 0 } }, JLSTZ_OFFSETS, add_to_blocks);
case RIGHT_CORNER:
return create_tetromino(QColor(255, 127, 0), { { 0, 0 }, { -1, 0 }, { 1, 0 }, { 1, 1 } }, JLSTZ_OFFSETS, add_to_blocks);
case SQUARE:
return create_tetromino(QColor(255, 127, 0), { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, O_OFFSETS, add_to_blocks);
case STEP_UP_RIGHT:
return create_tetromino(QColor(0, 255, 0), { { 0, 0 }, { -1, 0 }, { 0, 1 }, { 1, 1 } }, JLSTZ_OFFSETS, add_to_blocks);
case PYRAMID:
return create_tetromino(QColor(126, 63, 190), { { 0, 0 }, { -1, 0 }, { 1, 0 }, { 0, 1 } }, JLSTZ_OFFSETS, add_to_blocks);
case STEP_UP_LEFT:
return create_tetromino(QColor(255, 0, 0), { { 0, 0 }, { -1, 1 }, { 0, 1 }, { 1, 0 } }, JLSTZ_OFFSETS, add_to_blocks);
default:
return nullptr;
}
}
Tetromino* Engine::create_tetromino(const QColor& color, const std::vector<Point>& points_to_add, const std::vector<Offset>& offsets, const bool& add_to_blocks)
{
int start_x = width_ / 2;
int start_y = height_ + 1;
Tetromino* tetromino = new Tetromino(this, offsets);
for (Point point : points_to_add)
{
Block* block = new Block({ { start_x + point.x, start_y + point.y }, color });
tetromino->blocks_.push_back(block);
if (add_to_blocks)
{
blocks_.push_back(block);
}
}
return tetromino;
}
int Engine::remove_full_lines()
{
// Create and initialize grid
std::vector<std::vector<Block*>> grid_lines;
for (int i = 0; i < height_ + 4; i++)
{
grid_lines.push_back({});
}
// Add all blocks to their grid line
for (Block* block : blocks_)
{
grid_lines.at(block->position.y).push_back(block);
}
int removed_count = 0;
// Remove full lines
for (unsigned int i = 0; i < grid_lines.size(); i++)
{
if (grid_lines.at(i).size() == (unsigned int)width_)
{
removed_count++;
// Delete blocks
for (Block* block : grid_lines.at(i))
{
blocks_.erase(std::find(blocks_.begin(), blocks_.end(), block));
delete block;
}
// Move lines above this line down
for (unsigned int j = i + 1; j < grid_lines.size(); j++)
{
for (Block* block : grid_lines.at(j))
block->position.y--;
}
}
}
return removed_count;
}
bool Engine::check_gameover()
{
for (Block* block : current_tetromino_->blocks_)
{
if (block->position.y > height_)
{
return true;
}
}
return false;
}