对计算我的刽子手游戏分数背后的逻辑有问题

having trouble with the logic behind calculating a score for my hangman game

本文关键字:背后 有问题 计算 我的 刽子手 游戏      更新时间:2023-10-16

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]=='_'和用户正确获取字母时,您才会添加分数。我想你可以从这里拿走它。