-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame_model.cc
244 lines (231 loc) · 6.92 KB
/
game_model.cc
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
#include "game_model.h"
#include <stdlib.h>
#include <time.h>
#include <SDL2/SDL.h>
GameModel::GameModel(void) : blocks_(std::vector<Block*>()) {
next_tick_ = SDL_GetTicks() + 1000;
}
GameModel::~GameModel() {
for (std::vector<Block*>::iterator it = blocks_.begin(); it != blocks_.end();
++it) {
Block* block = *it;
delete block;
}
}
void GameModel::init(void) {
// Create the first figure
figure_ = *create_figure(&blocks_);
}
void GameModel::update(Uint32 time, Direction direction) {
switch (direction) {
case UP:
rotate_figure(figure_);
if (collides_with_grid(figure_, blocks_)) {
rotate_figure(figure_);
rotate_figure(figure_);
rotate_figure(figure_);
}
break;
case DOWN:
while (!collides_with_grid(figure_, blocks_)) {
move(figure_, DOWN);
}
move(figure_, UP);
figure_ = *create_figure(&blocks_);
delete_full_rows(blocks_);
break;
case LEFT:
move(figure_, LEFT);
if (collides_with_grid(figure_, blocks_)) {
move(figure_, RIGHT);
}
break;
case RIGHT:
move(figure_, RIGHT);
if (collides_with_grid(figure_, blocks_)) {
move(figure_, LEFT);
}
break;
case NONE:
if (time > next_tick_) {
next_tick_ += 1000;
move(figure_, DOWN);
if (collides_with_grid(figure_, blocks_)) {
move(figure_, UP);
figure_ = *create_figure(&blocks_);
delete_full_rows(blocks_);
}
}
break;
}
}
std::vector<Block*>& GameModel::get_blocks(void) {
return blocks_;
}
void GameModel::move(std::vector<Block*>& figure, Direction direction) {
for (std::vector<Block*>::iterator it = figure_.begin(); it != figure_.end();
++it) {
Block* block = *it;
switch (direction) {
case UP:
block->set_y(block->get_y() + 1);
break;
case DOWN:
block->set_y(block->get_y() - 1);
break;
case LEFT:
block->set_x(block->get_x() - 1);
break;
case RIGHT:
block->set_x(block->get_x() + 1);
break;
}
}
}
bool GameModel::collides_with_grid(std::vector<Block*>& figure,
std::vector<Block*>& blocks) {
for (std::vector<Block*>::iterator it = figure.begin(); it != figure.end();
++it) {
Block* a = *it;
if (a->get_x() < 0 || a->get_x() >= BLOCKS_IN_ROW || a->get_y() < 0 ||
a->get_y() > BLOCKS_IN_COL) {
return true;
}
for (std::vector<Block*>::iterator it2 = blocks.begin();
it2 != blocks.end(); ++it2) {
Block* b = *it2;
if (a != b) {
if (a->get_x() == b->get_x() && a->get_y() == b->get_y()) {
return true;
}
}
}
}
return false;
}
std::vector<Block*>* GameModel::create_figure(std::vector<Block*>* blocks) {
std::vector<Block*>* figure = new std::vector<Block*>;
srand(time(NULL));
int i = rand() % 7;
GLcolor color;
switch (i) {
case 0: // Square
color = {0.2f, 0.4, 0.6f};
create_block(4, 21, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(4, 20, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
break;
case 1: // 4 line
color = {0.8f, 0.4, 0.6f};
create_block(5, 21, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(5, 19, color, blocks, figure);
create_block(5, 18, color, blocks, figure);
break;
case 2:
color = {0.8f, 0.4, 0.2f};
create_block(4, 21, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(6, 20, color, blocks, figure);
break;
case 3:
color = {0.2f, 0.4, 0.2f};
create_block(4, 20, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(6, 21, color, blocks, figure);
break;
case 4:
color = {0.8f, 0.2, 0.6f};
create_block(4, 21, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(5, 19, color, blocks, figure);
break;
case 5:
color = {0.2f, 0.2, 0.6f};
create_block(6, 21, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(5, 19, color, blocks, figure);
break;
case 6:
color = {0.4f, 0.2, 0.2f};
create_block(4, 20, color, blocks, figure);
create_block(5, 20, color, blocks, figure);
create_block(5, 21, color, blocks, figure);
create_block(6, 20, color, blocks, figure);
break;
}
}
void GameModel::rotate_figure(std::vector<Block*>& figure) {
int max_x = -1, min_x = 100, max_y = -1, min_y = 100;
for (std::vector<Block*>::iterator it = figure.begin(); it != figure.end();
++it) {
Block* a = *it;
max_x = (a->get_x() > max_x) ? a->get_x() : max_x;
min_x = (a->get_x() < min_x) ? a->get_x() : min_x;
max_y = (a->get_y() > max_y) ? a->get_y() : max_y;
min_y = (a->get_y() < min_y) ? a->get_y() : min_y;
}
int width = max_x - min_x;
int height = max_y - min_y;
for (std::vector<Block*>::iterator it = figure.begin(); it != figure.end();
++it) {
Block* a = *it;
int x = a->get_x();
int y = a->get_y();
a->set_y(min_y + (height - (x - min_x)));
a->set_x(min_x + (y - min_y));
}
}
void GameModel::delete_full_rows(std::vector<Block*>& blocks) {
// Check every row if any is full
for (int y = BLOCKS_IN_COL - 1; y >= 0; --y) {
int blocks_in_row = 0;
// Count number of blocks on current row
for (std::vector<Block*>::iterator it = blocks.begin(); it != blocks.end();
++it) {
Block* block = *it;
if (block->get_y() == y) {
++blocks_in_row;
}
}
// If row is full
if (blocks_in_row == BLOCKS_IN_ROW) {
// Delete all blocks on the current row
std::vector<Block*>::iterator it = blocks.begin();
while (it != blocks.end()) {
Block* block = *it;
if (block->get_y() == y) {
// Free memory of the block
delete block;
// Erase the block entry from the collection
blocks.erase(it);
} else {
++it;
}
}
// Move down all blocks above the current row
move_down_blocks_above_level(blocks, y);
}
}
}
void GameModel::move_down_blocks_above_level(std::vector<Block*>& blocks,
int level) {
for (std::vector<Block*>::iterator it = blocks.begin(); it != blocks.end();
++it) {
Block* block = *it;
if (block->get_y() > level) {
block->set_y(block->get_y() - 1);
}
}
}
void GameModel::create_block(int x, int y, GLcolor color,
std::vector<Block*>* blocks, std::vector<Block*>* figure) {
Block* block = new Block(x, y, color);
blocks->push_back(block);
figure->push_back(block);
}