-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockFall.cpp
217 lines (179 loc) · 6.46 KB
/
BlockFall.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
#include "BlockFall.h"
#include <fstream>
#include <iostream>
#include <sstream>
//OG:
BlockFall::BlockFall(string grid_file_name, string blocks_file_name, bool gravity_mode_on, const string &leaderboard_file_name, const string &player_name) : gravity_mode_on(
gravity_mode_on), leaderboard_file_name(leaderboard_file_name), player_name(player_name) {
initialize_grid(grid_file_name);
read_blocks(blocks_file_name);
leaderboard.read_from_file(leaderboard_file_name);
}
std::vector<std::vector<bool>> BlockFall::turntoVector(std::string blocktext) {
std::vector<vector<bool>> vector;
std::vector<bool> row;
bool num=false;
for (char c : blocktext) {
if (c == '0' || c == '1') {
num=true;
bool value = (c == '1');
row.push_back(value);
} else if (c == '\n') {
if (num){
vector.push_back(row);
row.clear();}
}
}
//Because otherwise the last row won't be added :
if (!row.empty()) {
vector.push_back(row);
}
return vector;
};
//Left rotate
vector<vector<bool>> BlockFall::transpose(vector<vector<bool>> inputVector) {
vector<vector<bool>> rotatedMatrix(inputVector[0].size(), std::vector<bool>(inputVector.size()));
for (int i = 0; i < inputVector.size(); ++i) {
for (int j = 0; j < inputVector[0].size(); ++j) {
rotatedMatrix[inputVector[0].size() - 1 - j][i] = inputVector[i][j];
}
}
return rotatedMatrix;
}
void BlockFall::setupRotations(Block* current) {
Block* left = new Block;
left->shape = transpose(current->shape);
if (*current!= *left) {
// More than one rotation
current->left_rotation = left;
left->right_rotation = current;
Block* left2 = new Block;
left2->shape = transpose(left->shape);
if (*current != *left2) {
// Four unique rotations
Block* left3 = new Block;
left3->shape = transpose(left2->shape);
left->left_rotation = left2;
left2->left_rotation = left3;
left3->left_rotation = current;
current->right_rotation = left3;
left2->right_rotation = left;
left3->right_rotation = left2;
} else {
// Two rotations
delete left2;
left->left_rotation = current;
current->right_rotation = left;
}
} else {
// Only one rotation
delete left;
current->left_rotation = current;
current->right_rotation = current;
}
}
//Notice there are no 3 unique rotations, there is no such thing.
void BlockFall::read_blocks(const string &input_file) {
std::ifstream blocksdata(input_file);
if (!blocksdata.is_open()) {
std::cerr << "Error while opening Blocks data file...";
return;
}
std::string line, stringdata;
while (blocksdata >> line) {
stringdata = stringdata + line + "\n";
}
int startint = stringdata.find('[');
int endint = stringdata.find(']');
std::string firstBlockString = stringdata.substr(startint + 1, endint - startint - 1);
stringdata.erase(startint, endint - startint + 1);
initial_block = new Block;
initial_block->shape = turntoVector(firstBlockString);
setupRotations(initial_block);
Block* current = initial_block;
// Iterate through the remaining blocks
while ((startint = stringdata.find('[')) != std::string::npos &&
(endint = stringdata.find(']')) != std::string::npos) {
std::string blockString = stringdata.substr(startint + 1, endint - startint - 1);
stringdata.erase(startint, endint - startint + 1);
current->next_block = new Block;
current->next_block->shape = turntoVector(blockString);
current->right_rotation->next_block = current->next_block;
current->right_rotation->right_rotation->next_block = current->next_block;
current->right_rotation->right_rotation->right_rotation->next_block = current->next_block;
setupRotations(current->next_block);
current = current->next_block;
char searchChar = '[';
int count = 0;
for (int it = 0; it != stringdata.size(); ++it) {
if (stringdata[it] == searchChar) {
count++;
}
}
// Check if there is only one '[' in the string
if (count == 1) {
// Erase '[]' characters
size_t found = stringdata.find('[');
if (found != std::string::npos) {
stringdata.erase(found, 1);
}
found = stringdata.find(']');
if (found != std::string::npos) {
stringdata.erase(found, 1);
}
power_up = turntoVector(stringdata);
}
}
active_rotation=initial_block;
}
void BlockFall::initialize_grid(const string &input_file) {
//First off: Opening the file:
std::ifstream griddata(input_file);
if(!griddata.is_open()){
std::cerr<<"Error while opening Grid data file...";
return;
}
std::string line;
//Line by line, we insert into the vector:
while (std::getline(griddata, line)) {
std::istringstream iss(line);
std::vector<int> row;
int cell;
while (iss >> cell) {
row.push_back(cell);
}
grid.push_back(row);
}
rows = static_cast<int>(grid.size());
if (rows == 0) {
std::cerr << "Empty grid provided....." << std::endl;
return;
}
cols = static_cast<int>(grid[0].size());
// DONE: Initialize "rows" and "cols" member variables
// DONE: Initialize "grid" member variable using the command-line argument 1 in main
}
//Destructor
BlockFall::~BlockFall() {
while (initial_block != nullptr) {
Block* nextBlock = initial_block->next_block;
Block* current = initial_block;
Block* left = current->left_rotation;
Block* left2 = current->left_rotation->left_rotation;
Block* left3 = current->right_rotation;
if (current == left) {
delete current;
} else {
if (current == left2) {
delete left;
delete current;
} else {
delete current;
delete left;
delete left2;
delete left3;
}
}
initial_block = nextBlock;
}
}