forked from Carlos231/Hunt-the-Wumpus-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
504 lines (477 loc) · 17 KB
/
game.cpp
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
/******************************************************
** Program:Hunt_Wumpus.cpp
** Author: Carlos Lopez Molina
** Date: 05/22/2016
** Description: Hunt the Wumpus game
** Input: Choose size of cave, select where want to go.
** Output: Win or loose game.
******************************************************/
#include "game.h"
/*********************************************************************
** FUNCTION: Game
** DESCRIPTION: sets up all of the variables
** PARAMETERS: none
** PRE-CONDITIONS: variables made
** POST-CONDITIONS: variables updated with NULL states
** RETURNS: nothing
*********************************************************************/
//constructor
Game::Game(){
//*rooms = NULL;
gridSize = 0;
arrows = 3;
player_action = 0;
numGold = 0;
player_start_x = player_start_y = player_position_x = player_position_y = 0;
movement = 'o';
gameover = false;
won = false;
startingOver = false;
wumpusdead = false;
srand (time(NULL));
}
/*********************************************************************
** FUNCTION: set_gridSize
** DESCRIPTION: gets size of grid size
** PARAMETERS: none
** PRE-CONDITIONS: gridsize variable exists
** POST-CONDITIONS: new value for gridsize
** RETURNS: nothing
*********************************************************************/
//mutators
void Game::set_gridSize(){ //later through command line arguments
cout << "Enter grid size: ";
cin >> gridSize;
}
/*********************************************************************
** FUNCTION: set_rooms
** DESCRIPTION: array of rooms made
** PARAMETERS: none
** PRE-CONDITIONS: rooms pointer blank
** POST-CONDITIONS: rooms array made with gridsize
** RETURNS: nothing
*********************************************************************/
//make the grid
void Game::set_rooms(){
rooms = new Room *[gridSize];
for (int i = 0; i < gridSize; ++i)
rooms[i] = new Room[gridSize];
}
/*********************************************************************
** FUNCTION: set_movement
** DESCRIPTION: asks for where player wants to move to
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::set_movement(){
cout << " w - up " << endl;
cout << " a - left d - right" << endl;
cout << " s - down" << endl;
cin >> movement;
}
/*********************************************************************
** FUNCTION: set_random_position
** DESCRIPTION: returns random number from 0 - gridsize
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: num
*********************************************************************/
int Game::set_random_position(){
int num = rand() % gridSize;
return num;
}
/*********************************************************************
** FUNCTION: set_event_in_rooms
** DESCRIPTION: puts random events in different locations
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::set_event_in_rooms(){
rooms[player_start_x][player_start_y].set_event('x');
//set random location for gold
rooms[set_random_position()][set_random_position()].set_event('g');
//set random location for Wumpus
rooms[set_random_position()][set_random_position()].set_event('w');
//set random location for Bats
rooms[set_random_position()][set_random_position()].set_event('b');
//set random location for Bats
rooms[set_random_position()][set_random_position()].set_event('b');
//set random location for pits
rooms[set_random_position()][set_random_position()].set_event('p');
//set random location for pits
rooms[set_random_position()][set_random_position()].set_event('p');
}
/*********************************************************************
** FUNCTION: set_starting_position
** DESCRIPTION: sets up the starting position for player
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: variables updated
** RETURNS: nothing
*********************************************************************/
void Game::set_starting_position(){
player_position_x = player_start_x = set_random_position();
player_position_y = player_start_y = set_random_position();
cout << "Player start: " << player_start_x << " " << player_start_y << endl;
}
/*********************************************************************
** FUNCTION: adjust_arrows
** DESCRIPTION: checks to see if have any arrows, subtracts one when called
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: variable changed
** RETURNS: nothing
*********************************************************************/
//for when shoot an arrow
void Game::adjust_arrows(){
if (arrows <= 0){
cout << "You do not have any more arrows!" << endl;
gameover = true;
}
else arrows--;
}
/*********************************************************************
** FUNCTION: get_movement
** DESCRIPTION: returns movement
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: movement
*********************************************************************/
char Game::get_movement(){
return movement;
}
/*********************************************************************
** FUNCTION: get_won
** DESCRIPTION: returns movement
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: movement
*********************************************************************/
bool Game::get_won(){
return won;
}
/*********************************************************************
** FUNCTION: output_grid
** DESCRIPTION: outputs entire grid
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::output_grid(){
for (int i = 0; i < gridSize; ++i){
for (int j = 0; j < gridSize; ++j){
cout << "|" << rooms[i][j].get_event() << "|";
}
cout << endl;
}
}
/*********************************************************************
** FUNCTION: get_player_action
** DESCRIPTION: asks player if move or shoot arrow
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: gets action
** RETURNS: nothing
*********************************************************************/
void Game::get_player_action(){
cout << "Would you like to move through a (1) tunnel or (2) Fire an arrow through an adjacent room? ";
cin >> player_action;
while (player_action != 1 && player_action != 2){
cout << "Bad input, try again: ";
cin >> player_action;
}
}
/*********************************************************************
** FUNCTION: move_player
** DESCRIPTION: moves player from one spot to another and checks for event
** PARAMETERS: none
** PRE-CONDITIONS: player in one position
** POST-CONDITIONS: player in a new position
** RETURNS: nothing
*********************************************************************/
void Game::move_player(){
if ((movement == 'w') && (inbounds(player_position_x-1, player_position_y) == true)){ //up
//previous position
rooms[player_position_x][player_position_y].set_event(' ');
player_position_x--;
check_event();
rooms[player_position_x][player_position_y].set_event('x');
}
else if ((movement == 'a') && (inbounds(player_position_x, player_position_y-1) == true)){ //left
//previous position
rooms[player_position_x][player_position_y].set_event(' ');
player_position_y--;
check_event();
rooms[player_position_x][player_position_y].set_event('x');
}
else if ((movement == 'd') && (inbounds(player_position_x, player_position_y+1) == true)){ //right
//previous position
rooms[player_position_x][player_position_y].set_event(' ');
player_position_y++;
check_event();
rooms[player_position_x][player_position_y].set_event('x');
}
else if ((movement == 's') && (inbounds(player_position_x+1, player_position_y) == true)){ //down
//previous position
rooms[player_position_x][player_position_y].set_event(' ');
player_position_x++;
check_event();
rooms[player_position_x][player_position_y].set_event('x');
}
else cout << "**** You are out of bounds! ****" << endl;
}
/*********************************************************************
** FUNCTION: check_event
** DESCRIPTION: looks at what event is in the position then does their specific action
** PARAMETERS: none
** PRE-CONDITIONS: events set in position
** POST-CONDITIONS: actions made, position may change and variables updates
** RETURNS: nothing
*********************************************************************/
//function to check what event is in the spot
void Game::check_event(){
if (rooms[player_position_x][player_position_y].get_event() == 'w'){
//wumpus function
cout << "You have walked into the Wumpus room waking the Wumpus up! He has now ate you. " << endl;
gameover = true;
}
//bats
else if (rooms[player_position_x][player_position_y].get_event() == 'b'){
cout << "OH NO, SUPER BATS! They have taken you to a new location." << endl;
rooms[player_position_x][player_position_y].set_event(' ');
player_position_x = set_random_position();
player_position_y = set_random_position();
rooms[player_position_x][player_position_y].set_event('x');
}
//gold
else if (rooms[player_position_x][player_position_y].get_event() == 'g'){
cout << "You have found gold, congrats!" << endl;
numGold++;
}
//pit
else if (rooms[player_position_x][player_position_y].get_event() == 'p'){
cout << "You have fallen in a pit and died. You lose:(" << endl;
gameover = true;
}
}
/*********************************************************************
** FUNCTION: inbounds
** DESCRIPTION: makes sure player is in grid inbounds
** PARAMETERS: int x, int y
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: true or false
*********************************************************************/
bool Game::inbounds(int x, int y){
if (x > gridSize-1 || x < 0 || y > gridSize-1 || y < 0){
return false;
}
else
return true;
}
/*********************************************************************
** FUNCTION: output_messages
** DESCRIPTION: outputs nearby event messages
** PARAMETERS: none
** PRE-CONDITIONS: events made
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::output_messages(){
cout << "************MESSAGES!***********************" << endl;
//up
if ((inbounds(player_position_x-1, player_position_y) == true) && (rooms[player_position_x-1][player_position_y].get_event() != ' ')){
cout << rooms[player_position_x-1][player_position_y].get_messages() << endl;
}
//left
if ((inbounds(player_position_x, player_position_y-1) == true) && (rooms[player_position_x][player_position_y-1].get_event() != ' ')){
cout << rooms[player_position_x][player_position_y-1].get_messages() << endl;
}
//right
if ((inbounds(player_position_x, player_position_y+1) == true) && (rooms[player_position_x][player_position_y+1].get_event() != ' ')){
cout << rooms[player_position_x][player_position_y+1].get_messages() << endl;
}
//down
if ((inbounds(player_position_x+1, player_position_y) == true) && (rooms[player_position_x+1][player_position_y].get_event() != ' ')){
cout << rooms[player_position_x+1][player_position_y].get_messages() << endl;
}
}
/*********************************************************************
** FUNCTION: shoot_arrow
** DESCRIPTION: shoots arrow through 3 rooms or at Wumpus
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: sees if kills wumpus or not
** RETURNS: nothing
*********************************************************************/
void Game::shoot_arrow(){
set_movement(); //get direction for arrow
int tempX = player_position_x;
int tempY = player_position_y;
cout << "Arrow shot-> -> ->" << endl;
if (movement == 'w'){
//loop until arrow moves 3 places or hits wall
for (int i = 1; i < 4; i++){
tempX = player_position_x+i;
tempY = player_position_y;
if ((inbounds(tempX, tempY) == true) && (rooms[tempX][tempY].get_event() == 'w')){
cout << "You have killed the Wumpus!" << endl;
wumpusdead = true;
break;
}
else cout << "You did not kill the Wumpus!" << endl;
}
}
else if (movement == 'a'){ //left
//loop until arrow moves 3 places or hits wall
for (int i = 1; i < 4; i++){
tempX = player_position_x;
tempY = player_position_y-1;
if ((inbounds(tempX, tempY) == true) && (rooms[tempX][tempY].get_event() == 'w')){
cout << "You have killed the Wumpus!" << endl;
wumpusdead = true;
break;
}
else cout << "You did not kill the Wumpus!" << endl;
}
}
else if (movement == 'd') { //right
//loop until arrow moves 3 places or hits wall
for (int i = 1; i < 4; i++){
tempX = player_position_x;
tempY = player_position_y+1;
if ((inbounds(tempX, tempY) == true) && (rooms[tempX][tempY].get_event() == 'w')){
cout << "You have killed the Wumpus!" << endl;
wumpusdead = true;
break;
}
else cout << "You did not kill the Wumpus!" << endl;
}
}
else if (movement == 's') { //down
//loop until arrow moves 3 places or hits wall
for (int i = 1; i < 4; i++){
tempX = player_position_x+1;
tempY = player_position_y;
if ((inbounds(tempX, tempY) == true) && (rooms[tempX][tempY].get_event() == 'w')){
cout << "You have killed the Wumpus!" << endl;
wumpusdead = true;
break;
}
else cout << "You did not kill the Wumpus!" << endl;
}
}
}
/*********************************************************************
** FUNCTION: menu
** DESCRIPTION: main menu for the game, checks the actio they have chosen
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::menu(){
get_player_action();
if (player_action == 1){
set_movement(); //arrow selection to move
//function to move player
move_player();
check_event();
}
else if (player_action == 2){
adjust_arrows();
//function to send arrow through rooms
shoot_arrow();
}
}
/*********************************************************************
** FUNCTION: Game
** DESCRIPTION: removes arrray
** PARAMETERS: none
** PRE-CONDITIONS: array exists
** POST-CONDITIONS: array is deleted
** RETURNS: nothing
*********************************************************************/
//deconstructors
Game::~Game(){
for (int i = 0; i < gridSize; ++i){
delete[] rooms[i];
}
delete[] rooms;
}
/*********************************************************************
** FUNCTION: endgame
** DESCRIPTION: outputs messages for end of game
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::endgame(){
int choice;
if (won != true){
cout << "You lost, would you like to: " << endl;
cout << " (1) Start game over with same cave configuration. "<< endl;
cout << " (2) Start game over with new random cave configuration" << endl;
cout << " (3) Quit game entirely." << endl;
cin >> choice;
if (choice == 1){
won = false;
}
else if (choice == 2){
//run_game();
}
}
else cout << "Congratulations, you won!" << endl;
}
/*********************************************************************
** FUNCTION: check_if_won
** DESCRIPTION: checks variables to see if won
** PARAMETERS: none
** PRE-CONDITIONS: none
** POST-CONDITIONS: none
** RETURNS: nothing
*********************************************************************/
void Game::check_if_won(){
if (numGold == 1 && wumpusdead == true && player_position_x == player_start_x && player_position_y == player_start_y){
won = true;
gameover = true;
}
}
/*********************************************************************
** FUNCTION: run_game
** DESCRIPTION: runs entire game
** PARAMETERS: int num
** PRE-CONDITIONS: game non existent
** POST-CONDITIONS: game, variables and arrays made
** RETURNS: nothing
*********************************************************************/
void Game::run_game(int num){
if (startingOver == true){
set_gridSize(); //get number for cave size
}
gridSize = num;
set_rooms(); //build array and set to blank spot
set_starting_position(); //set random spot in cave to start
set_event_in_rooms(); //change spots in cave to events
output_grid(); //output the cave
do{
cout << endl;
cout << "Gold found: " << numGold << endl;
cout << "Number of arrows: " << arrows << endl;
cout << "Postion: " << player_position_x << " " << player_position_y << endl;
output_messages();
menu(); //walk or shoot
output_grid(); //output the cave
check_if_won();
} while (gameover != true);
endgame();
}