Jan 2, 2014 at 12:21pm UTC
Dear friends....please help me with this code..i found pieces of code and put it together but it doesnt work..
the program works but it wont draw a hangman..
please help..
tnx...
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
void drawgallows(int);
string THE_WORD; // riječ koja se pogađa
int wrong;
string soFar;
string used;
bool match(char letter, string word);
char askGuess(string usedLettersStr);
bool playAgain();
int state=0;
int main()
{
srand(time(0));
vector<string> words; //popis riječi koje se pogađaju
words.push_back("POGODAK");
words.push_back("SNIJEG");
words.push_back("OTORINOLARINGOLOGIJA");
words.push_back("KNJIGA");
words.push_back("ASTRONAUT");
words.push_back("PROGRAM");
cout << "DOBRO DOSLI U IGRU VJESALA. Sretno!\n";
bool done = false;
do
{
const int MAX_WRONG = 6; // maksimalan broj krivih pokušaja
random_shuffle(words.begin(), words.end());
THE_WORD = words[0];
soFar = string(THE_WORD.size(), '-');
used = ""; // program ispisuje slova koja smo rekli
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nPreostalo jos " << (MAX_WRONG - wrong) << " pokusaja.\n";
cout << "\nIskoristili ste sljedeca slova:\n" << used << endl;
cout << "\nRijec je:\n" << soFar << endl;
used += askGuess(used);
}
if (wrong == MAX_WRONG)
{
cout << "\nTi si objesen!";
}
cout << "\nTocna rijec je bila " << 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\nUnesi slovo: ";
cin >> guess;
guess = toupper(guess);
while (match(guess, used))
{
cout << "\nTo slovo je vec pogodjeno! " << guess << endl;
cout << "Unesi slovo: ";
cin >> guess;
guess = toupper(guess);
}
if (match(guess, THE_WORD))
{
cout << "To je tocno! " << guess << " je u rijeci.\n";
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
else
{
cout << "Zao mi je, " << guess << " nije u rijeci.\n";
++wrong;
drawgallows(state);
}
return guess;
}
void drawgallows(int state)
{
if(state==6)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | / \\ "<<endl
<<" |Your Dead "<<endl
<<" ============"<<endl<<endl;
}
else if(state==5)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | \\ "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
else if(state==4)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(state==3)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /| "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(state==2)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(state==1)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
return;
}
//Will draw the gallows
bool playAgain()
{
char again;
cout << "\n\nZeliš li ponovo igrati? <y/n>: ";
cin >> again;
cin.clear();
cin.ignore();
again = toupper(again);
system("cls");
return (again == 'Y');
}
Jan 2, 2014 at 7:38pm UTC
it would be much easier if you use a file, and pull words from text to fill the vector
Jan 3, 2014 at 10:56am UTC
tnx for the reply but i dont now how to do that...i need this for passing the exam in college...can you maybe help me?
Jan 3, 2014 at 12:32pm UTC
You have to increment the "state"-variable ( ++state ) or change the line drawgallows(state);
to
drawgallows(wrong);
But then you should remove the declaration of "state", because the variable is not in use.