例如:如果从我的列表中随机挑选的单词是" apple"并且用户输入" p"如果他们输入" p"他们再也不会透露任何更多的信件,但他们仍然会在胜利中获得两分,如果他们输入了#34; p"再一次,用户将赢得比赛
这是代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
const int maxWrongGuesses = 10;
int letter;
char guess;
string display;
bool won = false;
bool guessing = true;
int correctGuesses = 0;
int wrongGuesses = 0;
cout << "Lets Play a Game of Hangman!!! \n";
vector<string> word;
string line;
ifstream hangmanfile("hangman.txt");
if (hangmanfile.is_open())
{
while (getline(hangmanfile, line))
{
word.push_back(line);
}
hangmanfile.close();
}
else cout << "Unable to open file";
unsigned seed = time(0);
srand(seed);
int answer = rand() % word.size();
string currentWord = word.at(answer);
cout << currentWord << "\n";
for (int i = 0; i < currentWord.length(); i++)
{
display = display + "_ ";
}
while (guessing)
{
bool correctGuess = false;
cout << display << "\n";
cout << "Please Enter Your Guess \n";
cin >> guess;
for (int i = 0; i < currentWord.length(); i++)
{
if (guess == currentWord[i])
{
display[i * 2] = guess;
correctGuesses++;
correctGuess = true;
}
if (correctGuesses == currentWord.length())
{
guessing = false;
won = true;
}
}
if (!correctGuess)
{
cout << "Wrong Try Again \n";
wrongGuesses++;
cout << wrongGuesses << "\n";
}
if (maxWrongGuesses == wrongGuesses)
{
guessing = false;
}
}
if (won)
{
cout << display << "\n";
cout << "Congratz You Won!! \n";
}
else
{
cout << "The Answer Was " << currentWord << "\n";
cout << "Sorry You Lose \n";
}
return 0;
}
如何解决这个问题背后的逻辑让我难以忍受睡眠和长时间的工作,我想但我需要一些帮助来解决这个问题。
答案 0 :(得分:0)
只有在display[index]=='_'
且用户正确填写了字母时才会添加积分。我想你可以从这里拿走它。