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 170 171 172
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;
const int MAX_TRIES=8;
int triesleft=8,score=100;
int letterFill(char, string, string&);
string displayHangman(int num_of_wrong_guesses);
string displayHangingStation(int x);
int main()
{
string name;
char letter;
int num_of_wrong_guesses = 0;
string word;
string words[] =
{
"apple",
"orange",
"car",
"truck",
"bicycle",
"cat",
"dog",
"snake",
"rock",
"sock"
};
///choose and copy a word from array of words randomly
srand(time(NULL));
int n = rand() % 10;
word = words[n];
/// Initialize the secret word with the * character.
string unknown(word.length(), '*');
/// welcome the user
cout << "\n\n WELCOME TO THE HANGMAN GAME";
cout << "\n\n You have 8 attempts to try and guess the word.";
/// Loop until the guesses are used up
while (num_of_wrong_guesses <= triesleft)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
/// Fill secret word with letter if the guess is correct,
/// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Whoops! That letter is already there!" << endl;
cout << displayHangingStation(num_of_wrong_guesses);
cout << displayHangman(num_of_wrong_guesses) << endl;
}
else
{
cout << endl << "Good job you found a new letter!" << endl;
}
/// Tell user how many guesses has left.
cout << "You have " <<triesleft--;
cout << " guesses left." << endl;
/// Check if user guessed the word.
if (word == unknown)
{
/if (word.length()-num_of_wrong_guesses==word.length())
{score=100;}
else if (word.length()-num_of_wrong_guesses==word.length()-1)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-2)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-3)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-4)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-5)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-6)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-7)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses>=word.length()-8)
{score=word.length()/num_of_wrong_guesses;}
cout << word << endl;
cout << "Yeah! You got it!\nYour Score was "<<score<<"%"<< endl;
break;
}
}
if (num_of_wrong_guesses >= triesleft)
{
cout << "\nSorry you lose :(\nYour Score was "<<score<<"%"<< endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
/** Take a one character guess and the secret word, and fill in the
unfinished guess word. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
}
int letterFill(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
/// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
/// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
string displayHangman(int x)
{
for (int i = 0; i < 10; i++)
{
if (x == 1)
{
return " O";
}
if (x == 2)
{
return " O\n |";
}
if (x == 3)
{
return " O\n |\n |";
}
if (x == 4)
{
return " O\n |\n |\n /";
}
if (x == 5)
{
return " O\n |\n |\n /\\";
}
if (x == 6)
{
return " O\n -|\n |\n /\\";
}
if (x == 7)
{
return " O\n -|-\n |\n /\\";
}
if (x == 8)
{
return " O\n -|-\n |\n /\\";
}
}
}
string displayHangingStation(int x)
{
string station = "\n___________\n|_________|\n |\n |\n";
return station;
}
|