-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
179 lines (150 loc) · 6.73 KB
/
Copy pathCode
File metadata and controls
179 lines (150 loc) · 6.73 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
/*
* Description: This project is a word game where two players are given 9 random letters, including 3-5 vowels, and must create the longest valid word using only those letters. The program checks the validity of the words, declares a winner, and reveals the longest possible word from the provided letters.
* Author: Maria Karim
* UIN: 673270424
* Date: 10/18/2024
*/
#include <iostream>
#include <fstream>
#include "mersenne-twister.h" // Library for generating random numbers
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// Function to choose a random number between min and max (inclusive)
int chooseRandomNumber(int min, int max);
// Function to create a string of all possible vowels with their frequencies
string fullSetOfVowels();
// Function to create a string of all possible consonants with their frequencies
string fullSetOfConsonants();
// Generates 9 random letters with the specified number of vowels
string generateLetters(int numVowels) {
string letters; // Resulting letters
string vowels = fullSetOfVowels();
string consonants = fullSetOfConsonants();
// Add random vowels
for (int i = 0; i < numVowels; ++i) {
int index = chooseRandomNumber(0, vowels.size() - 1);
letters += vowels[index];
vowels.erase(index, 1); // Remove the chosen vowel to avoid duplicates
}
// Add random consonants to fill up the remaining slots
for (int i = 0; i < 9 - numVowels; ++i) {
int index = chooseRandomNumber(0, consonants.size() - 1);
letters += consonants[index];
consonants.erase(index, 1); // Remove the chosen consonant to avoid duplicates
}
return letters; // Return the set of letters
}
// Checks if a word is valid using the available letters
bool isWordValid(const string& word, string availableLetters) {
for (char c : word) { // Loop through each character in the word
size_t pos = availableLetters.find(c); // Check if the character is available
if (pos == string::npos) { // If character not found, word is invalid
return false;
}
availableLetters[pos] = ' '; // Mark the character as used
}
return true; // Word is valid
}
// Checks if a word exists in the sorted word list using binary search
bool isWordInList(const string& word, const vector<string>& wordList) {
return binary_search(wordList.begin(), wordList.end(), word);
}
// Handles the gameplay for two players
void playGame(const string& letters, const vector<string>& wordList) {
string word1, word2;
// Prompt Player 1 for their word
cout << "Player 1, enter your word: " << endl;
cin >> word1;
transform(word1.begin(), word1.end(), word1.begin(), ::toupper); // Convert to uppercase
// Prompt Player 2 for their word
cout << "Player 2, enter your word: " << endl;
cin >> word2;
transform(word2.begin(), word2.end(), word2.begin(), ::toupper); // Convert to uppercase
// Validate words for both players
bool valid1 = isWordValid(word1, letters) && isWordInList(word1, wordList);
bool valid2 = isWordValid(word2, letters) && isWordInList(word2, wordList);
// Display validation results
if (!valid1) cout << "Player 1's word is not valid." << endl;
if (!valid2) cout << "Player 2's word is not valid." << endl;
// Determine the winner
if (valid1 && valid2) {
if (word1.length() > word2.length()) {
cout << "Player 1 wins!" << endl;
} else if (word2.length() > word1.length()) {
cout << "Player 2 wins!" << endl;
} else {
cout << "Tie game." << endl;
}
} else if (valid1) {
cout << "Player 1 wins!" << endl;
} else if (valid2) {
cout << "Player 2 wins!" << endl;
} else {
cout << "Tie game." << endl;
}
}
// Finds the longest valid word that can be created with the given letters
string findLongestWord(const string& letters, const vector<string>& wordList) {
string longest;
for (const string& word : wordList) {
if (word.length() > longest.length() && isWordValid(word, letters)) {
longest = word; // Update longest word if criteria are met
}
}
return longest; // Return the longest word
}
// Main function to control the game flow
int main() {
int seedValue; // Seed value for random number generator
cout << "Enter random seed: ";
cin >> seedValue;
seed(seedValue); // Seed the random number generator
cout << endl;
vector<string> wordList; // Vector to store words from the dictionary
ifstream wordFile("words.txt"); // Open the dictionary file
string word;
while (wordFile >> word) { // Read each word from the file
wordList.push_back(word);
}
wordFile.close(); // Close the file
sort(wordList.begin(), wordList.end()); // Sort the word list for binary search
char playAgain; // To determine if players want to play another round
cout << "Let's play Countdown!" << endl;
do {
int numVowels;
// Ask the player for the number of vowels
do {
cout << "How many vowels would you like (3-5)? " << endl;
cin >> numVowels;
if (numVowels < 3 || numVowels > 5) {
cout << "Invalid number of vowels." << endl;
}
} while (numVowels < 3 || numVowels > 5);
string letters = generateLetters(numVowels); // Generate random letters
cout << "The letters are: " << letters << endl;
playGame(letters, wordList); // Play the game
string longestWord = findLongestWord(letters, wordList); // Find the longest word
cout << "The longest possible word is: " << longestWord << endl;
cout << "Do you want to play again (y/n)? " << endl;
cin >> playAgain; // Ask if the players want to replay
} while (playAgain == 'y' || playAgain == 'Y'); // Continue if they choose 'y'
return 0; // End the program
}
// Generates a random number between min and max
int chooseRandomNumber(int min, int max) {
return rand_u32() % (max + 1 - min) + min;
}
// Returns a string containing all vowels with their respective frequencies
string fullSetOfVowels() {
return string(15, 'A') + string(21, 'E') + string(13, 'I') + string(13, 'O') + string(5, 'U');
}
// Returns a string containing all consonants with their respective frequencies
string fullSetOfConsonants() {
return string(2, 'B') + string(3, 'C') + string(6, 'D') + string(2, 'F') + string(3, 'G') +
string(2, 'H') + string(1, 'J') + string(1, 'K') + string(5, 'L') + string(4, 'M') +
string(8, 'N') + string(4, 'P') + string(1, 'Q') + string(9, 'R') + string(9, 'S') +
string(9, 'T') + string(1, 'V') + string(1, 'W') + string(1, 'X') + string(1, 'Y') +
string(1, 'Z');
}