#include <iostream>
using namespace std;
#include <string>
#include <cctype>
void DrawGallows(int );
int main()
{
char solution[20]; //holds solution
char blank[20]; //holds "*"'s for unsolved letters
int counter = 0; //general-use counter
int right = 0; //1 = right guess, 0 = wrong guess.
char guess;
cout<<"Enter phrase 20 chars or less."<<endl;
cin.getline(solution, 20);
int puzzLength = strlen(solution); //finds lengtrh of puzzle, stores INT value to puzzlength
//convert puzzle to full uppercase
for (counter = 0; counter < puzzLength; counter++){
solution[counter] = toupper(solution[counter]);
}
//done converting
strcpy(blank, solution); //copy solution to the 'blank' array
for (counter = 0; counter < puzzLength; counter++) { //converts characters to *'s to represent blanks
if (isalnum(solution[counter])) blank[counter] = '*';
else blank[counter] = solution[counter];
} //closes for loop
while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
cout<<"Enter a guess."<<endl;
cin>>guess;
guess = toupper(guess);
//cbeck guess!
int State =1;
for (counter = 0; counter <= puzzLength; counter++) {
if (guess == solution[counter]) {
blank[counter] = guess; //fill in the puzzle with the letter
}
if (guess != solution[counter])
{
State++;
DrawGallows(State);
}
} //close loop, done checking guess
} //game is over.
cout<<"Winner!";
cin.get();
return 0;
}
void DrawGallows(int State)
{
if(State==6)
{
// The \\ will translate as '\' because it is a special char
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;
}
}
THIS IS MY CODE.. AND I HAVE PROBLEMS WITH THAT :
1) WHEN I INPUT THE WRONG CHARACTER, IT RUNS UNTIL STATE =5 ,NOT AS IT SHOULD RUNS AT STATE= 1+1 = 2... HOW CAN i FIX THAT
THIS IS MY SAMPLE RUN :
Enter phrase 20 chars or less.
DSADS
Enter a guess.
D
+----+
| |
| O
| |
|
|
=============+----+
| |
| O
| /|
|
|
=============+----+
| |
| O
| /|\
|
|
=============+----+
| |
| O
| /|\
| \
|
============Enter a guess.
2) I JUST WANNA TO CONFIRM, IF I WANNA TO LINK THIS FILE TO MY MAIN FILE .CPP . I SHOULD NAME THIS FILE .H AND DEFINE IT IN THE MAIN FILE, AND USE THE FUNCTION?? THE FUNCTIONS DRAWGALLOW : DO I HAVE TO DECLARE AND SET UP THINGS LIKE THAT IN THE MAIN FILE AND IN THE .H FILE SEPERATELY. OR I CAN COMBINE THEM. BUT I JUST CAN YOU THE FUNCTION FROM THE .H FILE TO THE MAIN FILE. NOT FROM THE MAIN FILE TO THE .H FILE RITE?