-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadris.cc
173 lines (159 loc) · 4.71 KB
/
quadris.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
#include <string>
#include <iostream>
#include <sstream>
#include "quadris.h"
using namespace std;
Quadris::Quadris(int argc, char *argv[]): text{false}, seed{0}, script_file{"sequence.txt"}, start_level{0}, hold{false}, ai{false} {
for (int i = 1; i < argc; ++i) {
string arg(argv[i]);
if (arg == "-text") {
text = true;
}
else if (arg == "-seed") {
++i;
stringstream(argv[i]) >> seed;
}
else if (arg == "-scriptfile") {
++i;
script_file = string(argv[i]);
}
else if (arg == "-startlevel") {
++i;
stringstream(argv[i]) >> start_level;
}
else if (arg == "-h" || arg == "--help")
usage();
else if (arg == "-hold")
hold = true;
else if (arg == "-ai")
ai = true;
else {
cout << "**INVALID OPTION**" << endl;
usage();
}
}
init();
}
void Quadris::start() {
string cmd;
int mult;
bool last_hint = false;
cout << *model;
while (cin >> cmd) {
mult = 1;
if (last_hint) {
last_hint = false;
model->clearHint();
}
if (47 < cmd[0] && cmd[0] < 58) {
string num = "";
int i = 0;
while (47 < cmd[i] && cmd[i] < 58) {
num += cmd[i];
++i;
}
stringstream(num) >> mult;
cmd = cmd.substr(i, cmd.size());
}
if (cmdMatch(cmd, 1, "sequence")) {
cin >> script_file;
model->setSeqFile(script_file);
}
else if (cmdMatch(cmd, 1, "norandom"))
model->setRandom(false);
else if (cmdMatch(cmd, 2, "random"))
model->setRandom(true);
else if (cmdMatch(cmd, 2, "restart"))
init();
else if (cmdMatch(cmd, 2, "hint")) {
last_hint = true;
model->getHint();
} else if (hold && cmdMatch(cmd, 2, "hold"))
model->hold();
else if (cmdMatch(cmd, 6, "levelup"))
model->levelUp(mult);
else if (cmdMatch(cmd, 6, "leveldown"))
model->levelDown(mult);
else if (cmdMatch(cmd, 3, "left"))
model->left(mult);
else if (cmdMatch(cmd, 2, "right"))
model->right(mult);
else if (cmdMatch(cmd, 2, "down"))
model->down(mult);
else if (cmdMatch(cmd, 2, "drop"))
model->drop(mult);
else if (cmdMatch(cmd, 2, "clockwise"))
model->clockwise(mult);
else if (cmdMatch(cmd, 2, "counterclockwise"))
model->cclockwise(mult);
else if (ai && cmdMatch(cmd, 1, "auto"))
model->automate(mult);
else if (cmd == "I")
model->swapType(BlockType::IBlock);
else if (cmd == "J")
model->swapType(BlockType::JBlock);
else if (cmd == "L")
model->swapType(BlockType::LBlock);
else if (cmd == "O")
model->swapType(BlockType::OBlock);
else if (cmd == "S")
model->swapType(BlockType::SBlock);
else if (cmd == "Z")
model->swapType(BlockType::ZBlock);
else if (cmd == "T")
model->swapType(BlockType::TBlock);
else
cout << "Invalid or ambiguous command" << endl;
if (model->isOver()) {
cout << "Game Over! You scored " << model->getScore() << " points." << endl;
init();
}
cout << *model;
}
}
bool Quadris::cmdMatch(string in, int min, string cmd) {
if (in.length() < min || in.length() > cmd.length())
return false;
for (int i = 0; i < in.length(); ++i) {
if (in[i] != cmd[i])
return false;
}
return true;
}
void Quadris::init() {
model = make_unique<QuadrisModel>(text, seed, script_file, start_level, hold);
}
void Quadris::usage() {
cout << endl <<
"QUADRIS. Made by Ryan Quanz and Jonathan Donas.\n"
"Usage: ./quadris [options]\n\n"
"Options:\n"
" -text Run in text-only mode\n"
" -seed [seed] Set the random number generator's seed\n"
" -scriptfile [file] Use [file] instead of the default sequence.txt\n"
" -startlevel [n] Start game in level [n]\n"
" -h or --help Display this help message\n"
" -hold Play with the hold feature (bonus!)\n"
" -ai Enable AI feature\n\n"
"In-game commands:\n"
" left\n"
" right\n"
" down\n"
" clockwise\n"
" counterclockwise\n"
" drop\n"
" levelup\n"
" leveldown\n"
" restart\n"
" norandom Make levels 3 and 4 read from input file\n"
" random Make levels 3 and 4 random (default)\n"
" sequence [file] Change sequence file\n"
" I, J, L, etc. Replace current block with specified block\n"
" hold Holds the current block for later use\n"
" hint Display a hint\n"
" auto Makes the best move for you\n\n"
"Commands can be shortened (eg. counterclockwise -> co)\n"
"and executed multiple times (eg. 2left)\n"
<< endl;
exit(1);
}