-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
executable file
·472 lines (401 loc) · 11.5 KB
/
Copy pathGame.cpp
File metadata and controls
executable file
·472 lines (401 loc) · 11.5 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
/*
* Copyright 2023 University of Michigan EECS183
*
* Game.cpp
* Project UID 848fee0125dbb5eb53ed294f20dbef81
*
* <#Names#>
* <#Uniqnames#>
*
* Final Project - Elevators
*/
#include <random>
#include <sstream>
#include "Game.h"
#include "AI.h"
#include "Utility.h"
using namespace std;
// Stub for playGame for Core, which plays random games
// You *must* revise this function according to the RME and spec
// Code that will not appear in your solution is noted in the comments
void Game::playGame(bool isAIModeIn, ifstream& gameFile) {
/* used to generate random numbers for spawning a randon Person
* these three statements will not be needed when you have
* finished this function */
std::mt19937 gen(1);
std::uniform_int_distribution<> floorDist(0, 9);
std::uniform_int_distribution<> angerDist(0, 3);
// initialize the game
isAIMode = isAIModeIn;
printGameStartPrompt();
initGame(gameFile);
/* play until checkForGameEnd() stops the program
* you *will* modify this loop
*/
while (true) {
// random floor and targetFloor
// these two statements are not needed in the finished solution
int src = floorDist(gen);
int dst = floorDist(gen);
/* check that the randomly generate floor and targetFloor differ
* none of this if statement will appear in your finished solution
* Persons will be read from the file instead */
if (src != dst) {
std::stringstream ss;
ss << "0f" << src << "t" << dst << "a" << angerDist(gen);
Person p(ss.str());
building.spawnPerson(p);
}
// print the state of the Building and check for end of game
building.prettyPrintBuilding(cout);
satisfactionIndex.printSatisfaction(cout, false);
checkForGameEnd();
// get and apply the next move
Move nextMove = getMove();
update(nextMove);
}
}
// Stub for isValidPickupList for Core
// You *must* revise this function according to the RME and spec
bool Game::isValidPickupList(const string& pickupList,
const int pickupFloorNum) const {
return true;
}
//////////////////////////////////////////////////////
////// DO NOT MODIFY ANY CODE BENEATH THIS LINE //////
//////////////////////////////////////////////////////
bool Game::performMove(Move& move) const
{
Elevator elevators[NUM_ELEVATORS];
for (int i = 0; i < NUM_ELEVATORS; ++i)
{
elevators[i] = building.getElevatorById(i);
}
if (move.isValidMove(elevators) && move.isPickupMove())
{
Elevator taggedElevator = building.getElevatorById(move.getElevatorId());
Floor taggedFloor =
building.getFloorByFloorNum(taggedElevator.getCurrentFloor());
if (taggedFloor.getNumPeople() > 0)
{
getPeopleToPickup(move);
return true;
}
return false;
}
else if (move.isSaveMove())
{
performSave();
}
else if (move.isQuitMove())
{
performQuit();
}
else if (!move.isValidMove(elevators))
{
return false;
}
return true;
}
void Game::printWelcomeMenu() const
{
cout << "########################################" << endl;
cout << "# Welcome to EECS 183 Elevator Escape! #" << endl;
cout << "########################################" << endl
<< endl;
}
void Game::printIntroMenu() const
{
cout << "[0] Run Tests" << endl;
cout << "[1] Load saved game" << endl;
cout << "[2] Start new game" << endl;
cout << "Choice: ";
}
void Game::printAIMenu() const {
cout << "[1] Play as human" << endl;
cout << "[2] Watch AI play" << endl;
cout << "Choice: ";
}
void Game::printOptions() const
{
cout << "########################################" << endl;
cout << "# Enter service move: #" << endl;
cout << "# 'e' elevatorId 'f' floorNum #" << endl;
cout << "# Or pickup move: #" << endl;
cout << "# 'e' elevatorId 'p' #" << endl;
cout << "########################################" << endl;
}
void Game::printGameStartPrompt() const
{
cout << endl;
cout << "########################################" << endl;
cout << "# Enter: #" << endl;
cout << "# Q to Quit #" << endl;
cout << "# S to Save #" << endl;
cout << "# At any time after this prompt #" << endl;
cout << "########################################" << endl;
cout << endl;
cout << "Press enter to continue....";
string trash;
getline(cin, trash);
cout << endl;
}
IntroChoice Game::getIntroChoice() const {
if (IS_AI_OVERRIDE) {
return IntroChoice::New;
}
int choice = -1;
while ((choice < 0) || (choice > 2))
{
printIntroMenu();
string s_choice;
getline(cin, s_choice);
choice = stoi(s_choice);
if ((choice < 0) || (choice > 2))
{
cout << endl
<< "Invalid menu choice!" << endl
<< endl;
}
}
cout << endl;
return static_cast<IntroChoice>(choice);
}
AIChoice Game::getAIChoice() const {
if (IS_AI_OVERRIDE) {
return AIChoice::AI;
}
int choice = 0;
printAIMenu();
string s_choice;
getline(cin, s_choice);
choice = stoi(s_choice);
while (choice != 1 && choice != 2) {
cout << endl << "Invalid menu choice!" << endl << endl;
getline(cin, s_choice);
choice = stoi(s_choice);
}
cout << endl;
return static_cast<AIChoice>(choice);
}
void Game::printSatisfactionIndex() const
{
cout << endl
<< endl;
cout << "Your hotel's current satisfaction index is: " << satisfactionIndex;
cout << endl
<< endl;
}
void Game::getPeopleToPickup(Move& move) const
{
int currentFloorNum =
building.getElevatorById(move.getElevatorId()).getCurrentFloor();
Floor currentFloor = building.getFloorByFloorNum(currentFloorNum);
if (currentFloor.getNumPeople() == 1)
{
move.setPeopleToPickup("0", currentFloorNum, currentFloor);
return;
}
string peopleIndices = "";
bool isValidPickup = false;
while (!isValidPickup)
{
currentFloor.printFloorPickupMenu(cout);
cout << endl;
cout << "Choices: ";
getline(cin, peopleIndices);
cout << "\n";
if (isAIMode) {
Elevator e = building.getElevatorById(move.getElevatorId());
Floor f = building.getFloorByFloorNum(e.getCurrentFloor());
peopleIndices = getAIPickupList(move, building.getBuildingState(), f);
cout << "AI OVERRIDE: " << peopleIndices << endl;
}
isValidPickup = isValidPickupList(peopleIndices, currentFloorNum);
if (!isValidPickup)
{
cout << "Invalid list of people chosen!" << endl;
checkForGameEnd();
}
}
move.setPeopleToPickup(peopleIndices, currentFloorNum, currentFloor);
}
Move Game::getMove() const
{
Move moveObj;
string moveString = "";
bool isValid = false;
GameChoice choiceCapture = GameChoice::Invalid;
do
{
cout << "Enter move: ";
getline(cin, moveString);
cout << "\n";
if (isAIMode) {
moveString = getAIMoveString(building.getBuildingState());
cout << "AI OVERRIDE: " << moveString << endl;
}
moveObj = Move(moveString);
choiceCapture = static_cast<GameChoice>(moveString[0]);
switch (choiceCapture)
{
case GameChoice::Quit:
performQuit();
break;
case GameChoice::Save:
isValid = performSave();
break;
case GameChoice::Pass:
case GameChoice::Move:
isValid = performMove(moveObj);
break;
default:
isValid = false;
break;
}
if (!isValid)
{
cout << endl
<< "Invalid move!" << endl
<< endl;
checkForGameEnd();
}
} while (!isValid);
return moveObj;
}
void Game::performQuit() const
{
QuitChoice quitChoice = static_cast<QuitChoice>(getExitChoice());
switch (quitChoice)
{
case QuitChoice::Quit:
endGame();
break;
case QuitChoice::Stay:
return;
break;
}
}
bool Game::performSave() const
{
ofstream saveFile(LOAD_FILENAME);
if (!saveFile.is_open())
{
cout << "Could not open " << LOAD_FILENAME << endl;
return false;
}
return saveGame(saveFile);
}
void Game::initGame(ifstream& loadFile)
{
cout << endl
<< "Loading game..." << endl;
loadFile >> satisfactionIndex;
int tempTime = 0;
loadFile >> tempTime;
building.setTime(tempTime);
string elvState;
int i = 0;
while (i < NUM_ELEVATORS && loadFile >> elvState) {
building.setElevator(elvState, i);
i++;
}
if (!loadFile.fail()) {
cout << "Loaded!" << endl << endl;
}
else {
cout << endl << "Error when loading the file! "
<< "Verify that you have correctly created your project."
<< endl << endl;
cout << "You will need to fix this before implementing Game::playGame "
<< "or the Reach AI." << endl << endl;
cout << "Press enter to continue....";
string junk;
getline(cin, junk);
cout << endl;
}
return;
}
void Game::printExitMenu() const
{
cout << endl;
cout << "Are you sure you want to quit?" << endl;
cout << "[1] Yes, I'm sure!" << endl;
cout << "[2] No, keep playing!" << endl;
cout << "Choice: ";
}
int Game::getExitChoice() const
{
int exitChoice = 0;
while ((exitChoice < 1) || (exitChoice > 2))
{
printExitMenu();
string choice = "";
getline(cin, choice);
exitChoice = stoi(choice);
if ((exitChoice < 1) || (exitChoice > 2))
{
cout << endl
<< "Invalid menu choice!" << endl
<< endl;
}
}
cout << endl;
return exitChoice;
}
void Game::endGame() const
{
cout << endl
<< "Goodbye!" << endl;
satisfactionIndex.save();
exit(0);
}
void Game::printSuccessEnding() const
{
cout << endl
<< endl;
cout << "Time is up! Your final score is: " << endl;
satisfactionIndex.printSatisfaction(cout);
}
void Game::printFailureEnding() const
{
cout << endl
<< endl;
cout << "Uh oh! Your hotel has gone out of business!" << endl;
cout << "Better luck next time!" << endl
<< endl;
cout << "Your final achievement:" << endl
<< endl;
satisfactionIndex.printSatisfaction(cout);
}
void Game::checkForGameEnd() const
{
if (building.getTime() >= MAX_TURNS)
{
printSuccessEnding();
endGame();
}
else if (satisfactionIndex.getSatisfaction() < 0)
{
printFailureEnding();
endGame();
}
}
void Game::update(const Move& m)
{
if (m.isPickupMove())
{
satisfactionIndex.updateSumDirectionRequest(m, building);
}
satisfactionIndex.updateSumExploded(building.tick(m));
satisfactionIndex.updateTimeReached(building);
if (building.getTime() % 2)
{
satisfactionIndex.updateSumIdle(building);
}
}
// Stub for saveGame for project
// You will *not* implement this function for the project
bool Game::saveGame(ofstream& saveFile) const {
return true;
}