Repository navigation
Expand file tree
/
Copy pathWordSmith.cpp
More file actions
283 lines (246 loc) · 6.75 KB
/
Copy pathWordSmith.cpp
File metadata and controls
283 lines (246 loc) · 6.75 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
#include <iostream>
#include <fstream>
#include "WordSmith.h"
using namespace std;
Trie WordSmith::dictionary;
long WordSmith::dictionarySize = 0;
string WordSmith::topScorePlayer;
int WordSmith::topScore = 0;
WordSmith::WordSmith()
{
loadDictTrie();
}
//////////Play Game Functions/////////////
/*The method will being a new round.
Each round can keep track of the score of multiple players, as well
as the highest score in the round.*/
void WordSmith::newRoundofGames()
{
string startNewGame;
cout << "Would you like to start a new game? (Y/N): ";
cin >> startNewGame;
while (startNewGame == "Y" || startNewGame == "y")
{
wordsSubmitted.clear(); //reset trie of words submitted
score = 0; //reset the score
getPlayerName();
startGame();
startTimeCalc();
while(checkTime())
wordSmithGame();
bestGame();
cout << "Would you like to start a new game? (Y/N): ";
cin >> startNewGame;
}
}
/*collects a first and last name from the player*/
void WordSmith::getPlayerName()
{
cout << "Please enter your First Name: ";
cin >> playerFirstName;
cout << "Please enter your Last Name: ";
cin >> playerLastName;
playerName();
}
/* Starts a single game. Will prompt the player to begin by printing out a
shuffled word.*/
void WordSmith::startGame()
{
string shuffledWord;
cout << "Welcome to WordSmith - Your letters are: ";
initialWordShuffled = returnShuffledWordUpper();
for (int i=0;i<initialWordShuffled.length();i++)
{
cout << initialWordShuffled[i] << " ";
}
cout << endl;
cout << "You have " << GAME_TIME << " seconds to form as many words as you"
<< " can with those letters.\n" ;
cout << "Your current score is: " << score << endl;
}
/*a single player game-play function, which prompts the player to enter
a word, checks if there is any time left int he game, checks if the word
is in the dictionary, and awards points appropriately.*/
void WordSmith::wordSmithGame()
{
string wordEntry;
cout << "Enter Your Word Here: ";
cin >> wordEntry;
if (!checkTime())
{
cout <<
"Sorry! You ran out of time. Your total score in this game is: ";
cout << score << endl;
cout << "The bonus word was: " << initialWord << endl;
return;
}
for (int i = 0; i < wordEntry.length(); i++)
{
wordEntry[i]=toupper(wordEntry[i]);
}
if(wordEntry == initialWord && !wordsSubmitted.exists(wordEntry))
{
score += 10;
wordsSubmitted.insert(wordEntry);
cout << "YESSS!!! Way to go - You got the 10 point bonus word!\n";
}
else if(checkWord(wordEntry))
{
score += wordEntry.length();
wordsSubmitted.insert(wordEntry);
cout << "YES! ";
}
else
{
if(dictionary.exists(wordEntry) && !isContainedIn(wordEntry, initialWord))
cout << "Word includes invalid letters. -2 penalty\n";
else if (wordsSubmitted.exists(wordEntry))
cout << "Word already submitted! -2 penalty.\n";
else
cout << "That isn't a valid word. -2 penalty.\n";
score -= 2;
}
cout << "Your current score is: " << score << " and you have " << timeLeft
<< " seconds left.\n";
cout << "Your letters are: " ;
for (int i=0;i<initialWordShuffled.length();i++)
{
cout << initialWordShuffled[i] << " ";
}
cout << endl;
cout << "Your words so far: ";
wordsSubmitted.printTrie(nullDeque);
cout << endl;
}
//prints the player with the best score in a single round.
void WordSmith::bestGame()
{
if (score > topScore && score > 0)
{
topScorePlayer = player;
topScore = score;
}
if (topScore == 0)
{
cout << "There is no top score yet!" << endl;
return;
}
cout << "The best score so far: " << WordSmith::topScore <<
". The player: " << WordSmith::topScorePlayer << "." << endl;
cout << "Thank you for Playing!\n";
return;
}
//voidSmith helpder
bool WordSmith::checkWord(string submittedWord)
{
return (!wordsSubmitted.exists(submittedWord)
&& dictionary.exists(submittedWord)
&& isContainedIn(submittedWord, initialWord));
}
/*checkWord() helper, makes sure letters submitted by the player
were in original letters (of the shuffled word)
given at the beginning of the game*/
bool WordSmith::isContainedIn(string playerStr, string gameLetters)
{
int availableLetterCounts[256]; // Num pats in a byte
for (int i = 0; i < 256; i++) {
availableLetterCounts[i] = 0;
}
for (int i = 0; i < gameLetters.length(); i++) {
int j = toupper(gameLetters[i]);
availableLetterCounts[j]++;
}
for (int i = 0; i < playerStr.length(); i++) {
int j = toupper(playerStr[i]);
if (availableLetterCounts[j]-- <= 0)
return false;
}
return true;
}
//////////Time Calculations/////////////
void WordSmith::startTimeCalc()
{
startTime = time(NULL);
}
bool WordSmith::checkTime()
{
long timePassed;
currentTime = time(NULL);
timePassed = currentTime - startTime;
timeLeft = GAME_TIME - timePassed;
if (timePassed < GAME_TIME)
return true;
else
return false;
}
//////////Prepare Dictionary/////////////
void WordSmith::loadDictTrie()
{
static bool firstTime = true;
if (!firstTime) return;
firstTime = false;
string word;
ifstream infile("dictionary.txt");
if (!infile)
{
cout << "couldn't open dictionary.txt for input.\n";
exit(1);
}
while(getline(infile, word))
{
dictionarySize++;
for (int i = 0;i<word.length();i++)
{
word[i]=toupper(word[i]);
}
dictionary.insert(word);
}
}
//////Prepare Shuffled Word for Start of Game//////////
void WordSmith::pickRandXLetterWord()
{
string word;
ifstream newInfile("dictionary.txt");
if (!newInfile)
{
cout << "couldn't open myInputFile.txt for input.\n";
exit(1);
}
while(getline(newInfile, word))
{
if(word.length() == xLetters)
{
XLetterWords.push_back(word);
numberofXLetterWords++;
}
}
}
string WordSmith::selectDictWord()
{
pickRandXLetterWord();
srand((unsigned) time(NULL));
int randInt;
randInt = rand() % numberofXLetterWords-1;
if (randInt > 0 && randInt < dictionarySize)
{
initialWord = XLetterWords[randInt];
for (int i = 0;i<initialWord.length();i++)
{
initialWord[i]=toupper(initialWord[i]);
}
return XLetterWords[randInt];
}
else
exit(1);
}
string WordSmith::returnShuffledWordUpper()
{
string wordToReturn;
wordToReturn = selectDictWord();
random_shuffle(wordToReturn.begin(), wordToReturn.end());
for (int i = 0;i<wordToReturn.length();i++)
{
wordToReturn[i]=toupper(wordToReturn[i]);
}
return wordToReturn;
}