Ok i am having trouble with my hang man game that i am creating for school.
Code listed below is what i have so far.
what i need to know is:
1: my function for deleting a word is not created yet, how do i go about searching the text document for a word given. this will also allow me to add to the add word function, in efforts to search the document before adding a word to stop double entries.
2: in the Game function inbetween lines 317 and 337 is about were it will deduct from nog(number of gueses) based on whether guess was correct or not. When running i thought i set it up so when you made a guess nog-- and when the gues was a corect letter it would then nog++. it seams that when you gues corectly it is skipping the nog-- comand.
3: In the game function i need to implement a menu that allows the user to decide between easy medium hard, I have not goten to this point yet because i am stuck trying to just get it to work.
4: I also have come across the problem of when you go to quit after playing it doesnt quit i think this may be because the way i have set the function up.
Any advice, help, links would be much appreciated. And by all means if i have gone about something in a wrong manner please help me out I have a tendancy of doing things harder than needed.
thank you in advance for anyone who helps me out. : )
CODE
// Hang Man :In the end suppos to be a working Hang Man game that can
// add, delete and search words seperated by Easy, Medium and Hard.
// and guess witch word has been randomly selected in a set amount
// of guesses.
//
// Still Needing drasic implementation!!!!!
//
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void viewWords();
void addWord();
void Menu();
void game();
void deleteWord();
int main()
{
// run menu program
Menu();
return 0;
}
//this is were the menu code will be
void Menu()
{
char option;
option = 'n';
// while loop untill quit is selected
while (option != 'q')
{
cout << "## ## ## ## #######\n";
cout << "## ## #### #### ## #\n";
cout << "## ## ###### ## ## ## \n";
cout << "###### ## ## ## ## ## \n";
cout << "###### ## ## ## ## ## \n";
cout << "## ## ###### ## ## ## ###\n";
cout << "## ## ## ## ## ## ## ##\n";
cout << "## ## ## ## ## ## #######\n";
cout << "\n";
cout << " ## ## #### ## \n";
cout << " ### ### ###### #### \n";
cout << " ####### ## ## ## ##\n";
cout << " ## ## ## ## ## ##\n";
cout << " ## ## ###### ## ##\n";
cout << " ## ## ## ## ## ##\n";
cout << " ## ## ## ## ## ##\n";
cout << "Please select from the options below:\n";
cout << "P:lay Game\n";
cout << "A:dd Words\n";
cout << "D:elete Words\n";
cout << "V:iew Words\n";
cout << "Q:uit\n";
cin >> option;
/*
void Menu()
{ char option;
cout << "Please select from the options below:\n";
cout << "P:lay Game\n";
cout << "A:dd Words\n";
cout << "D:elete Words\n";
cout << "V:iew Words\n";
cout << "Q:uit\n";
cin >> option;
// while loop untill quit is selected while (option != 'q')
*/
switch (option)
{
case 'P':
game();
break;
case 'p':
game();
break;
case 'A':
addWord();
break;
case 'a':
addWord();
break;
case 'D':
deleteWord();
break;
case 'd':
deleteWord();
break;
case 'V':
viewWords();
break;
case 'v':
viewWords();
break;
case 'Q':
cout << "Quiting\n";
break;
case 'q':
cout << "Quiting\n";
break;
default:
cout << "Sorry that is not an option. ";
}
}
}
void addWord()
// Program needs to be able to read the word and only add it if it does not already exist.
{
//clear screen
system("CLS");
// opening file to be used
ofstream fout;
fout.open("library.txt", ios::app);
char addedWord[25];
char level; // label for word to determine Easy, Medium or Hard.
// put in just incase something has happened to the library file the user will be notified.
if (!fout.good())
{
cout << "Error creating library.txt";
}
cout << "Please type the word you wish to be added: ";
cin >> addedWord;
int length = strlen(addedWord);
if (length <= 5)
level = 'E';
else if(length <= 10)
level = 'M';
else if(length > 10)
level = 'H';
fout<<level<<addedWord<<endl;
cout << "You have added "<< addedWord << " to the list of words in the Library\n";
// close open file
fout.close();
// Here i want to implement a looping code to ask if user wants to add another word
// rather than having to go back to the menu.
}
void viewWords() // This Section will change to Wich words would you like to view, Easy, Meduim, Hard.
// at that point the program will pull up the words listed by the dificulty varable
// listed with the word in library file.
{
//clear screen
system("CLS");
ifstream fin("library.txt");
// declaring varaiables to be used
string words;
// Checks to ensure the library file is intact.
if (!fin.good())
{
cout << "Cannot find library.txt" << endl;
}
// need to adjust so that you specify wich words to display, easy medium hard
while(! fin.eof())
{
getline(fin, words);
cout << words << endl;
}
system("PAUSE");
cout<< "\n";
fin.close();
// want program to ask if user would like to display again
}
void game()
{
system("CLS");
void startGraf();
void gallowGraf();
void ropeGraf();
void headGraf();
void bodyGraf();
void armsGraf();
void deadGraf();
void youWon();
// I want the search for word code in a seperate function
char word[25];
int i;
int x;
char c;
ifstream fin("library.txt");
if(!fin)
{ //clrscr();
cout<<"File missing, aborting.";
system("pause");
}
for (i=0;!fin.eof();i++) fin.getline(word,25);
fin.close();
do {x=rand();}
while(x>i || x<0);
ifstream finn("library.txt");
for (i=0;!finn.eof();i++)
{finn>>c; finn.getline(word,25);
if (x==i)
break;}
finn.close();
int L;
int nog;
int n;
int d;
char ch;
char blankWord[25];
L=strlen(word);
char choosen[25]="\0";
n=0;d=0;
for(i=0;i<=24;i++) // define blank word to display * if letter not guessed.
{
if (word[i]=='\0')
{blankWord[i]='\0';break;}
if (word[i]== ' ')
{blankWord[i]=' '; n++;}
if (word[i]!=' ')
blankWord[i]='*';
}
nog = 7;
do{ system("CLS");
there:
if (d!=0)
switch (nog)
{
case 6:
startGraf();
break;
case 5:
gallowGraf();
break;
case 4:
ropeGraf();
break;
case 3:
headGraf();
break;
case 2:
bodyGraf();
break;
case 1:
armsGraf();
break;
}
cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
cout<<"\n\n\n\t\t\t "<<blankWord<<"\n\n\nYou have "<<nog<< " guesses left, choose a letter : ";
cin>>ch; cin.get();
for (i=0;i<25;i++)
if (choosen[i]==ch)
{
system("CLS");
cout<<"\a\t\t !!You have choosen "<<ch<<" already!!\n";goto there;
}
if (choosen[i] !=ch)
{
choosen [d]=ch;
choosen [d+1]=',';
d+=2;
nog = (nog - 1);
}
for (i=0;i<=24;i++)
if (word[i]==ch)
{
blankWord[i]=ch;
nog = (nog + 1);
}
if (!strcmpi (blankWord,word))
youWon();
// this is not working corectly!
}while(nog>0 || !strcmpi (blankWord,word));
deadGraf();
// need to have computer pick a word based on level of difficulty supplied by user
// also need to figure out how to set in the loop to display the apropriate graphic
// based off of how many guesses are left (nog variable)
}
void deleteWord() // Here user will enter a word, program then takes word searches library file
// and removes the word if it is there. in either case gives conformation
// if the word was removed or if not because it does not exist.
{
cout << "This function is not yet working\n";
}
void startGraf()
{
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<" \n";
cout <<"_________________________\n";
cout <<"#########################\n";
}
void gallowGraf()
{
system("CLS");
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
}
void ropeGraf()
{
system("CLS");
cout <<" !:::!!!!!!!!!!!\n";
cout <<" : !!!\n";
cout <<" : !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
}
void headGraf()
{
system("CLS");
cout <<" !:::!!!!!!!!!!!\n";
cout <<" : !!!\n";
cout <<" : !!!\n";
cout <<" ~~~ !!!\n";
cout <<" (x x) !!!\n";
cout <<" (o) !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
}
void bodyGraf()
{
system("CLS");
cout <<" !:::!!!!!!!!!!!\n";
cout <<" : !!!\n";
cout <<" : !!!\n";
cout <<" ~~~ !!!\n";
cout <<" (x x) !!!\n";
cout <<" (o) !!!\n";
cout <<" aaa !!!\n";
cout <<" aaa !!!\n";
cout <<" aaa !!!\n";
cout <<" a !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
}
void armsGraf()
{
system("CLS");
cout <<" !:::!!!!!!!!!!!\n";
cout <<" : !!!\n";
cout <<" : !!!\n";
cout <<" ~~~ !!!\n";
cout <<" (x x) !!!\n";
cout <<" a (o) a !!!\n";
cout <<" a aaa a !!!\n";
cout <<" aaaaa !!!\n";
cout <<" aaa !!!\n";
cout <<" a !!!\n";
cout <<" !!!\n";
cout <<" !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
}
void deadGraf()
{
char playagain;
system("CLS");
cout <<" !:::!!!!!!!!!!!\n";
cout <<" : !!!\n";
cout <<" : !!!\n";
cout <<" ~~~ !!!\n";
cout <<" (x x) !!!\n";
cout <<" a (o) a !!!\n";
cout <<" a aaa a !!!\n";
cout <<" aaaaa !!!\n";
cout <<" aaa !!!\n";
cout <<" aaa !!!\n";
cout <<" a a !!!\n";
cout <<" ddd bbb !!!\n";
cout <<"______________________!!!\n";
cout <<"#########################\n";
cout <<" \n";
cout <<" GAME OVER \n";
cout <<" \n";
cout <<"#########################\n";
cout <<"\n";
cout <<"Would you like to Play again? Y/N\n";
cin >> playagain;
switch (playagain)
{
case 'Y':
game();
break;
case 'y':
game();
break;
case 'N':
break;
case 'n':
break;
}
}
void youWon()
{
char again;
system("CLS");
cout <<"\n";
cout <<" ,~'*````````````*'~,\n";
cout <<" Congratulations \n";
cout <<" ,~'*............*'~,\n";
cout <<"\n";
cout <<" Would you like to play again? Y/N\n";
cin >> again;
switch (again)
{
case 'Y':
game();
break;
case 'y':
game();
break;
case 'N':
// need beter way to exit
break;
case 'n':
// need better way to exit
break;
}
}
// Need to make this function work instead of having it in the game function
/*char selectingWord()
{
char word[25];
int i;
int x;
char c;
ifstream fin("library.txt");
if(!fin)
{ //clrscr();
cout<<"File missing, aborting.";
system("pause");
return 0;
}
for (i=0;!fin.eof();i++) fin.getline(word,25);
fin.close();
do {x=rand();}
while(x>i || x<0);
ifstream finn("library.txt");
for (i=0;!finn.eof();i++)
{finn>>c; finn.getline(word,25);
if (x==i)
break;}
finn.close();
}*/
edit: added [code] tags ~ jayman9