-
Notifications
You must be signed in to change notification settings - Fork 0
/
textGameOneFileVer.cpp
1540 lines (1285 loc) · 34.7 KB
/
textGameOneFileVer.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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
** Program name: player.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Holds information unique to the player, including inventory
** of items and player's current state (win, lose, or playing)
*********************************************************************/
//#ifndef PLAYER_HPP
//#define PLAYER_HPP
#include <vector>
class Player
{
public:
enum State { NONE, PLAYING, WIN, LOSE };
private:
State state; // player's current state (playing, win, or lose)
const int INVENTORY_MAX = 2;
std::vector<char> inv;
public:
Player();
~Player();
void setState(State newState);
State getState();
void addItem(char item);
bool hasItem(char item);
bool useItem(char item);
void printInventory();
bool inventoryFull();
};
//#endif
/*********************************************************************
** Program name: space.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents a space the player can move to within the text-based
** game. Each space contains pointers to adjacent spaces.
*********************************************************************/
//#ifndef SPACE_HPP
//#define SPACE_HPP
//#include "player.hpp"
class Space
{
private:
const char PLAYER_SYMBOL = 'X';
Space* top = nullptr;
Space* bottom = nullptr;
Space* left = nullptr;
Space* right = nullptr;
protected:
bool player;
bool walkable;
char symbol;
char defaultSymbol;
public:
Space();
virtual ~Space();
enum Direction {UP, DOWN, LEFT, RIGHT};
bool hasPlayer();
void setPlayer(bool status);
bool isWalkable();
char getSymbol();
void setAdjacent(Direction direction, Space* target);
Space* getAdjacent(Direction direction);
virtual void arrive(Player* player) = 0;
virtual void inspect(Player* player) = 0;
virtual void setSymbol(char symbol) {}
};
//#endif
/*********************************************************************
** Program name: board.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Manages the creation and printing of a game board consisting of
** linked spaces, each of which contains pointers to its adjacent spaces
*********************************************************************/
//#ifndef BOARD_HPP
//#define BOARD_HPP
//#include "space.hpp"
#include <string>
class Board
{
private:
int boardRows;
int boardCols;
Space* origin;
Space* playerSpace;
const std::string mapFilename = "map.txt";
std::string map; // string of chars used to read map.txt and create board from it
public:
Board();
~Board();
// Get and set methods
Space* getPlayerSpace();
// Gameplay methods
void print();
bool playerMove(char direction);
// Board creation methods
void readMap(); // reads a 'map' of the game board from .txt file into string
Space* createSpace(char type); // allocates and returns a Space of given type
};
//#endif
/*********************************************************************
** Program name: emptySpace.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents an empty space that the player can walk on.
*********************************************************************/
//#ifndef EMPTYSPACE_HPP
//#define EMPTYSPACE_HPP
//#include "space.hpp"
//#include "player.hpp"
class EmptySpace : public Space
{
private:
public:
EmptySpace();
virtual ~EmptySpace();
virtual void arrive(Player* player);
virtual void inspect(Player* player);
virtual void setSymbol(char symbol);
};
//#endif
/*********************************************************************
** Program name: exitSpace.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents the exit in the game; if the player reaches
** the exit space, the player wins the game.
*********************************************************************/
//#ifndef EXITSPACE_HPP
//#define EXITSPACE_HPP
//#include "space.hpp"
//#include "player.hpp"
class ExitSpace : public Space
{
private:
public:
ExitSpace();
virtual ~ExitSpace();
virtual void arrive(Player* player);
virtual void inspect(Player* player);
};
//#endif
/*********************************************************************
** Program name: game.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Contains the game board and player object, and manages
** interactions between them
*********************************************************************/
//#ifndef GAME_HPP
//#define GAME_HPP
//#include "player.hpp"
class Game
{
private:
const int START_STEPS = 22;
Player* player;
public:
Game();
~Game();
void play();
};
//#endif
/*********************************************************************
** Program name: getInput.hpp
** Author: Jesse McKenna
** Date: 9/27/2017
** Description: Helper functions for input validation
*********************************************************************/
//#ifndef GETINPUT_HPP
//#define GETINPUT_HPP
#include <string>
int getInt();
int getInt(int min);
int getInt(int min, int max);
double getDouble();
double getDouble(double min);
double getDouble(double min, double max);
bool getYesNo();
char getChar();
char getChar(std::string validChars);
std::string getString();
std::string getFilename(std::string extension);
//#endif
/*********************************************************************
** Program name: holeSpace.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents a hole that the player can either fall into, ending
** the game, or fill with rocks to solve the puzzle.
*********************************************************************/
//#ifndef HOLESPACE_HPP
//#define HOLESPACE_HPP
//#include "space.hpp"
//#include "player.hpp"
class HoleSpace : public Space
{
private:
public:
HoleSpace();
virtual ~HoleSpace();
virtual void arrive(Player* player);
virtual void inspect(Player* player);
};
//#endif
/*********************************************************************
** Program name: menu.hpp
** Author: Jesse McKenna
** Date: 10/3/2017
** Description: Header file for menu functions.
*********************************************************************/
//#ifndef MENU_HPP
//#define MENU_HPP
#include <string>
class Menu
{
private:
int size;
std::string* prompts; // dynamically allocated array of strings
const int MAX_PROMPTS = 10;
public:
Menu(int size); // default constructor takes a menu size
~Menu(); // destructor
void setPrompt(int number, std::string prompt); // set the desired prompt text
int showMenu(); // displays the menu and returns the selection made
};
//#endif
/*********************************************************************
** Program name: symbols.hpp
** Author: Jesse McKenna
** Date: 11/19/2017
** Description: Defines the symbols used in the game to represent different
** types of spaces, items, etc.
*********************************************************************/
//#ifndef SYMBOLS_HPP
//#define SYMBOLS_HPP
// Spaces
const char EMPTY = ' ';
const char WALL = '#';
const char HOLE = '@';
const char EXIT = 'E';
// Items
const char ROCK = '^';
const char PICK = 'P';
//#endif
/*********************************************************************
** Program name: wallSpace.hpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents a wall space that the player cannot walk on.
*********************************************************************/
//#ifndef WALLSPACE_HPP
//#define WALLSPACE_HPP
//#include "space.hpp"
//#include "player.hpp"
class WallSpace : public Space
{
private:
public:
WallSpace();
virtual ~WallSpace();
virtual void arrive(Player* player);
virtual void inspect(Player* player);
};
//#endif
/*********************************************************************
** Program name: space.cpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents a space the player can move to within the text-based
** game. Each space contains pointers to adjacent spaces.
*********************************************************************/
//#include "space.hpp"
// --- Constructor ---
Space::Space()
{
}
// --- Destructor ---
Space::~Space()
{
}
// --- hasPlayer ---
// Return the value of member variable "player"
bool Space::hasPlayer()
{
return player;
}
// --- setPlayer ---
void Space::setPlayer(bool status)
{
player = status;
if (player)
{
symbol = PLAYER_SYMBOL;
}
else
{
symbol = defaultSymbol;
}
}
// --- isWalkable ---
// Return the value of member variable "walkable"
bool Space::isWalkable()
{
return walkable;
}
// --- getSymbol ---
char Space::getSymbol()
{
return symbol;
}
void Space::setAdjacent(Space::Direction direction, Space* target)
{
switch (direction)
{
case UP:
top = target;
break;
case DOWN:
bottom = target;
break;
case LEFT:
left = target;
break;
case RIGHT:
right = target;
break;
}
}
Space* Space::getAdjacent(Space::Direction direction)
{
switch (direction)
{
case UP:
return top;
break;
case DOWN:
return bottom;
break;
case LEFT:
return left;
break;
case RIGHT:
return right;
break;
}
return nullptr;
}
/*********************************************************************
** Program name: player.cpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Holds information unique to the player, including inventory
** of items and player's current state (win, lose, or playing)
*********************************************************************/
//#include "player.hpp"
#include <iostream>
// --- Constructor ---
Player::Player()
{
state = NONE;
}
// --- Destructor ---
Player::~Player()
{
inv.clear();
}
// --- setState ---
void Player::setState(Player::State newState)
{
state = newState;
}
// --- getState ---
Player::State Player::getState()
{
return state;
}
// --- addItem ---
// If there is room in the inventory, add the given item
void Player::addItem(char item)
{
if (inv.size() <= INVENTORY_MAX)
{
inv.push_back(item);
}
}
// --- hasItem ---
// Search for given item in inventory, and return true if found
bool Player::hasItem(char item)
{
for (std::vector<char>::iterator i = inv.begin(); i != inv.end(); i++)
{
if (*i == item) // if item found
{
return true; // delete item from inventory
}
}
return false;
}
// --- useItem ---
// Search for given item in inventory, remove from inventory and return true
// if found
bool Player::useItem(char item)
{
for (std::vector<char>::iterator i = inv.begin(); i != inv.end(); i++)
{
if (*i == item) // if item found
{
inv.erase(i); // delete item from inventory
return true;
}
}
return false;
}
// --- printInventory ---
// Print each char in inventory
void Player::printInventory()
{
if (inv.size() == 0)
{
std::cout << "\nInventory is empty.\n";
}
else
{
std::cout << "\nInventory: ";
for (std::vector<char>::iterator i = inv.begin(); i != inv.end(); i++)
{
std::cout << *i << " ";
}
std::cout << "\n";
}
}
// --- inventoryFull ---
// Return true if inventory is full
bool Player::inventoryFull()
{
return inv.size() >= INVENTORY_MAX;
}
/*********************************************************************
** Program name: getInput.cpp
** Author: Jesse McKenna
** Date: 9/27/2017
** Description: Helper functions for input validation
*********************************************************************/
#include <iostream>
#include <string>
#include <cctype>
//#include "getInput.hpp"
// --- getInt ---
// Requests user input until a valid int is entered. Rejects input containing
// any non-digit characters (except starting hyphen representing negative).
int getInt()
{
// Initialize variables
bool isClean;
std::string input;
int length = 0;
int result;
// Loop until an int value is returned
do
{
isClean = true;
// Get entire line of user input
std::getline(std::cin, input);
// Reject input if length is less than 1 (ex. if user just pressed [enter])
if (input.length() < 1)
{
isClean = false;
}
// Reject input if it contains any non-digit characters
if (isClean && !isdigit(input.at(0)) && input.at(0)!='-')
{
isClean = false; // first character can be digit or - (for negative)
}
for (int i = 1; i < length && isClean; i++)
{
if (!isdigit(input.at(i))) // if any subsequent character is a non-digit
{
isClean = false; // set isClean to false and stop checking characters
}
}
} while (!isClean);
// Convert clean input to an int and assign to result
result = std::stoi(input);
return result;
}
// --- getInt(min) ---
// Requests user input until a valid int is entered. Rejects input containing
// any non-digit characters (except starting hyphen representing negative).
// Input must be greater than or equal to min.
int getInt(int min)
{
int result;
// Keep calling getInt until result is within acceptable range
do
{
result = getInt();
} while (result < min);
return result;
}
// --- getInt(min, max) ---
// Requests user input until a valid int is entered. Rejects input containing
// any non-digit characters (except starting hyphen representing negative).
// Input must be between min and max, inclusive.
int getInt(int min, int max)
{
int result;
// Keep calling getInt until result is within acceptable range
do
{
result = getInt();
} while (result < min || result > max);
return result;
}
// --- getDouble ---
double getDouble()
{
// Initialize variables
bool isClean;
std::string input;
int length = 0;
double result;
int decimalPoints; // count of decimal points (maximum of 1 is accepted)
// Loop until a double value is returned
do
{
isClean = true;
decimalPoints = 0;
// Get entire line of user input
std::getline(std::cin, input);
// Reject input if length is less than 1 (ex. if user just pressed [enter])
if (input.length() < 1)
{
isClean = false;
}
// First character can only be a digit, negative sign, or decimal point
if (isClean && !isdigit(input.at(0)) && input.at(0)!='-' && input.at(0)!='.')
{
isClean = false;
}
else if (input.at(0) == '.')
{
decimalPoints++; // decimal chars are not rejected, but increment count
}
// Subsequent characters can only be digits or decimal points (max 1)
for (int i = 1; i < length && isClean; i++)
{
if (input.at(i) == '.')
{
decimalPoints++; // decimal chars are not rejected, but increment count
}
else if (!isdigit(input.at(i))) // otherwise, if any char is non-digit
{
isClean = false; // set isClean to false and stop checking characters
}
}
// Reject input if more than one decimal point was counted
if (decimalPoints > 1)
{
isClean = false;
}
} while (!isClean);
// Convert clean input to a double and assign to result
result = std::stod(input);
return result;
}
// --- getDouble(min) ---
// Input must be greater than or equal to min.
double getDouble(double min)
{
double result;
// Keep calling getDouble until result is within acceptable range
do
{
result = getDouble();
} while (result < min);
return result;
}
// --- getDouble(min, max) ---
// Input must be between min and max, inclusive.
double getDouble(double min, double max)
{
double result;
// Keep calling getDouble until result is within acceptable range
do
{
result = getDouble();
} while (result < min || result > max);
return result;
}
// --- getYesNo ---
// Requests user input until yes ("y", "yes") or no ("n", "no") is entered,
// then returns true for yes and false for no. Non-case-sensitive.
bool getYesNo()
{
std::string input;
while (true) // loop until appropriate response received
{
std::getline(std::cin, input); // get user input
for (int i = 0, n = input.length(); i < n; i++)
{
input[i] = tolower(input[i]); // convert to lowercase (non-case-sensitive)
}
if (input == "y" || input == "yes")
{
return true;
}
if (input == "n" || input == "no")
{
return false;
}
}
}
// --- getChar ---
// Requests user input until a single character is provided
char getChar()
{
std::string input;
char result;
// Loop until a char value is returned
do
{
// Get entire line of user input
std::getline(std::cin, input);
} while (input.length() != 1); // reject if blank or longer than a single char
result = input[0];
return result;
}
// --- getChar ---
// Requests user input until a single character within validChars is provided
char getChar(std::string validChars)
{
char result;
bool isValid = false;
// Loop until a char value is returned
do
{
result = getChar();
for (int i = 0, n = validChars.length(); i < n && !isValid; i++)
{
if (result == validChars.at(i))
{
isValid = true; // return once result char found in validChars string
}
}
} while (!isValid); // reject if not within validChars
return result;
}
// --- getString ---
// Requests user input until a string is entered; accepts any string containing
// only alphanumeric characters, periods, spaces, hyphens, pounds, and commas
std::string getString()
{
std::string input;
bool isText;
char c;
do
{
isText = true;
std::getline(std::cin, input);
for (int i = 0, n = input.length(); i < n && isText; i++)
{
c = input[i];
if (!isalnum(c) && !isspace(c) && c != '.' && c != '-' && c != '#' &&
c != ',')
{
isText = false; // reject string if non-permitted character is found
}
}
} while (!isText || input.length() < 1);
return input;
}
// --- getFilename ---
// Similar to getString, but appends the file extension if it was not included
// (ex. if requesting a .txt name and user enters "data", returns "data.txt")
std::string getFilename(std::string extension)
{
int extLength = extension.length();
// Add leading . character to extension if not included
if (extLength > 0 && extension.at(0) != '.')
{
extension = '.' + extension;
}
std::string input = getString(); // get string from user
int inputLength = input.length();
bool hasExtension = false;
if (inputLength > extLength) // check whether user added extension
{
hasExtension = true;
for (int i = 0, j = inputLength - extLength;
i < extLength && hasExtension; i++, j++)
{
if (input.at(j) != extension.at(i))
{
hasExtension = false;
}
}
}
if (!hasExtension)
{
input.append(extension);
}
return input;
}
/*********************************************************************
** Program name: menu.cpp
** Author: Jesse McKenna
** Date: 10/3/2017
** Description: Implementation file for menu functions.
*********************************************************************/
//#include "menu.hpp"
//#include "getInput.hpp"
#include <iostream>
// --- Menu ---
// Default constructor, called with the desired menu size; menu size cannot be
// greater than MAX_PROMPTS or it will be limited to MAX_PROMPTS
Menu::Menu(int menuSize)
{
size = menuSize;
if (size > MAX_PROMPTS)
{
size = MAX_PROMPTS; // restrict size to MAX_PROMPTS limit
}
prompts = new std::string[size];
}
// --- ~Menu ---
// Destructor; deletes the dynamically allocated string array "prompts"
Menu::~Menu()
{
delete [] prompts;
prompts = nullptr;
}
// --- setPrompt ---
// Accepts a prompt position and a string as arguments; puts the string into the
// "prompts" array at the specified position
void Menu::setPrompt(int number, std::string prompt) // set the desired prompt text
{
if (number <= 0 || number > size)
{
return; // do nothing if invalid number referenced
}
prompts[number - 1] = prompt; // put entered prompt into the "prompts" array
}
// --- showMenu ---
// Displays the menu by printing each prompt in "prompts", then returns the
// selection made by the user
int Menu::showMenu()
{
std::cout << "\n";
for (int i = 0; i < size; i++)
{
std::cout << i + 1 << ". "; // prompt numbering, starting at 1
std::cout << prompts[i]; // prompt text
std::cout << "\n\n";
}
int choice;
choice = getInt(1, size);
return choice;
}
/*********************************************************************
** Program name: emptySpace.cpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Represents an empty space that the player can walk on.
*********************************************************************/
//#include "emptySpace.hpp"
//#include "player.hpp"
//#include "symbols.hpp"
//#include "menu.hpp"
#include <iostream>
// --- Constructor ---
EmptySpace::EmptySpace()
{
walkable = true;
defaultSymbol = EMPTY;
symbol = defaultSymbol;
}
// --- Destructor ---
EmptySpace::~EmptySpace()
{
}
// --- arrive ---
// Executes upon player arriving in a space;
// does nothing for an empty space
void EmptySpace::arrive(Player* player)
{
}
// --- inspect ---
// Executes upon player choosing to inspect current space
void EmptySpace::inspect(Player* player)
{
if (defaultSymbol == ROCK)
{
std::cout << "\nA formation of large rocks.\n";
if (player->hasItem(PICK) && player->inventoryFull())
{
std::cout << "You have a pickaxe, but your inventory is full.\n";
}
else if (player->hasItem(PICK) && !player->inventoryFull())
{
std::cout << "You have a pickaxe.\n";
Menu mineMenu(2);
mineMenu.setPrompt(1, "Mine the rocks");
mineMenu.setPrompt(2, "Do nothing");
if (mineMenu.showMenu() == 1) // player chooses to mine rocks
{
player->addItem(ROCK); // add rock to inventory
defaultSymbol = EMPTY; // remove rock from space
}
}
}
else if (defaultSymbol == PICK)
{
std::cout << "\nA pickaxe.\n";
if (!player->inventoryFull())
{
Menu pickMenu(2);
pickMenu.setPrompt(1, "Take the pickaxe");
pickMenu.setPrompt(2, "Do nothing");
if (pickMenu.showMenu() == 1) // player chooses to take pickaxe
{
player->addItem(PICK); // add pick to inventory
defaultSymbol = EMPTY; // remove pick from space