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
|
#include <iostream>
using namespace std;
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_s(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<<endl<<"Solution phrase is: "<<solution<<"."<<endl;
cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
cout<<"Enter a guess."<<endl;
cin>>guess;
guess = toupper(guess);
//cbeck guess!
for (counter = 0; counter <= puzzLength; counter++) {
if (guess == solution[counter]) {
blank[counter] = guess; //fill in the puzzle with the letter
}
} //close loop, done checking guess
} //game is over.
cout<<"Winner!";
cin.get();
return 0;
}
|
I tried to cut out as much 'unneeded' code as I could, such as counting mistakes, outputting the wrong letters, and drawing the hangman. The problem I've having is when it comes to checking whether the user has finished solving the puzzle. I can't seem to find a way to make the comparison, at least the way I'm accepting hte user input and have my arrays set up currently.
Here's a breakdown of what happens in this code, which doesn't include clearing the screen, counting scores, etc.:
1) Player 1 inputs a phrase, stored into ::char solution[20]::
2) ::solution:: is converted to uppercase via a loop.
3) ::solution:: is copied to ::char blank[20]:: via strcopy_s();
4) using a loop and isalnum();, each alphanumeric character in ::blank:: is changed into a '*' (using a _ results in a big long line, like so: ____)
5) Player 2 inputs a character, which is converted to uppercase and stored into ::char guess::
6) a loop checks each character in ::solution::. if the guess matches, the corresponding letter in ::blank:: is changed.
7) Number 6) loops until ::solution:: == ::blank::.
Example of use:
1) P1 enters the phrase 'c-o-p-s' (solution[20] = 'c-o-p-s')
2) Program converts cops to COPS (solution[20] = 'C-O-P-S')
3) Program copies solution[20] to blank[20] (blank[20] = 'C-O-P-S)
4) Program changes alpha-numeric characters to *'s (blank[20] = '*-*-*-*')
5) P2 enters a guess, C
6) Program checks guess against solution[20]. finds a match, and changes the corresponding letter in blank[20]) (solution[20] = C-O-P-S, blank[20] = C-*-*-*)
7) P2 enters O, P, S...
8) solution[20] = C-O-P-S, blank[20] = C-O-P-S...on the screen.
When it comes to making the actual comparison, however, it apparently is not equal, as it would break the loop if this were the case. In the actual code, I don't use the arrays in the while loop, but I still can't get the program to find the two arrays to be equal. In the output, I added a "." after the cout statements, to make sure there's no trailing spaces. Can anyone give me a hand?