#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
string THE_WORD; // guess word
int wrong;
string soFar;
string used;
bool match(char letter, string word);
char askGuess(string usedLettersStr);
bool playAgain();
int main() //main
{
srand(time(0)); //gets the random word
vector<string> words; // list of words to get
words.push_back("BROWN");
words.push_back("GOLD");
words.push_back("GREY");
words.push_back("SILVER");
words.push_back("BLACK");
words.push_back("WHITE");
words.push_back("PURPLE");
words.push_back("PINK");
words.push_back("YELLOW");
words.push_back("GREEN");
words.push_back("RED");
words.push_back("BLUE");
//title
cout<<"\n\n";
cout<< "\t## ## #### ## ## ### ## ## #### ## ##\n";
cout<< "\t## ## ## ### ### ## ## ### ### ## ### ### ##\n";
cout<< "\t###### ####### ## # ## ## ### ## # # ## ####### ## # ##\n";
cout<< "\t## ## ## ## ## ### ## # ## ## ## ## ## ## ###\n";
cout<< "\t## ## ## ## ## ## #### ## ## ## ## ## ## ##\n\n";
cout << "\t\t\t Hangman 2010 - Wayne Daly\n";
cout<< "\n\n\nHINT: The word is a colour\n";
// loop starts here
bool done = false;
do
{
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed before death, change your total lives here
random_shuffle(words.begin(), words.end());
THE_WORD = words[0]; // word to guess
soFar = string(THE_WORD.size(), '*'); // word guessed so far
used = ""; // letters already guessed
// loop for current word
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong)
<< " Lives left.\n";
cout << "\nYou've picked the following correct letters:\n" << used
<< endl;
cout << "\nSo far, the word is:\n\n" << soFar << endl;
used += askGuess(used);
{
} // end of while
}
if (wrong == MAX_WRONG) //when all your lives are gone
{
cout<<"\n\n";
cout<<" +----+ \n";
cout<<" | | \n";
cout<<" | O \n";
cout<<" | /|\\ \n";
cout<<" | / \\ \n";
cout<<" |Your Dead \n";
cout<<" ============\n\n";
}
cout << "\nThe word was " << THE_WORD << endl;
} while (playAgain());
return 0;
}
inline bool match(char letter, string word)
{
return (word.find(letter) != string::npos);
}
char askGuess(string usedLettersStr)
{
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
while (match(guess, used))
{
cout << "\nYou have already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
if (match(guess, THE_WORD))
{
cout << "That's right! " << guess << " is in the word.\n";
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
// update soFar to include new guessed letter
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
return 0;
}
bool playAgain() //play again while clearing system
{
char again;
cout << "\n\nWould you like to play again? <y/n>: ";
cin >> again;
cin.clear(); //clear and ignore cin
cin.ignore();
again = toupper(again);
system("cls");
return (again == 'Y');
}
2 replies to this topic
#1
Posted 02 April 2010 - 01:52 PM
It works good if you get the word right, but when it starts, if i get all my guesses wrong and then type y to restart it does not, it just stays put..
|
|
|
#2
Posted 02 April 2010 - 05:35 PM
I think it is because you never reset the variable "wrong" after the user says they want to play again. Also, my linux machine doesn't know the system command "cls", so you might take that into account.
This is a pretty nifty program though. I like it!
This is a pretty nifty program though. I like it!
I don't document code. If it was hard to write, it should be hard to read ;)
#3
Posted 03 April 2010 - 09:36 AM
Thanks, i have reset wrong at the end and now it works, great help!
about the linux i found that system ("clear") works, but i couldnt find a universal code for the job =P
but thanks again for the help!
about the linux i found that system ("clear") works, but i couldnt find a universal code for the job =P
but thanks again for the help!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









