-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameController.cpp
396 lines (344 loc) · 15.3 KB
/
GameController.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "GameController.h"
#include "BlockFall.h"
#include <fstream>
#include <iostream>
using namespace std;
#include "Leaderboard.h"
#include <chrono>
#include <ctime>
using namespace std;
std::vector<std::vector<int>> GameController::check(const std::vector<std::vector<int>>& grid,std::vector<std::vector<bool>> powerup,unsigned long* scorePtr) {
int rows = grid.size();
int cols = grid[0].size();
bool powerUpFound = false;
for (int i = 0; i <= rows - powerup.size(); ++i) {
for (int j = 0; j <= cols - powerup[0].size(); ++j) {
bool found = true;
for (int k = 0; k < powerup.size(); ++k) {
for (int l = 0; l < powerup[0].size(); ++l) {
if (grid[i + k][j + l] != powerup[k][l]) {
found = false;
break;
}
}
if (!found) {
break;
}
}
if (found) {
powerUpFound = true;
std::cout << "Before clearing:"<< std::endl;
for (const auto &row : grid) {
for (int value : row) {
std::cout << (value == 0 ? unoccupiedCellChar : occupiedCellChar) ;
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
int trueCount = 0;
for (const auto& row : grid) {
for (bool cell : row) {
if (cell) {
trueCount++;
}
}
};
*scorePtr=*scorePtr+(1000+trueCount);
std::vector<std::vector<int>> clearedGrid(rows, std::vector<int>(cols, 0));
return clearedGrid;
}
}
}
std::vector<int> onesVector(cols, 1);
std::vector<int> zerosVector(cols, 0);
std::vector<std::vector<int>> checkedgrid = grid;
int checker=0;
for (int i = 0; i < rows; ++i) {
if (grid[i] == onesVector) {
if (!gravityclear&&checker==0){
checker++;
std::cout << "Before clearing:"<< std::endl;
for (const auto &row : grid) {
for (int value : row) {
std::cout << (value == 0 ? unoccupiedCellChar : occupiedCellChar) ;
}
std::cout << std::endl;
}
std::cout <<endl <<std::endl;}
*scorePtr = *scorePtr + (cols);
checkedgrid.erase(checkedgrid.begin() + i); // Delete the full line
checkedgrid.insert(checkedgrid.begin(), zerosVector);
}
}
return checkedgrid;
}
//Just printing the grid :
void GameController::print_grid(std::vector<std::vector<int>> grid, int score, int highscore) {
std::cout<<"Score: "<<score<<std::endl;
std::cout<<"High Score: "<< highscore<<std::endl;
for (const auto &row : grid) {
for (int value : row) {
std::cout << (value == 0 ? unoccupiedCellChar : occupiedCellChar);
}
std::cout << std::endl;
}
}
//Printing a block:
void GameController::print_block(std::vector<std::vector<bool>> grid) {
for (const auto &row : grid) {
for (int value : row) {
std::cout << (!value ? unoccupiedCellChar : occupiedCellChar);
}
std::cout << std::endl;
}
}
//Checking if a block can be placed
bool GameController::canPlaceBlock(const std::vector<std::vector<int>>& grid, const std::vector<std::vector<bool>>& block, int startI, int startJ) {
for (int i = 0; i < block.size(); ++i) {
int newRow = startI + i;
for (int j = 0; j < block[i].size(); ++j) {
int newCol = startJ + j;
// Check if the insertion position is within the grid's borders
if (newRow >= 0 && newRow < grid.size() && newCol >= 0 && newCol < grid[0].size()) {
// Check if the cell is occupied in the grid before insertion
if (block[i][j] && grid[newRow][newCol] == 1) {
return false;
}
} else {
return false;
}
}
}
return true; //Case where the block can be placed
}
//Inserting a certain block into a certain cell into the grid
std::vector<std::vector<int>> GameController::insert(const std::vector<std::vector<int>>& grid, const std::vector<std::vector<bool>>& block, int startI, int startJ) {
std::vector<std::vector<int>> updatedGrid = grid;
//Iterating over the rows and the columns of the block:
for (int i = 0; i < block.size(); ++i) {
int newRow = startI + i;
for (int j = 0; j < block[i].size(); ++j) {
int newCol = startJ + j;
// Check if the insertion position is within the grid's borders
if (newRow >= 0 && newRow < updatedGrid.size() && newCol >= 0 && newCol < updatedGrid[0].size()) {
// Check if the cell is empty in the grid before insertion
if (block[i][j] && updatedGrid[newRow][newCol] != 1) {
updatedGrid[newRow][newCol] = 1;
}
}
}
}
return updatedGrid;
}
bool GameController::play(BlockFall& game, const string& commands_file){
//We will need to initiate i and j:
int i=0;
int j=0;
std::chrono::system_clock::time_point currentTime = std::chrono::system_clock::now();
LeaderboardEntry* currentplayer = new LeaderboardEntry(game.current_score, std::chrono::system_clock::to_time_t(currentTime), game.player_name);
highscore= (game.leaderboard.head_leaderboard_entry!=nullptr?game.leaderboard.head_leaderboard_entry->score:0);
//We will need this temporary grid: to display before dropping and finalizing:
std::vector<std::vector<int>> tempGrid;
//Reading the commands
std::ifstream commands(commands_file);
if(!commands.is_open()){
std::cerr<<"Error while opening commands data file...";
return false;
}
std::string line;
//Now we start iterating over the lines:
while (commands >> line) {
std::vector<std::vector<bool>>& curr = game.active_rotation->shape;
if (line == "PRINT_GRID") {
//Update curr first:
tempGrid = insert(game.grid, curr, i, j);
// game.grid=check(game.grid,game.power_up);
print_grid(tempGrid, game.current_score, highscore);
std::cout<<std::endl;
std::cout<<std::endl;
}
if (line == "ROTATE_RIGHT" || line == "ROTATE_LEFT") {
int startI= i;
int startJ=j;
Block* tempRotatedBlock = game.active_rotation;
Block* next = game.active_rotation->next_block;
if (line == "ROTATE_RIGHT") {
tempRotatedBlock= tempRotatedBlock->right_rotation;
} else {
tempRotatedBlock= tempRotatedBlock->left_rotation;}
bool canPlace = canPlaceBlock(game.grid, tempRotatedBlock->shape, startI, startJ);
if (canPlace) {
game.active_rotation=tempRotatedBlock;
}
}
if (line == "MOVE_RIGHT") {
if (canPlaceBlock(game.grid, curr, i, j + 1)) {
j += 1;
}
}
if (line == "MOVE_LEFT") {
if (canPlaceBlock(game.grid, curr, i, j - 1)) {
j -= 1;
}
}
if (line == "DROP") {
//First for the score we need to find the number of 1s/ trues.
int trueCount = 0;
for (const auto& row : curr) {
for (bool cell : row) {
if (cell) {
trueCount++;
}
}
}
//No gravity
if (!game.gravity_mode_on) {
//We check required rows to make sure it stops when we reach the end of the grid:
int required_rows = game.rows - curr.size();
// int drop= game.rows;
//This is where the block should be inserted:
int stop=required_rows;
int i=0;
//Now iterating over the rows, to find the smallest row under one of the blocks columns that has a 1.
while (i < required_rows) {
for (int row = 0; row < curr.size(); row++) {
for (int col = 0; col < curr[0].size(); col++) {
if (curr[row][col] == 1 && game.grid[i + row+1][j + col] == 1) {
stop= (stop>=i)?i:stop;
// drop=min(drop,stop);
}
}
}
i++;
}
game.grid = insert(game.grid, curr, stop, j);
game.current_score+=(trueCount*stop);
highscore = max(highscore, game.current_score);
game.grid = check(game.grid, game.power_up, &(game.current_score));
}
//If gravity mode is on:
else {
//This is the row we will insert the ones in
int rowPos=0;
int smallest_distance=game.rows;
int final_drop_distance=game.rows;
int sum_all_ones=0;
//First thing: We need to iterate over every column, Find out how many 1s are in it
//And find out which column in the grid they should be placed in:
for (int col = 0; col < curr[0].size(); col++) {
int biggest1_index=0;
int onesCount=0;
for (int row = 0; row < curr.size(); row++) {
// If the element in this column is true (i.e., one), increment the count for this column
if (curr[row][col]==1) {
biggest1_index=max(biggest1_index,row);
onesCount++;
sum_all_ones++;
}
}
//This is the column we will insert the ones in
int columnPos=j+col;
for (int row = curr.size(); row < game.rows; ++row) {
if (game.grid[row][columnPos] == 1) {
rowPos=row-onesCount;
break;
}
if (row==game.rows-1){
rowPos=game.rows-onesCount;
break;
}
}
std::vector<std::vector<bool>> insertion(onesCount, std::vector<bool>(1, true));
game.grid =insert(game.grid,insertion,rowPos,columnPos);
int drop_distance=rowPos-biggest1_index+onesCount-1;
final_drop_distance=min(drop_distance,final_drop_distance);
}
game.current_score+=(final_drop_distance*sum_all_ones);
highscore = max(highscore, game.current_score);
game.grid=check(game.grid,game.power_up,&(game.current_score));
}
i=0;
j=0;
if (game.active_rotation->next_block!=nullptr){
game.active_rotation =game.active_rotation->next_block;
curr=game.active_rotation->shape;
bool canPlace = canPlaceBlock(game.grid, curr, 0, 0);
if (!canPlace) {
std::cout << "GAME OVER!" << std::endl;
std::cout<<"Next block that couldn't fit:"<<std::endl;
print_block(game.active_rotation->shape);
std::cout<<std::endl;
std::cout<<"Final grid and score:"<<std::endl;
std::cout<<std::endl;
print_grid(game.grid, game.current_score,highscore);
currentplayer->score=game.current_score;
game.leaderboard.insert_new_entry(currentplayer);
std::cout<<endl;
std::cout<<"Leaderboard"<<std::endl;
std::cout<<"-----------"<<endl;
game.leaderboard.print_leaderboard();
game.leaderboard.write_to_file(game.leaderboard_file_name);
// std::cout<<endl<<endl<<endl;
return false; // End the game
}
}
else{
std::cout<<"YOU WIN!"<<std::endl<<"No more blocks."<<std::endl<<"Final grid and score:"<<std::endl;
std::cout<<endl;
print_grid(game.grid, game.current_score,highscore);
currentplayer->score=game.current_score;
game.leaderboard.insert_new_entry(currentplayer);
std::cout<<endl;
std::cout<<"Leaderboard"<<std::endl;
std::cout<<"-----------"<<endl;
game.leaderboard.print_leaderboard();
game.leaderboard.write_to_file(game.leaderboard_file_name);
// std::cout<<endl<<endl<<endl;
youwin=true;
return true; // End the game
}
};
if (line=="GRAVITY_SWITCH"){
game.gravity_mode_on=!(game.gravity_mode_on);
std::vector<std::vector<int>> gravityGrid = game.grid;
if (game.gravity_mode_on) {
std::vector<std::vector<int>> tempGrid = game.grid;
for (int col = 0; col < game.grid[0].size(); col++) {
int onesCount = 0;
std::vector<int> columnValues;
// Find the number of ones:
for (int row = 0; row < game.grid.size(); row++) {
columnValues.push_back(game.grid[row][col]);
if (game.grid[row][col] == 1) {
onesCount++;
}
}
for (int row = 0; row < game.grid.size(); row++) {
tempGrid[row][col] = 0;
}
// Fill the ones where they should go
for (int i = game.grid.size() - onesCount; i < game.grid.size(); ++i) {
tempGrid[i][col] = 1;
}
}
// Update the original game grid with the modified temp grid
game.grid = tempGrid;
gravityclear=true;
game.grid=check(game.grid, game.power_up,&(game.current_score));
gravityclear=false;
}
};
}
std::cout<<"GAME FINISHED!"<<std::endl<<"No more commands."<<std::endl<<"Final grid and score:"<<std::endl;
std::cout<<endl;
print_grid(game.grid,game.current_score,highscore);
currentplayer->score=game.current_score;
game.leaderboard.insert_new_entry(currentplayer);
std::cout<<endl;
std::cout<<"Leaderboard"<<std::endl;
std::cout<<"-----------"<<endl;
game.leaderboard.print_leaderboard();
game.leaderboard.write_to_file(game.leaderboard_file_name);
return true;
}