-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cc
347 lines (319 loc) · 13.6 KB
/
game.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
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
#include "game.h"
#include <cassert>
#include <sstream>
#include <string>
using namespace std;
Game::Game(bool isGraphics, int seed, string file1, string file2, int startLevel, bool bonusEnabled)
: isGraphics { isGraphics }
, bonusEnabled { bonusEnabled }
, seed { seed }
, file1 { file1 }
, file2 { file2 }
, startLevel { startLevel }
{
player1 = make_unique<Player>(file1);
player2 = make_unique<Player>(file2);
this->seed = seed;
this->startLevel = startLevel;
currentPlayer = player1.get();
turn_count = 0;
if (isGraphics) {
window = make_unique<Window>(bonusEnabled);
window->setQueue(player1->q.get());
}
}
void Game::restart()
{
player1 = make_unique<Player>(file1);
player2 = make_unique<Player>(file2);
player1->setLevel(startLevel);
player2->setLevel(startLevel);
player1->blockFactory->setSeed(seed);
player2->blockFactory->setSeed(seed + 1);
player1->gameBoard.currentBlock = player1->blockFactory->getNext(player1->effect);
player2->gameBoard.currentBlock = player2->blockFactory->getNext(player2->effect);
player1->gameBoard.nextBlock = player1->blockFactory->getNext(player1->effect);
player2->gameBoard.nextBlock = player2->blockFactory->getNext(player2->effect);
currentPlayer = player1.get();
turn_count = 0;
printGame();
if (isGraphics) {
renderGame();
window->setQueue(player1->q.get());
}
}
void Game::renderGame() {
if(!isGraphics)
return;
unique_ptr<Window::RenderData> d(new Window::RenderData{ player1->gameBoard.toString(true, bonusEnabled, player1->effect % 3 == 0),
player2->gameBoard.toString(true, bonusEnabled, player2->effect % 3 == 0),
player1->score, player2->score,
player1->level, player2->level,
currentPlayer == player1.get() ? player1->gameBoard.nextBlock.get() : nullptr,
currentPlayer == player2.get() ? player2->gameBoard.nextBlock.get() : nullptr,
player1->gameBoard.ROWS, player1->gameBoard.COLS,
highScore });
window->renderGame(std::move(d));
}
void Game::startGame()
{
if (turn_count != 0)
return;
player1->setLevel(startLevel);
player2->setLevel(startLevel);
player1->blockFactory->setSeed(seed);
player2->blockFactory->setSeed(seed + 1);
player1->gameBoard.currentBlock = player1->blockFactory->getNext(player1->effect);
player2->gameBoard.currentBlock = player2->blockFactory->getNext(player2->effect);
player1->gameBoard.nextBlock = player1->blockFactory->getNext(player1->effect);
player2->gameBoard.nextBlock = player2->blockFactory->getNext(player2->effect);
isRunning = true;
printGame();
renderGame();
textThread = thread(&Game::textInput, this);
mainThread = thread(&Game::runMainLoop, this);
if (isGraphics) {
window->startDisplay();
}
else {
mainThread.join();
}
}
void Game::printGame()
{
assert(player1 != nullptr);
assert(player2 != nullptr);
assert(currentPlayer != nullptr);
assert(player1->gameBoard.COLS == player2->gameBoard.COLS);
assert(player1->gameBoard.ROWS == player2->gameBoard.ROWS);
string player1Board = player1->gameBoard.toString(true, false, player1->effect % 3 == 0);
string player2Board = player2->gameBoard.toString(true, false, player2->effect % 3 == 0);
assert(player1Board.length() == player2Board.length());
cout << "\t "
<< "Highscore: " << highScore << endl;
cout << "-----------------------------------" << endl;
cout << "Level:\t" << player1->level << "\t\t"
<< "Level:\t" << player2->level << endl;
cout << "Score:\t" << player1->score << "\t\t"
<< "Score:\t" << player2->score << endl;
cout << "-----------\t\t-----------" << endl;
for (int i = 0; i < player1->gameBoard.ROWS; ++i) {
cout << player1Board.substr(i * player1->gameBoard.COLS, player1->gameBoard.COLS) << "\t\t" << player2Board.substr(i * player2->gameBoard.COLS, player2->gameBoard.COLS) << endl;
}
cout << "-----------\t\t-----------" << endl;
cout << "Next:\t\t\tNext:" << endl;
if (currentPlayer == player1.get()) {
player1->gameBoard.nextBlock->printBlock(true);
} else if (currentPlayer == player2.get()) {
player2->gameBoard.nextBlock->printBlock(false);
}
}
void Game::textInput()
{
string command;
while (isRunning) {
cin >> command;
std::istringstream ss { command };
int multiplier;
if (ss >> multiplier) {
if (multiplier < 0) {
multiplier = 1;
}
} else {
multiplier = 1;
}
ss >> command;
int count = 0;
string match;
for (auto& c : COMMANDS) {
if (c.substr(0, command.length()) == command) {
count++;
match = c;
}
}
if (count == 1) {
command = match;
}
Queue* q = (currentPlayer == player1.get() ? player1->q.get() : player2->q.get());
if (command == "quit") {
q->push("quit");
break; // break out of while loop
}
if (command == "norandom" || command == "sequence") {
string filename;
cin >> filename;
q->push(command); // push the command and filename to the queue
q->push(filename);
continue;
}
if (command == "random" || command == "restart" || command == "hint") {
q->push(command); // multipliers don't work for these commands
continue;
}
for (int i = 0; i < multiplier; ++i) {
q->push(command); // push the command to the queue multiplier times
}
}
}
void Game::runMainLoop()
{
while (isRunning) {
cout << (player1.get() == currentPlayer ? "Player 1" : "Player 2") << "'s turn" << endl;
string command = (player1.get() == currentPlayer ? player1->q : player2->q)->pop();
// Check if game is over
if (!player1->gameBoard.validBoard()) {
cout << "Player 2 wins!" << endl;
restart();
continue;
} else if (!player2->gameBoard.validBoard()) {
cout << "Player 1 wins!" << endl;
restart();
continue;
}
if (command == "left") {
currentPlayer->gameBoard.left(currentPlayer->effect % 2 == 0);
}
else if (command == "right") {
currentPlayer->gameBoard.right(currentPlayer->effect % 2 == 0);
}
else if (command == "down") {
currentPlayer->gameBoard.down(currentPlayer->effect % 2 == 0);
}
else if (command == "clockwise") {
currentPlayer->gameBoard.clockwise();
} else if (command == "counterclockwise") {
currentPlayer->gameBoard.counterClockwise();
}
else if (command == "drop") {
currentPlayer->effect = 1;
currentPlayer->gameBoard.drop(); // next block is now nullptr
currentPlayer->gameBoard.nextBlock = currentPlayer->blockFactory->getNext(currentPlayer->effect); // no longer nullptr
int linesCleared = currentPlayer->clearRow();
cout << "Lines cleared: " << linesCleared << endl;
if (linesCleared && isGraphics)
window->playSound(2);
else if (isGraphics)
window->playSound(0);
highScore = currentPlayer->score > highScore ? currentPlayer->score : highScore;
currentPlayer = currentPlayer == player1.get() ? player2.get() : player1.get();
if (isGraphics) window->setQueue(currentPlayer->q.get());
turn_count++;
if (currentPlayer->level == 4){
currentPlayer->gameBoard.turn_count++;
}
if (currentPlayer->level == 4 && currentPlayer->gameBoard.turn_count % 5 == 0){
if (currentPlayer->score == currentPlayer->score5turnsago) {
currentPlayer->gameBoard.dropStar();
} else{
currentPlayer->score5turnsago = currentPlayer->score;
}
}
if (linesCleared >= 2) {
if (isGraphics) {
renderGame();
}
printGame();
cout << "Special action triggered! Enter your desired effect:" << endl;
string effect = (player1.get() == currentPlayer ? player1->q : player2->q)->pop();
while (true) {
if (effect == "heavy") {
currentPlayer == player1.get() ? player1->effect *= 2 : player2->effect *= 2;
break;
} else if (effect == "blind") {
currentPlayer == player1.get() ? player1->effect *= 3 : player2->effect *= 3;
break;
} else if (effect == "force") {
string block = (player1.get() == currentPlayer ? player1->q : player2->q)->pop();
if (block == "I") {
currentPlayer->gameBoard.currentBlock = make_unique<IBlock>(currentPlayer->effect);
} else if (block == "J") {
currentPlayer->gameBoard.currentBlock = make_unique<JBlock>(currentPlayer->effect);
} else if (block == "L") {
currentPlayer->gameBoard.currentBlock = make_unique<LBlock>(currentPlayer->effect);
} else if (block == "O") {
currentPlayer->gameBoard.currentBlock = make_unique<OBlock>(currentPlayer->effect);
} else if (block == "S") {
currentPlayer->gameBoard.currentBlock = make_unique<SBlock>(currentPlayer->effect);
} else if (block == "Z") {
currentPlayer->gameBoard.currentBlock = make_unique<ZBlock>(currentPlayer->effect);
} else if (block == "T") {
currentPlayer->gameBoard.currentBlock = make_unique<TBlock>(currentPlayer->effect);
}
break;
}
effect = (player1.get() == currentPlayer ? player1->q : player2->q)->pop();
}
continue;
}
}
else if (command == "levelup") {
if(currentPlayer->setLevel(currentPlayer->level + 1))
currentPlayer->gameBoard.nextBlock = currentPlayer->blockFactory->getNext(currentPlayer->effect);
} else if (command == "leveldown") {
if(currentPlayer->setLevel(currentPlayer->level - 1))
currentPlayer->gameBoard.nextBlock = currentPlayer->blockFactory->getNext(currentPlayer->effect);
} else if (command == "I") {
currentPlayer->gameBoard.currentBlock = make_unique<IBlock>(currentPlayer->effect);
} else if (command == "J") {
currentPlayer->gameBoard.currentBlock = make_unique<JBlock>(currentPlayer->effect);
} else if (command == "L") {
currentPlayer->gameBoard.currentBlock = make_unique<LBlock>(currentPlayer->effect);
} else if (command == "O") {
currentPlayer->gameBoard.currentBlock = make_unique<OBlock>(currentPlayer->effect);
} else if (command == "S") {
currentPlayer->gameBoard.currentBlock = make_unique<SBlock>(currentPlayer->effect);
} else if (command == "T") {
currentPlayer->gameBoard.currentBlock = make_unique<TBlock>(currentPlayer->effect);
} else if (command == "Z") {
currentPlayer->gameBoard.currentBlock = make_unique<ZBlock>(currentPlayer->effect);
} else if (command == "restart") {
restart();
continue;
} else if (command == "quit") {
endGame();
break;
} else if (command == "sequence") {
Queue *q = player1.get() == currentPlayer ? player1->q.get() : player2->q.get();
string filename = q->pop();
ifstream file(filename);
string line;
while (file >> line) {
q->push(line);
if (line == "drop") {
q = q == player1->q.get() ? player2->q.get() : player1->q.get();
}
}
} else if (command == "random") {
currentPlayer->blockFactory->setRandom(true);
} else if (command == "norandom") {
string filename = (player1.get() == currentPlayer ? player1->q : player2->q)->pop();
currentPlayer->blockFactory->setRandom(false, filename);
} else if (command == "hint") {
cerr << "Rust is better than C++" << endl;
}
if (isGraphics) {
renderGame();
}
printGame();
}
}
void Game::endGame()
{
if (isGraphics)
window->quit = true;
isRunning = false;
try {
if (textThread.joinable())
textThread.join();
if (mainThread.joinable())
mainThread.join();
} catch (...) {
// Many things can go wrong here, especially if the mutex is still locked
// We don't care about any of them, the game is over and we just want to kill the threads
}
exit(0);
}
Game::~Game()
{
endGame();
}