I have the following code for a game of hangman that imports the words it uses from a flat text file called "words.dat". It works well. How though can I convert the words when they are brought in from the file to into pig latin to be used by the app? Any help is greatly appreciated.
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define MAX_WORD_SIZE 15
#define MAX_WORDS 255
void LoadFile();
void RunGame();
void DrawGallows(int State);
typedef char String[MAX_WORD_SIZE];
String Words[MAX_WORDS - 1];
int Count;
void main()
{
char Continue = 'Y';
cout<<"Hangman"<<endl;
LoadFile();
while(Continue == 'Y')
{
RunGame();
cout<<endl<<"Do you want to play again (Yes or No)? ";
cin>>Continue;
Continue = toupper(Continue);
}
cout<<endl<<"Thanks for playing."<<endl;
}
void LoadFile()
{
char C;
ifstream Datfile;
Count=0;
Datfile.open("words.dat");
while((C=Datfile.peek()) != EOF)
{
Datfile>>Words[Count++];
if(Count > MAX_WORDS - 1)
{
cout<<endl<<"Too many words in the file, stopping with "
<<MAX_WORDS<<" Words."<<endl;
Count = MAX_WORDS;
break;
}
}
Count--;
Datfile.close();
}
void RunGame()
{
int Word;
int Size;
int State=1;
int Subscript=0;
char Guess[MAX_WORD_SIZE];
char Copy[MAX_WORD_SIZE];
char Letter;
int Correct=0;
srand((unsigned)time( NULL ));
Word = rand() % Count;
strcpy(Copy,Words[Word]);
Size = strlen(Words[Word]);
for(; Subscript < Size; Subscript++)
{
Guess[Subscript] = '-';
}
Guess[Subscript] = '\0';
while(State!=7)
{
DrawGallows(State);
cout<<Guess<<endl;
cout<<"Guess a letter :";
cin>>Letter;
Letter = tolower(Letter);
for(Subscript = 0; Subscript < Size; Subscript++)
{
if(Copy[Subscript] == Letter)
{
Guess[Subscript] = Letter;
Correct = 1;
cout<<endl<<"Good Guess!";
if(strcmp(Words[Word],Guess) == 0)
{
cout<<endl<<"You won!";
return;
}
}
}
if(Correct == 0)
{
cout<<endl<<"Sorry, bad guess!";
State++;
}
Correct = 0;
}
DrawGallows(State);
cout<<"The word was : "<<Words[Word]<<endl<<endl;
}
void DrawGallows(int State)
{
if(State==7)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" /|\\ | "<<endl
<<" | | "<<endl
<<" / \\ | "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
else if(State==6)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" /|\\ | "<<endl
<<" | | "<<endl
<<" \\ | "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
else if(State==5)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" /|\\ | "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==4)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" /|\\ | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==3)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" /| | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==2)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" O | "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==1)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
}