对计算我的刽子手游戏分数背后的逻辑有问题
having trouble with the logic behind calculating a score for my hangman game
im运行一个基本的刽子手游戏,代码有效,但IM目前陷入了一个涉及如何计算分数的逻辑错误,该程序允许用户多次输入相同的单词,并且仍然为获胜提供功劳
例如:如果从我的列表中随机选择的单词是"Apple"并且用户输入"P",如果他们再次输入"P",他们将显示两个单词,他们不会再显示字母,但他们仍然会在获胜时获得两分,如果他们再次输入"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;
}
如何解决这个问题背后的逻辑让我陷入了睡眠不足和长时间的工作,我想但我需要一些帮助来解决这个问题。
只有当display[index]=='_'
和用户正确获取字母时,您才会添加分数。我想你可以从这里拿走它。
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 我在范围内未声明的错误类有问题
- 我的C++语言蛮力算法有问题
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- 我对数组即 0x6dfe78 有问题
- 我对 std::unique(算法)C++有问题
- 使用 fstream 库并在屏幕上打印的文件有问题?
- 这个模板为什么有问题?如何正确编译
- 我对 MyGraph 属性顶点名称和边权重有问题
- delete[]有问题,如何部分删除内存
- 在C++中使用exit()退出程序有问题吗
- 跳转到if(false)块有问题吗
- 查找有问题的宏
- 使用 rand() 和 srand() 有问题
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 我在 C++ "out_of_range at memory location"有问题
- 我在 c++ 中的 AES 代码有问题。明文正在更改,但加密邮件是相同的。我找不到我的错误
- 对计算我的刽子手游戏分数背后的逻辑有问题