-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
177 lines (155 loc) · 4.97 KB
/
main.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
// Idea provided by www.reddit.com/r/dailyprogrammer
//
// This program recreates the hacking minigame from Fallout 3 and Fallout New Vegas.
// A randomized word bank is created based on the difficulty choice of the player, a random
// word is chosen to be the key, and the player must make guesses to figure out which word
// is the key.
// The word list is provided by www.reddit.com/r/dailyprogrammer and is a .txt file
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#define MAX_ATTEMPTS 4 // Maximum amount of times the player can guess the word
std::vector<std::string> wordList;
// Function protoypes
void setupGame ();
void getWords (const int, const int);
void displayWordBank ();
void startGame();
void playAgain();
int main () {
setupGame();
displayWordBank();
startGame();
return 0;
}
// Prepares the word bank by getting the difficulty from the user
// Higher difficulty means more words and longer words
void setupGame () {
int diffChoice = 0;
int diffLength = 0;
do {
std::cout << "Choose your difficulty (1-5): "; std::cin >> diffChoice;
} while (diffChoice < 1 || diffChoice > 5);
switch (diffChoice) {
case 1: // Very easy
std::cout << "You chose very easy.\n";
diffLength = 4;
wordList.resize(5);
break;
case 2: // Easy
std::cout << "You chose easy.\n";
diffLength = 6;
wordList.resize(10);
break;
case 3: // Average
std::cout << "You chose average.\n";
diffLength = 8;
wordList.resize(10);
break;
case 4: // Hard
std::cout << "You chose hard.\n";
diffLength = 12;
wordList.resize(15);
break;
case 5: // Very hard
std::cout << "You chose very hard.\n";
diffLength = 15;
wordList.resize(15);
break;
default:
break;
}
getWords(diffChoice, diffLength);
}
// Reads from a database of words, attempting to choose random words
// to insert into the word bank
void getWords (const int diffChoice, const int diffLength) {
std::ifstream wordDatabase;
std::string word = " ";
int startingChar = 0;
wordDatabase.open("words.txt");
srand(time(NULL));
// Loop through the vector and fill it with random words with length matching the
// length decided by the difficulty choice
for (int i = 0; i < wordList.size(); i++) {
do {
// Read in a random number of lines to help randomize the word bank
// This is probably a really dumb and horrible way to do this
for (int k = (rand() % 250); k > 0; k--) {
wordDatabase >> word;
}
wordDatabase >> word;
} while (word.length() != diffLength);
wordList[i] = word;
}
wordDatabase.close();
// Shuffle the vector to further randomize the word bank
std::random_shuffle(wordList.begin(), wordList.end());
}
// Displays the word bank with all words in upper-case
void displayWordBank () {
for (int i = 0; i < wordList.size(); i++) {
std::string str = wordList[i];
// Nested loop to display the words as all upper-case
for (int k = 0; k < wordList[i].length(); k++) {
char temp = str[k]-32;
std::cout << temp;
}
std::cout << std::endl;
}
std::cout << std::endl;
}
// Begins the game, asking the player to guess and informing them if their guess is incorrect or correct
// When incorrect, tell the player how close their guess was
void startGame () {
int attempts = MAX_ATTEMPTS;
std::string playerGuess = " ";
std::string password = " ";
// Choose a word in the bank at random to become the password
password = wordList[(rand() % wordList.size())];
while (attempts > 0) {
int count = 0;
// Check the validity of the user's guess (oh nefarious user)
do {
std::cout << "Guess the word (" << attempts << " left): ";
std::cin >> playerGuess;
if(playerGuess.length() != password.length())
std::cout << "Word Length mismatched. Word length should be " << password.length() << ". Please re-enter." << std::endl;
} while (playerGuess.length() != password.length());
// Ensure the player's guess is lower case
for (int i = 0; i < playerGuess.length(); i++) {
tolower(playerGuess[i]);
}
if (playerGuess != password) { // If player's guess isn't correct then check which letters (if any) match
--attempts;
for (int i = 0; i < playerGuess.length(); i++) { // See if each letter matches
if (playerGuess[i] == password[i])
count++;
}
std::cout << count << "/" << playerGuess.length() << " letters correct\n";
} else { // If player's guess is correct then tell them so and quit the function
std::cout << playerGuess.length() << "/" << playerGuess.length() << " letters correct\n";
std::cout << "You win! :D\n";
playAgain();
return;
}
}
std::cout << "You lost :( the correct password was: " << password << std::endl;
playAgain();
}
//Gives option to start a new game after the end of current game session
void playAgain() {
char playAgainOption;
std::cout << "Want to play again? (Y/N)" << std::endl;
std::cin >> playAgainOption;
if(playAgainOption == 'y' || playAgainOption == 'Y')
main();
else {
std::cout << "Bye" << std::endl;
return;
}
}