#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
const char File[] = "dictionary.txt";
class Dictionary {public:Dictionary()
{
retrieve();
}
vector<string> getWords()
{
return words;}
int Size(){
return words.size();}
private:vector<string> words;
int retrieve(){
fstream file;
string myWord;
file.open(File, ios::in);
if (file.fail()){
cout << "Error! Input file cannot be opened.\n\n" ;return 0;}
file >> myWord;
while (!file.eof()){
words.push_back(myWord);
file >> myWord;
}
file.close();
}
};
class Scramble {
private:vector<string> DictionaryWords;
int size;bool Check (string T, string S);
public:Scramble(Dictionary myLibrary)
{
size= myLibrary.Size();
DictionaryWords = myLibrary.getWords();
}
string getWord(int i){
return DictionaryWords.at(i);}
void Descramble (string Scrambled);};
void Scramble::Descramble(string Scrambled){
bool flag=false; // stop once found
for(int i=0;i < DictionaryWords.size() && !flag; i++){
if(DictionaryWords.at(i).size()==Scrambled.size())if(Check(Scrambled,DictionaryWords.at(i))){
cout << "The word is: " << DictionaryWords.at(i)<< endl; flag=
true;}
}
if (flag)return; else{
cout<< "The word: " << Scrambled <<" is not found"<< endl;}
}
bool Scramble::Check(string T, string S){
int n=T.size();int Found=0; //count matches
string temp=S;
for (int i=0; i<n; i++){
bool flag = true; for (int j=0; j < n &&flag;j++)if(T[i]== temp[j]){ temp[j]=' ';flag=false;Found ++;} // matched
}
if (Found==n)return true; else return false;}
void ClearScreen();
void flush(){
char ch; while (ch=cin.get()!= '\n');}
int main()
{
Dictionary scrmbl;
string Scrambled;
char choice;cout << "*********Scramble Game**********" <<endl << endl;
while (true){cout << "Please choose one of the following options:" << endl;cout <<
"1- Enter a word to descramble" << endl;cout << "2- Quit" << endl << endl;cin>>choice;
ClearScreen();
if (choice == '1'){
cout << "Please Enter a New Scrambled Word: ";cin >> Scrambled;
Scramble test(scrmbl);
test.Descramble(Scrambled);
}
else if(choice == '2'){
cout << "Thank you for playing. Press any key to exit.\n";getchar();
return 0;}
else{
cout << "Invalid entry. Please select again." << endl;}
}
}
void ClearScreen()
{
#ifdef _WIN32system("cls");//windows
#endif
}