-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
610 lines (528 loc) · 18.4 KB
/
Copy pathmain.cpp
File metadata and controls
610 lines (528 loc) · 18.4 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <memory>
#include <string>
#include "board.h"
using namespace std;
int main(int argc, char * argv[]) {
cin.exceptions(ios::eofbit);
bool showWindow = true;
bool haveSeed = false;
int seed = 0;
string scriptfile1 = "sequence1.txt"; //level1 player1's filename
string scriptfile2 = "sequence2.txt"; //level1 player2's filename
int levelHolder = 0; //store starting level1 leve2
int level1 = 0;
int level2 = 0;
bool bonus = false;
for (int i = 1; i < argc; ++i) {
if (string(argv[i]).compare("-text") == 0) {
showWindow = false;
} else if (string(argv[i]).compare("-seed") == 0) {
haveSeed = true;
seed = stoi(argv[i + 1]);
i = i + 1;
} else if (string(argv[i]).compare("-scriptfile1") == 0) {
scriptfile1 = argv[i + 1];
i = i + 1;
} else if (string(argv[i]).compare("-scriptfile2") == 0) {
scriptfile2 = argv[i + 1];
i = i + 1;
} else if (string(argv[i]).compare("-startlevel") == 0) {
level1 = stoi(argv[i + 1]);
i = i + 1;
} else if (string(argv[i]).compare("-bonus") == 0) {
bonus = true;
}
}
if (haveSeed == true) {
srand(seed);
} //####command line argument extraction done####
bool player = true; // true=player1's turn, false=player2's turn
bool readSequence = false; //true=read from sequence
int turn = 0; //inc by 1 after a player'turn finishes
string cmd = "";
int commandIndex = 1; //multiplier prefix ex.: 3left
int commandPos = -1; //the last index position of a command where its an int
vector<string> inputcommand; //vector of commands from file
int inputIndex = 0;
string inputfileName = "";
bool tmpplayer;
auto g = make_unique<Board>(showWindow);
bool gameOver = false;
bool p1hint = true;
bool p2hint = true;
bool forceDrop = false;
if (bonus && level1 > 5) {
level1 = 5;
} else if (!bonus && level1 > 4) {
level1 = 4;
} else if (level1 < 0) {
level1 = 0;
}
level2 = level1;
levelHolder = level1;
g->setUp(scriptfile1, scriptfile2, level1, level2);
g->init();
g->putBlock();
g->show(showWindow);
while (true) {
try{
forceDrop = false;
if (turn % 2 == 1) {
player = false;
} else {
player = true;
}
commandIndex = 1;
if (g->getDroptime(player) > 0) { //auto drop
cmd = "drop";
commandIndex = 1;
} else {
if (readSequence == true) { //end of file
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command to be executed. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) { //read from sequence
cmd = inputcommand[inputIndex];
inputIndex = inputIndex + 1;
} else { //read from user input
cin >> cmd;
}
//get the last index position in cmd where it's an int
for (int i = 0; i < cmd.size(); ++i) {
if (cmd[i] >= 48 && cmd[i] <= 57 ) {
commandPos = i;
} else {
break;
}
}
//convert multiplier prefix from string to int
if (commandPos > 0) {
commandIndex = stoi(cmd.substr(0, commandPos + 1));
} else if (commandPos == 0) {
commandIndex = cmd[0] - 48;
}
//extract command
cmd = cmd.substr(commandPos + 1, cmd.size());
commandPos = -1;
}
if (cmd == "lef" || cmd == "left") {
if (commandIndex == 0) {
cout << "Trivial left command" << endl;
continue;
}
for (int i = 0; i < commandIndex; ++i) {
bool check = g->validLeft(player);
if (check == false) {
break;
}
}
int downtime = 0;
if (g->getLevel(player) == 3 || g->getLevel(player) == 4 || g->getLevel(player) == 5) {
downtime = downtime + 1;
}
if (g->isHeavy(player) == true) {
downtime = downtime + 2;
}
for (int i = 0; i < downtime; ++i) {
bool check = g->validDown(player);
if (check == false) {
if (g->isHeavy(player) == true) {
forceDrop = true;
}
break;
}
}
g->show(showWindow);
} else if (cmd == "ri" || cmd == "rig" || cmd == "righ" || cmd == "right") {
if (commandIndex == 0) {
cout << "Trivial right command" << endl;
continue;
}
for (int i = 0; i < commandIndex; ++i) {
bool check = g->validRight(player);
if (check == false) {
break;
}
}
int downtime = 0;
if (g->getLevel(player) == 3 || g->getLevel(player) == 4 || g->getLevel(player) == 5) {
downtime = downtime + 1;
}
if (g->isHeavy(player) == true) {
downtime = downtime + 2;
}
for (int i = 0; i < downtime; ++i) {
bool check = g->validDown(player);
if (check == false) {
if (g->isHeavy(player) == true) {
forceDrop = true;
}
break;
}
}
g->show(showWindow);
} else if (cmd == "do" || cmd == "dow" || cmd == "down") {
if (commandIndex == 0) {
cout << "Trivial down command" << endl;
continue;
}
int downtime = commandIndex;
if (g->getLevel(player) == 3 || g->getLevel(player) == 4 || g->getLevel(player) == 5) {
downtime = downtime + 1;
}
for (int i = 0; i < downtime; ++i) {
bool check = g->validDown(player);
if (check == false) {
break;
}
}
g->show(showWindow);
} else if (cmd == "levelu" || cmd == "levelup") {
if (commandIndex == 0 ) {
cout << "Trivial levelup command." << endl;
continue;
}
int levelIncrease = 0;
string filename = "";
if (player == true) {
level1 = level1 + commandIndex;
if (bonus && level1 > 5) {
level1 = 5;
} else if (!bonus && level1 > 4) {
level1 = 4;
}
levelIncrease = level1;
filename = scriptfile1;
} else {
level2 = level2 + commandIndex;
if (bonus && level2 > 5) {
level2 = 5;
} else if (!bonus && level2 > 4) {
level2 = 4;
}
levelIncrease = level2;
filename = scriptfile2;
}
g->setLevel(levelIncrease, player, filename);
g->show(showWindow);
} else if (cmd == "leveld" || cmd == "leveldo" || cmd == "leveldow" || cmd == "leveldown") {
if (commandIndex == 0 ) {
cout << "Trivial leveldown command." << endl;
continue;
}
int levelDecrease = 0;
string filename = "";
if (player == true) {
level1 = level1 - commandIndex;
if (level1 < 0) {
level1 = 0;
}
levelDecrease = level1;
filename = scriptfile1;
} else {
level2 = level2 - commandIndex;
if (level2 < 0) {
level2 = 0;
}
levelDecrease = level2;
filename = scriptfile2;
}
g->setLevel(levelDecrease, player, filename);
g->show(showWindow);
} else if (cmd == "I" || cmd == "J" || cmd == "L" || cmd == "S" || cmd == "Z" || cmd == "T" || cmd == "O" ) {
bool validChange = g->changeBlock(cmd, player);
if (validChange == false) {
cout << "No space for the " << cmd << "-block. Keep the current Block that you have" << endl;
} else {
if ((level1 == 5 && player == true) || (level2 == 5 && player == false)) {
g->scoreDeduction(player);
}
}
g->show(showWindow);
} else if (cmd == "re" || cmd == "res" || cmd == "rest" || cmd == "resta" || cmd == "restar" || cmd == "restart") {
level1 = levelHolder;
level2 = levelHolder;
g->restart();
g->setUp(scriptfile1, scriptfile2, level1, level2);
g->init();
g->putBlock();
turn = 0;
gameOver = false;
p1hint = true;
p2hint = true;
g->show(showWindow);
} else if (cmd == "ra" || cmd == "ran" || cmd == "rand" || cmd == "rando" || cmd == "random") {
if (player) {
if (level1 == 3 || level1 == 4 || level1 == 5) {
g->changeRandom(player, "apple");
} else {
if (level2 == 3 || level2 == 4 || level2 == 5) {
g->changeRandom(player, "apple");
}
}
}
g->show(showWindow);
} else if (cmd == "n" || cmd == "no" || cmd == "nor" || cmd == "nora" || cmd == "noran" || cmd == "norand" || cmd == "norando" || cmd == "norandom") {
if (readSequence == true) {
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command to be executed. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) {
cmd = inputcommand[inputIndex];
inputIndex = inputIndex + 1;
} else {
cin >> cmd;
}
if (player) {
if (level1 == 3 || level1 == 4 || level1 == 5) {
g->changeRandom(player, cmd);
}
} else {
if (level2 == 3 || level2 == 4 || level2 == 5) {
g->changeRandom(player, cmd);
}
}
g->show(showWindow);
} else if (cmd == "cl" || cmd == "clo" || cmd == "cloc" || cmd == "clock" || cmd == "clockw" || cmd == "clockwi" || cmd == "clockwis" || cmd == "clockwise") {
if (commandIndex == 0) {
cout << "Trivial clockwise command" << endl;
continue;
}
for (int i = 0; i < commandIndex; ++i) {
bool check = g->validCW(player);
if (check == false) {
break;
}
}
int downtime = 0;
if (g->getLevel(player) == 3 || g->getLevel(player) == 4 || g->getLevel(player) == 5) {
downtime = downtime + 1;
}
for (int i = 0; i < downtime; ++i) {
bool check = g->validDown(player);
if (check == false) {
break;
}
}
g->show(showWindow);
} else if (cmd == "co" || cmd == "cou" || cmd == "coun" || cmd == "count" || cmd == "counte" || cmd == "counter" || cmd == "counterc" || cmd == "countercl" || cmd == "counterclo" || cmd == "countercloc" || cmd == "counterclock" || cmd == "counterclockw" || cmd == "counterclockwi" || cmd == "counterclockwis" || cmd == "counterclockwise") {
if (commandIndex == 0) {
cout << "Trivial counterclockwise command" << endl;
continue;
}
for (int i = 0; i < commandIndex; ++i) {
bool check = g->validCCW(player);
if (check == false) {
break;
}
}
int downtime = 0;
if (g->getLevel(player) == 3 || g->getLevel(player) == 4 || g->getLevel(player) == 5) {
downtime = downtime + 1;
}
for (int i = 0; i < downtime; ++i) {
bool check = g->validDown(player);
if (check == false) {
break;
}
}
g->show(showWindow);
} else if (cmd == "s" || cmd == "se" || cmd == "seq" || cmd == "sequ" || cmd == "seque" || cmd == "sequen" || cmd == "sequenc" || cmd == "sequence") {
string tmp = "";
if (readSequence == true) {
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command to be executed. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) {
tmp = inputcommand[inputIndex];
} else {
cin >> tmp;
readSequence = true;
}
//clear command vector and read in new commands from file
inputcommand.clear();
inputcommand.shrink_to_fit();
inputIndex = 0;
ifstream inputfile{tmp};
string commands;
while ((inputfile >> commands)) {
inputcommand.emplace_back(commands);
}
g->show(showWindow);
} else if (cmd == "h" || cmd == "hi" || cmd == "hin" || cmd == "hint") {
if (player && p1hint) {
g->show(showWindow);
g->hint(player);
p1hint = false;
} else if (player == false && p2hint) {
g->show(showWindow);
g->hint(player);
p2hint = false;
} else {
cout << "You have consumed the chance to use hint before." << endl;
}
}
if (cmd == "dr" || cmd == "dro" || cmd == "drop" || forceDrop == true) {
if (forceDrop == true) {
commandIndex = 1;
}
if (commandIndex > 0) {
if (commandIndex > 1) {
g->modifyDrop(player, commandIndex - 1);
}
bool validDrop = g->drop(player);
if (validDrop == false) {
gameOver = true;
} else {
bool callSpecial = g->checkGrid(player);
if (callSpecial == true) {
g->show(showWindow);
cout << "You now active the Special action. Available actions are:" << endl;
cout << "blind heavy force" << endl;
cout << "Please select one on your opponent by typing the action's name all in lowercase: " << endl;
if (readSequence == true) {
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) {
cmd = inputcommand[inputIndex];
inputIndex = inputIndex + 1;
} else {
while (true) {
cin >> cmd;
if (cmd == "blind" || cmd == "heavy" || cmd == "force") {
break;
} else {
cout << "Input is invalid, please input again." << endl;
}
}
}
if (cmd == "blind") {
g->changeToBlind(player);
} else if (cmd == "heavy") {
g->changeToHeavy(player);
} else if (cmd == "force") {
cout << "Please select the block that you want your opponent's current block be: " << endl;
cout << "L I O S Z T J" << endl;
if (readSequence == true) {
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) {
cmd = inputcommand[inputIndex];
inputIndex = inputIndex + 1;
} else {
while (true) {
cin >> cmd;
if (cmd == "I" || cmd == "J" || cmd == "L" || cmd == "S" || cmd == "Z" || cmd == "T" || cmd == "O" ) {
break;
} else {
cout << "Input is invalid, please input again." << endl;
}
}
}
if (player == true) {
tmpplayer = false;
} else {
tmpplayer = true;
}
bool validforce = g->changeBlock(cmd, tmpplayer);
if (validforce == false) {
gameOver = true;
}
}
}
if (g->StarTerminate(player) == true) {
gameOver = true;
}
if (gameOver == false) {
bool validSwap = g->putCurBlock(player);
//new block cannot be placed in its initial position => lose
if (validSwap == false) {
gameOver = true;
}
if (gameOver == false) {
turn = turn + 1;
int remainingDrop = g->getDroptime(player);
if (remainingDrop > 0 && commandIndex == 1) {
g->modifyDrop(player, remainingDrop - 1);
}
g->removeBlind(player);
p1hint = true;
p2hint = true;
g->show(showWindow);
}
}
}
} else {
cout << "Trivial drop command." << endl;
g->show(showWindow);
}
}
if (gameOver == true) {
cout << "No space for you to drop the block or place the next block or place a star. Game Over." << endl;
string winner = g->win();
if (winner == "Tie") {
cout << "Both player's score is same." << endl;
} else {
cout << "The person who obtains higher score in this round is: " << winner << endl;
}
cout << "Do you want to restart another round?(Y/N) ";
if (readSequence == true) {
if (inputIndex == inputcommand.size()) {
cout << "Your file doesn't have any command to executed. Please mannually input any commands." << endl;
readSequence = false;
}
}
if (readSequence == true) {
cmd = inputcommand[inputIndex];
inputIndex = inputIndex + 1;
} else {
while (true) {
cin >> cmd;
if (cmd == "N" || cmd == "Y") {
break;
} else {
cout << "Invalid input. Please input again." << endl;
}
}
}
if (cmd == "N") {
break;
} else {
level1 = levelHolder;
level2 = levelHolder;
g->restart();
g->setUp(scriptfile1, scriptfile2, level1, level2);
g->init();
g->putBlock();
turn = 0;
p1hint = true;
p2hint = true;
gameOver = false;
g->show(showWindow);
}
}
} catch (exception &) { break; }
}
string finalwinner = g->finalWin();
if (finalwinner == "Tie") {
cout << "Both of players' highestscores are same. Tie" << endl;
} else {
cout << "This highest score winner is: " << finalwinner << endl;
}
}