现在我正试图让我的记分卡为刽子手工作。这是一个我可以玩多次的游戏,当我在游戏菜单中按2时,我想让它显示我的最低分数。出于某种原因,我的逻辑不起作用。有人可以帮忙吗?我附上了为此工作所需的3部分代码。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);
int main()
{
int intro;
int bodyPart = 0;
ifstream wordIn; //file stream read in
wordIn.open("WordList.txt"); //list of words used in this game
//if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
char letter; //letter guessed
int numWrongGuesses = 0; //counts the number of wrong guesses
string theWord;
string words[13];
int play = 0;
bool run = true;
int makeYourSelection = 0;
int bestScore = 11;
while (run == true)
{
int attemptsGame = 0;
// put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
cout << " WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
cout << " Press 1 to play the game, press 2 to exit to the scorecard \n\n";
cout << " You have 10 attempts to guess the words before you get hung \n\n";
cin >> play;
while (play == 1)//loops while user has entered 1, 0 exits the game
{
for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
{
wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
cout << words[i] << endl;
}
srand(time(NULL));//to get a random word
int n = rand() % 12;
theWord = words[n]; //pulls word from file
wordIn.close();
string mystery(theWord.length(), '*'); //replaces word letters with asterisks
while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
{
cout << mystery << endl << endl;
cout << "You now have the length of the word represented by the *'s. \n\n";
cout << "Guess a letter \n\n";
cin >> letter;
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
bodyPart++;
poorStiff(bodyPart);
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
numWrongGuesses++;
attemptsGame++;
}
else
{
for (int i = 0; i < mystery.length(); i++)
{
if (theWord[i] == letter)
{
mystery[i] = letter;
}
}
cout << "You have found one of the letters. Congratulations! \n\n";
cout << "You have: " << maxAttempts - numWrongGuesses;
cout << " guesses left \n\n" << endl;
}
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
}
}
if (numWrongGuesses == maxAttempts) //when you run out of guesses
{
cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
cout << "The word you were trying to guess was " << theWord << endl;
poorStiff(bodyPart);
}
cin.ignore();
cin.get();
break;
}
while (play == 2)
{
cout << "Best Scores: \n\n";
cout << bestScore << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
{
int i;
int hits = 0; //letter hits within the word
int many = theWordSecret.length();
for (i = 0; i < many; i++)
{
if (guess == guessWord[i])
return 0;
if (guess == theWordSecret[i])
{
guessWord[i] == guess;
hits++;
}
}
return hits;
}
void poorStiff(int bodyPart)
{
if (bodyPart == 1)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 2)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 3)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 4)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / " << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 5)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 6)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 7)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 8)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 9){
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / " << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 10)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / \." << endl;
cout << "_______________" << endl;
}
}
答案 0 :(得分:1)
要启动启用编译器警告,您可以通过快速查看代码获得许多警告。
更改此
guessWord[i] == guess;
到这个
guessWord[i] = guess;
此时
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
...
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
}
else
{
...
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
...
}
}
在这里,当你输入第一个if时,你也会输入第二个if,这没有意义。这是因为theWord
和mystery
都是空字符串!
另请注意,中断应该在这部分代码之后:
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
因为实际上,这部分代码永远不会被执行。
我建议接受这个答案(与您的逻辑错误无关)
答案 1 :(得分:1)
你试过调试吗?如果您单步执行代码,您将立即发现问题,即:
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break; // problem here
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
// break should be here
}
在检查逻辑之前,你已经脱离了循环。因此,在检查之后移动break
语句,因为该代码永远不会被执行。