hangman game problem
Jun 14, 2008 at 6:38pm UTC
i have a text document with 10 words to guess. i have the code already read a random word from the file. then the user guesses a letter and it finds whether or not if the letter is in the word. my problem is how can i have it remember the previous letter and outcome and builds on so it will complete the entire word. my teacher said this as a hint " I suggest you make a word of all blanks
then replace letters with the letters that are in the word to be guessed
you can use the position found by find to put the letter in the appropriate position." but how would i go about doing that any help would be greatly appreciated.
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
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
void drawing();
void guessword(string word);
int main()
{
int number;
int count = 0;
int position;
string word;
char letter;
ifstream infile;
infile.open("words.txt" );
srand(time(NULL)); //initializes the random number generator
while (count < 1)
{
number = rand()%10 +1; // rand returns a random integer from 0 to maxInt
count++;
}
for (count=0;count<number;count++)
getline (infile, word);
guessword(word);
cin.ignore(255,'\n' );
cin.get();
return 0;
}
void guessword(string word)
{
char letter;
int position;
for (int i=0;i<5;i++)
{
cout << "What letter would you like to guess?" ;
cin >>letter;
position = word.find(letter);
if (position > word.length())
cout<<letter<< " is not in the word " <<endl;
else
{
cout<< letter << " is in the word" <<endl;
if (position==0)
cout<<letter<<" _ _ _" <<endl;
else if (position==1)
cout<<"_ " <<letter<<" _ _" <<endl;
else if (position==2)
cout<<"_ _ " <<letter<<" _" <<endl;
else if (position==3)
cout<<"_ _ _ " <<letter<<endl;
}
}
}
void drawing(int number)
{
switch (number)
{
case 1:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | " <<endl;
cout << "_|______________" <<endl;
break ;
case 2:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ " <<endl;
cout << "_|______________" <<endl;
break ;
case 3: cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 " <<endl;
cout << "_|______________" <<endl;
break ;
case 4:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << "_|______________" <<endl;
break ;
case 5:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << "_|______________" <<endl;
break ;
case 6:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << " | / " <<endl;
cout << "_|______________" <<endl;
break ;
case 7:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << " | / \\ " <<endl;
cout << "_|______________" <<endl;
}
}
Last edited on Jun 15, 2008 at 5:40pm UTC
Jun 15, 2008 at 5:43pm UTC
any ideas, it should be easy but i need a hint to loop through and add onto the guessed word until its completed
Jun 15, 2008 at 7:07pm UTC
Somewhere you have a string that stores the word you want the user to guess (you named it "word", but I would have named it something like "word_to_guess").
And you have to keep track of what letters have already been guessed that: 1) are in the word, and 2) that are not in the word. Since either way the letter has already been guessed, this suggests to me a string to remember them ("letters_guessed").
But you also need to know how much of the puzzle has been solved. This suggests one more string "word_guessed_so_far" or somesuch.
You can then compare the "word_to_guess" and "word_guessed_so_far" to see if the user has completed the puzzle:
1 2
"hello world" <-- word_to_guess
" ello o l " <-- word_guessed_so_far
The last thing you need is a function that will display the word guessed so far to the user in a fancy way, like
_ e l l o _ o _ l _
Hope this helps.
Last edited on Jun 15, 2008 at 7:08pm UTC
Jun 15, 2008 at 7:44pm UTC
thanks for the help, i changed code and it enters the guessed letters into a blankword. how could i compare blankword[4] to word so if the word guessed so far is equal to the word to be guessed are equal then it prints out a congratulations or if not then you lose. also how should i call the drawings should i use a switch statement like i have and if so how should i call it to draw
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
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
void drawing(int position);
void guessword(string word);
int main()
{
int number;
int count = 0;
int position;
string word;
char letter;
ifstream infile;
infile.open("words.txt" );
srand(time(NULL)); //initializes the random number generator
while (count < 1)
{
number = rand()%10 +1; // rand returns a random integer from 0 to maxInt
count++;
}
for (count=0;count<number;count++)
getline (infile, word);
guessword(word);
cin.ignore(255,'\n' );
cin.get();
return 0;
}
void guessword(string word)
{
char letter;
int position;
string blankword[4];
blankword[0] = "_ " ;
blankword[1] = "_ " ;
blankword[2] = "_ " ;
blankword[3] = "_ " ;
int wrongguess = 0;
for (int i=0;i<6;i++)
{
cout << "What letter would you like to guess?" ;
cin >>letter;
position = word.find(letter);
if (position > word.length())
cout<<letter<< " is not in the word " <<endl;
else
{
cout<< letter << " is in the word" <<endl;
if (position==0)
{
blankword[0] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if (position==1)
{
blankword[1] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if (position==2)
{
blankword[2] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if (position==3)
{
blankword[3] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
}
}
}
void drawing(int position)
{
switch (position)
{
case 1:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | " <<endl;
cout << "_|______________" <<endl;
break ;
case 2:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ " <<endl;
cout << "_|______________" <<endl;
break ;
case 3: cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 " <<endl;
cout << "_|______________" <<endl;
break ;
case 4:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << "_|______________" <<endl;
break ;
case 5:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << "_|______________" <<endl;
break ;
case 6:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << " | / " <<endl;
cout << "_|______________" <<endl;
break ;
case 7:
cout << " ___________" <<endl;
cout << " | }" <<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |" <<endl;
cout << " | / \\ " <<endl;
cout << "_|______________" <<endl;
}
}
Jun 16, 2008 at 3:00pm UTC
Use a loop.
Topic archived. No new replies allowed.