guys i have made a hangman game and its working well!
the problem is that when i repeat a letter that is already guessed, it will decrement my guesses left. P.S : don 't look at the comments its not english (i am lebanese).
#include <iostream>/**for cout, cin...*/
#include <string>/**string **/
#include <cstdlib>/**random number generating**/
#include <vector>/**to use vectors**/
#include <algorithm>/**to find letter and arrange them**/
#include <ctime>/**aso for random number**/
#include<windows.h>/**system("....")**/
#include <cctype>/**to convert to uppercase letter**/
void hangmanboard(void);
void hangmanboard2(void);
usingnamespace std;
int main (void)
{
system ("color 1a");
constint MAX_WRONG = 8;
vector<string>words;
words.push_back ("GUESS");
words.push_back ("HANGMAN");
words.push_back ("DIFFICULT");
words.push_back ("PROGRAMMING");
srand (static_cast <unsignedint> (time(NULL)));/**seed the generator**/
random_shuffle (words.begin() , words.end());/**bi 5alle y5arbet l aseme**/
const string THE_WORD = words[0];/**bi na2e awal ra2em. w kel marra awal ra2em byer8ayyar**/
int wrong = 0;/**kam marra 8allat**/
string soFar(THE_WORD.size() , '-');/**bi 5ale l a7rof ysiro -**/
string used = "";/**chou henne l a7rof li sta3malon**/
cout<<"welcome to hangman ! "
<<"good luck"<<endl;
/**main loop **/
while ((wrong<MAX_WRONG) && (soFar != THE_WORD))/**hayde kermel t3id l loop ta ken 7ezer am 5eser**/
{
cout<<"you have "<<(MAX_WRONG-wrong)<<" incorrect guesses left"<<endl;/**addeh 3endo tries**/
cout<<"you have used the following letters : \n \n "
<<used<<endl
<<"so far, the word is "<<soFar<<endl;
char guess;/**declaring l variable yalli ra7 yna2ia le3eb**/
cout<<"enter your guess :"<<endl;
cin>>guess;
guess = toupper(guess);/**bi 7ett enno l guess hiyye mettel el GUESS (uppercase)**/
used += guess;/**bi 7ett l gess ma3 l guesse yalli na2ehon l user **/
if (THE_WORD.find(guess) != string::npos)/** bi nabbech 3al guess bel word wel != string::npos houwe metel != did not find**/
{
cout<<"that 's right ! "<<guess
<<"is in the word !"<<endl;
/**update so far to include
newly guessed letter**/
for (int i=0 ; i<THE_WORD.length() ; ++i)/**bya3mel update lal game w bi 7ett l guess ma7all l letter l le2eh**/
{
system ("cls");
if (THE_WORD[i] == guess)/**iza le2a ci letter metel l guess...**/
{
soFar[i] = guess;/**bi 7ett ma7al l -, l guess**/
}
}
}
else
{
cout<<"sorry, "<<guess<<" is not in the word"<<endl;
++wrong;/**bi zid l wrong number of guesses**/
system ("cls");
}
}
/**SHUT DOWN**/
if (wrong == MAX_WRONG)/**iza 5allas l tries**/
{
system ("color 40");
cout<<"\n you 've been hanged \n \n \n";
hangmanboard();
}
else
{
system("color 1a");
cout<<"you guessed it"<<endl;
hangmanboard2();
}
cout<<"the word was: "<<THE_WORD<<endl;
return 0;
}
void hangmanboard()
{
cout<<"+--------+ " <<endl;
cout<<"| | " <<endl;
cout<<"| 0 " <<endl;
cout<<"| \\|/ " <<endl;
cout<<"| | " <<endl;
cout<<"| /|\\ " <<endl;
cout<<"| | " <<endl;
cout<<"| " <<endl;
cout<<"==================="<<endl;
}
void hangmanboard2()
{
cout<<"+--------+ " <<endl;
cout<<"| | " <<endl;
cout<<"| " <<endl;
cout<<"| " <<endl;
cout<<"| " <<endl;
cout<<"| " <<endl;
cout<<"| " <<endl;
cout<<"| " <<endl;
cout<<"==================="<<endl;
}