Repository navigation
Expand file tree
/
Copy pathcommand-impl.cc
More file actions
361 lines (298 loc) ยท 11.9 KB
/
Copy pathcommand-impl.cc
File metadata and controls
361 lines (298 loc) ยท 11.9 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
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
module command;
import <iostream>;
import <string>;
import <map>;
import <vector>;
import <memory>;
import <algorithm>;
import <cctype>;
import <fstream>;
import game;
import board;
import level;
import constants;
using namespace GameConstants;
// Command base class implementation
bool Command::canMultiply() const { return true; }
// LeftCommand implementation
void LeftCommand::execute(Game* game) {
Board* board = game->getCurrentBoard();
board->moveLeft();
// Apply heavy effect if active (from level or special action)
// Level heavy = 1 drop, Heavy effect = 2 drops, they add together
Level* level = game->getCurrentLevel();
int heavyDrops = 0;
if (level->isHeavy()) heavyDrops += 1;
if (board->hasHeavyEffect()) heavyDrops += HEAVY_EXTRA_DROP;
for (int i = 0; i < heavyDrops; ++i) {
board->moveDown();
}
}
// RightCommand implementation
void RightCommand::execute(Game* game) {
Board* board = game->getCurrentBoard();
board->moveRight();
// Apply heavy effect if active (from level or special action)
// Level heavy = 1 drop, Heavy effect = 2 drops, they add together
Level* level = game->getCurrentLevel();
int heavyDrops = 0;
if (level->isHeavy()) heavyDrops += 1;
if (board->hasHeavyEffect()) heavyDrops += HEAVY_EXTRA_DROP;
for (int i = 0; i < heavyDrops; ++i) {
board->moveDown();
}
}
// DownCommand implementation
void DownCommand::execute(Game* game) {
Board* board = game->getCurrentBoard();
board->moveDown();
// Apply heavy effect if active (from level or special action)
// Level heavy = 1 drop, Heavy effect = 2 drops, they add together
Level* level = game->getCurrentLevel();
int heavyDrops = 0;
if (level->isHeavy()) heavyDrops += 1;
if (board->hasHeavyEffect()) heavyDrops += HEAVY_EXTRA_DROP;
for (int i = 0; i < heavyDrops; ++i) {
board->moveDown();
}
}
// DropCommand implementation
void DropCommand::execute(Game* game) {
game->drop();
}
// RotateClockwiseCommand implementation
void RotateClockwiseCommand::execute(Game* game) {
Board* board = game->getCurrentBoard();
board->rotate(true);
// Apply heavy effect if active (from level or special action)
// Level heavy = 1 drop, Heavy effect = 2 drops, they add together
Level* level = game->getCurrentLevel();
int heavyDrops = 0;
if (level->isHeavy()) heavyDrops += 1;
if (board->hasHeavyEffect()) heavyDrops += HEAVY_EXTRA_DROP;
for (int i = 0; i < heavyDrops; ++i) {
board->moveDown();
}
}
// RotateCounterClockwiseCommand implementation
void RotateCounterClockwiseCommand::execute(Game* game) {
Board* board = game->getCurrentBoard();
board->rotate(false);
// Apply heavy effect if active (from level or special action)
// Level heavy = 1 drop, Heavy effect = 2 drops, they add together
Level* level = game->getCurrentLevel();
int heavyDrops = 0;
if (level->isHeavy()) heavyDrops += 1;
if (board->hasHeavyEffect()) heavyDrops += HEAVY_EXTRA_DROP;
for (int i = 0; i < heavyDrops; ++i) {
board->moveDown();
}
}
// LevelUpCommand implementation
void LevelUpCommand::execute(Game* game) {
game->levelUp();
}
// LevelDownCommand implementation
void LevelDownCommand::execute(Game* game) {
game->levelDown();
}
// RandomCommand implementation
void RandomCommand::execute(Game* game) {
game->getCurrentLevel()->setRandom(true);
}
bool RandomCommand::canMultiply() const { return false; }
// NoRandomCommand implementation
void NoRandomCommand::execute(Game* game) {
std::string filename;
std::cin >> filename;
game->getCurrentLevel()->setRandom(false);
game->getCurrentLevel()->setNonRandom(filename);
}
bool NoRandomCommand::canMultiply() const { return false; }
// RestartCommand implementation
void RestartCommand::execute(Game* game) {
game->restart();
}
bool RestartCommand::canMultiply() const { return false; }
// SequenceCommand implementation
void SequenceCommand::execute(Game* game) {
// Actual execution handled in CommandInterpreter
}
bool SequenceCommand::canMultiply() const { return false; }
// IBlockCommand implementation
void IBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('I');
}
bool IBlockCommand::canMultiply() const { return false; }
// JBlockCommand implementation
void JBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('J');
}
bool JBlockCommand::canMultiply() const { return false; }
// LBlockCommand implementation
void LBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('L');
}
bool LBlockCommand::canMultiply() const { return false; }
// OBlockCommand implementation
void OBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('O');
}
bool OBlockCommand::canMultiply() const { return false; }
// SBlockCommand implementation
void SBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('S');
}
bool SBlockCommand::canMultiply() const { return false; }
// ZBlockCommand implementation
void ZBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('Z');
}
bool ZBlockCommand::canMultiply() const { return false; }
// TBlockCommand implementation
void TBlockCommand::execute(Game* game) {
game->getCurrentBoard()->replaceCurrentBlock('T');
}
bool TBlockCommand::canMultiply() const { return false; }
// HelpCommand implementation
void HelpCommand::execute(Game*) {
std::cout << "โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n";
std::cout << "โ BIQUADRIS COMMANDS HELP โ\n";
std::cout << "โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ\n";
std::cout << "โ MOVEMENT: โ\n";
std::cout << "โ left/lef - Move block left โ\n";
std::cout << "โ right/ri - Move block right โ\n";
std::cout << "โ down/do - Move block down โ\n";
std::cout << "โ drop/dr - Drop block โ\n";
std::cout << "โ โ\n";
std::cout << "โ ROTATION: โ\n";
std::cout << "โ clockwise/cw - Rotate clockwise โ\n";
std::cout << "โ counterclockwise/cc - Rotate CCW โ\n";
std::cout << "โ โ\n";
std::cout << "โ LEVEL: โ\n";
std::cout << "โ levelup/levelu - Increase level โ\n";
std::cout << "โ leveldown/leveld - Decrease level โ\n";
std::cout << "โ โ\n";
std::cout << "โ MODE: โ\n";
std::cout << "โ random/r - Random blocks โ\n";
std::cout << "โ norandom/nor - Use sequence file โ\n";
std::cout << "โ โ\n";
std::cout << "โ TESTING: โ\n";
std::cout << "โ I,J,L,O,S,Z,T - Replace block โ\n";
std::cout << "โ sequence file - Run commands from fileโ\n";
std::cout << "โ โ\n";
std::cout << "โ GAME: โ\n";
std::cout << "โ restart - Restart game โ\n";
std::cout << "โ help/h - Show this help โ\n";
std::cout << "โ โ\n";
std::cout << "โ TIP: Use numbers before commands! โ\n";
std::cout << "โ Example: 3left, 2down โ\n";
std::cout << "โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n\n";
}
bool HelpCommand::canMultiply() const { return false; }
// CommandInterpreter implementation
CommandInterpreter::CommandInterpreter(Game* g) : game(g) {
registerCommands();
}
void CommandInterpreter::registerCommands() {
commands["left"] = std::make_unique<LeftCommand>();
commands["right"] = std::make_unique<RightCommand>();
commands["down"] = std::make_unique<DownCommand>();
commands["drop"] = std::make_unique<DropCommand>();
commands["clockwise"] = std::make_unique<RotateClockwiseCommand>();
commands["cw"] = std::make_unique<RotateClockwiseCommand>();
commands["counterclockwise"] = std::make_unique<RotateCounterClockwiseCommand>();
commands["ccw"] = std::make_unique<RotateCounterClockwiseCommand>();
commands["levelup"] = std::make_unique<LevelUpCommand>();
commands["leveldown"] = std::make_unique<LevelDownCommand>();
commands["random"] = std::make_unique<RandomCommand>();
commands["norandom"] = std::make_unique<NoRandomCommand>();
commands["restart"] = std::make_unique<RestartCommand>();
commands["sequence"] = std::make_unique<SequenceCommand>();
commands["help"] = std::make_unique<HelpCommand>();
commands["I"] = std::make_unique<IBlockCommand>();
commands["J"] = std::make_unique<JBlockCommand>();
commands["L"] = std::make_unique<LBlockCommand>();
commands["O"] = std::make_unique<OBlockCommand>();
commands["S"] = std::make_unique<SBlockCommand>();
commands["Z"] = std::make_unique<ZBlockCommand>();
commands["T"] = std::make_unique<TBlockCommand>();
}
std::string CommandInterpreter::matchCommand(const std::string& prefix) {
std::vector<std::string> matches;
for (const auto& [name, cmd] : commands) {
if (name.substr(0, prefix.size()) == prefix) {
matches.push_back(name);
}
}
// Return the match if it's unique
if (matches.size() == 1) {
return matches[0];
}
// If exact match exists, use it
if (commands.count(prefix)) {
return prefix;
}
return ""; // No unique match
}
void CommandInterpreter::executeCommand(const std::string& input) {
// Parse multiplier and command
int multiplier = 1;
std::string commandStr = input;
// Check for multiplier prefix
size_t i = 0;
while (i < input.size() && std::isdigit(input[i])) {
i++;
}
if (i > 0) {
multiplier = std::stoi(input.substr(0, i));
commandStr = input.substr(i);
}
// Match command
std::string fullCommand = matchCommand(commandStr);
if (fullCommand.empty() || !commands.count(fullCommand)) {
std::cout << "Invalid command, use 'help' or 'h' for a list of commands" << "\n";
return;
}
Command* cmd = commands[fullCommand].get();
// Special handling for sequence command
if (fullCommand == "sequence") {
std::string filename;
std::cin >> filename;
executeSequenceFile(filename);
return; // Don't render yet, sequence commands will render
}
// Execute command (respecting multiplier if allowed)
bool gameWasRestarted = false;
if (cmd->canMultiply()) {
for (int j = 0; j < multiplier; ++j) {
cmd->execute(game);
// Stop executing if game restarted due to game over
if (game->shouldStopExecutingCommands()) {
gameWasRestarted = true;
break;
}
}
// Clear the stop flag after processing
game->clearStopExecutionFlag();
} else {
cmd->execute(game);
}
// Note: Drop command switches players (unless game was restarted)
if (fullCommand == "drop" && !gameWasRestarted) {
game->switchPlayer();
}
// Render after command execution
game->render();
}
void CommandInterpreter::executeSequenceFile(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Error: Could not open sequence file: " << filename << "\n";
return;
}
std::string command;
while (file >> command) {
executeCommand(command);
}
}