-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainfile.cpp
232 lines (201 loc) · 7.82 KB
/
mainfile.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
#include "letterrush.h"
// Credit to 'dwyl' for the dictionary file.
// https://github.com/dwyl
int main()
{
// create hash table object for dictionary
hashTable dictTable;
string filename = "dictionary.txt", word;
ifstream file(filename);
// read the dictionary text file into the hash table object
if (file.is_open())
{
while (file >> word)
{
dictTable.insert(word);
}
}
// close the file when done using it
file.close();
// create hash table for alphabet
hashTable oneCharacterTable(60);
string filenameTwo = "oneCharacter.txt";
ifstream fileTwo(filenameTwo);
if (fileTwo.is_open())
{
while (fileTwo >> word)
{
oneCharacterTable.insert(word);
}
}
fileTwo.close();
// create hash table for combinations of two letters in alphabet
hashTable twoCharacterTable(1400);
string filenameThree = "twoCharacters.txt";
ifstream fileThree(filenameThree);
if (fileThree.is_open())
{
while (fileThree >> word)
{
twoCharacterTable.insert(word);
}
}
fileThree.close();
//----------------------------------------------------------------------------
int numberOfPlayers = 0;
cout << "Welcome to Letter Rush! How many people will be playing?: (must have at least 2 players)" << endl;
// need to save the user input in a variable.
cin >> numberOfPlayers;
// verify whether there is sufficient players or not
if (numberOfPlayers <= 1)
{
cout << endl << "Sorry, you need at least 2 players." << endl;
exit(0);
}
// allocate space for an array containing all of the player objects
Player* listOfPlayers = new Player[numberOfPlayers];
// obtain nicknames and initalize the player objects
string playerName;
for (int i = 0; i < numberOfPlayers; i++)
{
cout << endl << "Player " << i+1 << "!" << " What is your nickname?: (enter at least 1 character)" << endl;
cin >> playerName;
Player currentPlayer(playerName);
listOfPlayers[i] = currentPlayer;
}
// word bank to keep track of words that have been used
vector<string> usedWords;
// boolean variables for knowing when to continue/stop the round
bool stillPlay = true;
bool stillPlayersLeft = true;
// while the user still wants to play
while (stillPlay)
{
sleep(1);
cout << endl << "The round will now start!" << endl;
// clear the word box for the new round
usedWords.clear();
stillPlayersLeft = true;
int sec = 10;
int setTime = 60;
bool difficultyUp = false;
// variable used to keep track of runtime of current round
time_t roundTime = time(NULL);
// while there are still players left
while (stillPlayersLeft)
{
// for each player
for (int i = 0; i < numberOfPlayers; i++)
{
// check if there's at least 2 players with lives left
if (checkPlayers(listOfPlayers, numberOfPlayers) == false)
{
stillPlayersLeft = false;
break;
}
// checking for players that have been eliminated, to skip them
if (listOfPlayers[i].lives == 0)
{
continue;
}
// check if round time has gone over a minute, if so, increase difficulty
if (difftime(time(NULL), roundTime) >= setTime && difficultyUp == false)
{
cout << endl << "...Difficulty is increasing!" << endl;
difficultyUp = true;
}
sleep(1);
cout << endl << listOfPlayers[i].nickname << ", it is your turn!" << endl;
string character;
// check if round difficulty has been increased or not
if (difficultyUp == true)
{
sec = 5;
character = twoCharacterTable.chooseRandom();
}
else
{
character = oneCharacterTable.chooseRandom();
}
sleep(1);
cout << endl << "You have " << sec << " seconds to type in a valid word with the letter: " << character << endl;
bool validWord = false;
string userInput;
// set timer for player to enter input(s)
time_t start = time(NULL);
// keep checking if user has input a valid otherwise, otherwise, time will run out
while (!validWord)
{
// obtain user input
cin >> userInput;
// if time runs out
if (difftime(time(NULL), start) >= sec)
{
cout << endl << "Didn't enter in " << sec << " seconds! You lose a life :(" << endl;
// remove a life from the current player
listOfPlayers[i].lives -= 1;
// check if the player should be eliminated
if (listOfPlayers[i].lives == 0)
{
cout << endl << listOfPlayers[i].nickname << " is out!" << endl;
}
break;
}
// verify that the given word is in the dictionary
else if (dictTable.find(userInput) == false)
{
cout << endl << "That word does not exist! Try again:" << endl;
continue;
}
// verify that the word has not been used in the round already
else if (duplicateCheck(usedWords, userInput) == true)
{
cout << endl << "That word has already been used! Try again:" << endl;
continue;
}
// verify that the given character is within the given word
else if (characterCheck(userInput, character) == false)
{
cout << endl << "That word doesn't contain the given letter! Try again:" << endl;
continue;
}
// if user input passes all of the cases
else
{
cout << endl << "That word is valid!" << endl;
usedWords.push_back(userInput);
validWord = true;
continue;
}
}
}
}
// announce winner of that round, and award them with +1 to their score.
for (int i = 0; i < numberOfPlayers; i++)
{
if (listOfPlayers[i].lives != 0)
{
cout << endl << listOfPlayers[i].nickname << " won this round!" << endl;
listOfPlayers[i].score += 1;
}
// reset lives for next round
listOfPlayers[i].lives = 3;
}
// print scores of all of the players
cout << endl << "Here are the current scores: " << endl;
for (int i = 0; i < numberOfPlayers; i++)
{
cout << endl;
cout << listOfPlayers[i].nickname << " - Wins: " << listOfPlayers[i].score << endl;
}
// ask user if they to play another round
if (nextRoundCheck() == false)
{
stillPlay = false;
}
}
cout << endl << "Thanks for playing Letter Rush by Daniel Nguyen!" << endl;
// deallocate space for the player objects
delete[] listOfPlayers;
return 0;
}