-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.cpp
More file actions
225 lines (200 loc) · 4.9 KB
/
driver.cpp
File metadata and controls
225 lines (200 loc) · 4.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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "GridWorld.h"
using std::cin;
using std::cout;
using std::string;
/**
* simple driver program exercising some of the
* member functions in GridWorld.
*
*/
enum CmdResult { failure, success, quit };
/* prints contens of an integer vector */
void pvec(vector<int> *v) {
cout << " [ ";
for (int x : *v) {
cout << x << " ";
}
cout << "]\n";
}
void arg_err(const string &cmd, int correct_nargs) {
cout << "usage error: " << cmd << " expects " << correct_nargs
<< " argument(s)\n";
}
/* function for evaluating/applying a command:
*
* cmd is a command name
* args[] is an array of nargs integer arguments
* gw is the GridWorld to which the command is applied if possible.
*
* returns:
* success / failure depending whether the command exists, has correct
* number of parameters and if/when invoked on the gw object, the success
* or failure operation.
*
* Or returns quit if the "quit" command was issued.
*
*/
CmdResult eval_cmd(GridWorld *gw, const string &cmd, int args[], int nargs) {
if (cmd == "birth") {
int id;
if (nargs != 2) {
arg_err(cmd, 2);
return failure;
} else {
if (!gw->birth(args[0], args[1], id)) {
cout << " operation failed\n";
return failure;
} else {
cout << " operation succeeded\n";
cout << " PersonID: " << id << "\n";
return success;
}
}
}
if (cmd == "death" || cmd == "kill") {
if (nargs != 1) {
arg_err(cmd, 1);
return failure;
} else {
if (!gw->death(args[0])) {
cout << " operation failed\n";
return failure;
} else {
cout << " operation succeeded\n";
return success;
}
}
}
if (cmd == "move") {
if (nargs != 3) {
arg_err(cmd, 3);
return failure;
} else {
if (!gw->move(args[0], args[1], args[2])) {
cout << " operation failed\n";
return failure;
} else {
cout << " operation succeeded\n";
return success;
}
}
}
if (cmd == "members") {
if (nargs != 2) {
arg_err(cmd, 2);
return failure;
} else {
vector<int> *members;
members = gw->members(args[0], args[1]);
pvec(members);
delete members;
return success;
}
}
if (cmd == "whereis") {
if (nargs != 1) {
arg_err(cmd, 1);
return failure;
} else {
int row, col;
if (!gw->whereis(args[0], row, col)) {
cout << " operation failed\n";
return failure;
} else {
cout << " district (" << row << ", " << col << ")\n";
return success;
}
}
}
if (cmd == "pop" || cmd == "population") {
if (nargs == 0) {
cout << gw->population() << "\n";
return success;
} else if (nargs == 2) {
cout << gw->population(args[0], args[1]) << "\n";
return success;
} else {
arg_err(cmd, 0);
cout << " OR\n";
arg_err(cmd, 2);
return failure;
}
}
if (cmd == "nrows" || cmd == "num_rows") {
if (nargs != 0) {
arg_err(cmd, 0);
return failure;
} else {
cout << gw->num_rows() << "\n";
return success;
}
}
if (cmd == "ncols" || cmd == "num_cols") {
if (nargs != 0) {
arg_err(cmd, 0);
return failure;
} else {
cout << gw->num_cols() << "\n";
return success;
}
}
if (cmd == "quit") {
if (nargs != 0) {
arg_err(cmd, 0);
return failure;
}
return quit;
}
cout << "command '" << cmd << "' not supported\n";
return failure;
}
/*
* defaults to a 5x5 grid.
*
*/
int main() {
GridWorld *gw = new GridWorld(5, 5);
string line;
cout << "Welcome to the GridWorld Interactive Frontend\n";
cout << "=============================================\n";
cout << "COMMANDS:\n\n";
cout << " birth <row> <col>\n";
cout << " death <id>\n";
cout << " move <id> <targe-row> <target-col>\n";
cout << " members <row> <col>\n";
cout << " whereis <id> \n";
cout << " population \n";
cout << " population <row> <col>\n";
cout << " num_rows\n";
cout << " num_cols\n";
cout << " quit\n\n";
bool done = false;
do {
cout << "cmd> ";
// read a complete line
std::getline(std::cin, line);
// now create a "stringstream" on the line just read
std::stringstream ss(line);
string cmd;
string junk;
ss >> cmd; // extract first token as command
// up to 3 integer arguments should follow
int args[3];
int n = 0;
// extract command arguments
while (n < 3 && ss >> args[n]) {
n++;
}
// now we have the command string and have
// parsed 0-3 integer arguments into args[]
//
// let eval_cmd try to apply the command
if (eval_cmd(gw, cmd, args, n) == quit) done = true;
} while (!done && !cin.eof());
delete gw;
return 0;
}