c++ - C++ 中循环的 Hangman 程序

标签 c++ loops for-loop while-loop

首先,我正在编写一个使用用户定义函数和 while 循环的程序。我的用户定义函数 getNextWord() 从文件中生成一个随机单词供我的用户用于 hangman 游戏。我程序的下一步是检查用户的猜测。我正在尝试在 while 循环中使用 while 循环来执行此操作是一个无法正常工作的 for 语句。我假设使用函数 getNextWord() 作为测试表达式是不正确的。我尝试使用 cin >> word 将生成的单词放入变量中,但这也无法正常工作。我如何编写此循环,以便将生成的单词中的字母与用户猜测的字母进行检查?

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <algorithm> 
using namespace std;

#include "randword.h"
#include "myfuncts.h"

const string boardOne = " ------|\n |     |\n       |\n         |\n            |\n    -------\n\n";
const string boardTwo = " ------|\n |     |\n 0     |\n       |\n           |\n    -------\n\n";
const string boardThree = " ------|\n |     |\n 0     |\n |     |\n       |\n    -------\n\n";
const string boardFour = " ------|\n |     |\n 0     |\n-|     |\n       |\n    -------\n\n";
const string boardFive = " ------|\n |     |\n 0     |\n-|-    |\n       |\n    -------\n\n";
const string boardSix = " ------|\n |     |\n 0     |\n-|-    |\n  \\    |\n    -------\n\n";
const string boardSeven = " ------|\n |     |\n 0     |\n-|-    |\n/ \\    |\n    -------\n\n";

int main()
{
    // Declarations
    string fileWord; 
    int wrongGuess = 0;
    char letterGuess = 0;
    string playerYN;
    string word;

getWords("hangman.dat");

cout << "\nDo you want to play hangman? (y or n): ";
cin >> playerYN; 
PromptYN(playerYN);

getNextWord();
cout << "\n\nWord to Guess: " << getNextWord() << endl;

cout << "\n" << boardOne << endl;

while (wrongGuess != 6)
{
    cout << "\nEnter a letter to guess: ";
    cin >> letterGuess;
    letterGuess = toupper(letterGuess);
    cout << "You guessed the letter: " << letterGuess << endl;
    bool found = false;

    for (int i = 0; i < getNextWord().length(); i++)
    {
        if (getNextWord()[i] == letterGuess)
        {
            cout << "\n" << letterGuess << " is in the letter to guess." << endl;
            found = true;
            if (wrongGuess == 0)
                cout <<  "\n" << boardOne << endl;
            if (wrongGuess == 1)
                cout << "\n" << boardTwo << endl;
            if (wrongGuess == 2)
                cout << "\n" << boardThree << endl;
            if (wrongGuess == 3)
                cout << "\n" << boardFour << endl;
            if (wrongGuess == 4)
                cout << "\n" << boardFive << endl;
            if (wrongGuess == 5)
                cout << "\n" << boardSix << endl;
            if (wrongGuess == 6)
                cout << "\n" << boardSeven << endl;
        }

    }
    // if not found - increment wrong guesses
    if (!found)
    {
        wrongGuess++;
        cout << "\n" << letterGuess << " is not in the word to guess." << endl;
        //print the board that corresponds to the wrongGuess

        if (wrongGuess == 0)
            cout << "\n" << boardOne << endl;
        if (wrongGuess == 1)
            cout << "\n" << boardTwo << endl;
        if (wrongGuess == 2)
            cout << "\n" << boardThree << endl;
        if (wrongGuess == 3)
            cout << "\n" << boardFour << endl;
        if (wrongGuess == 4)
            cout << "\n" << boardFive << endl;
        if (wrongGuess == 5)
            cout << "\n" << boardSix << endl;
        if (wrongGuess == 6)
            cout << "\n" << boardSeven << endl;
    }
}


cout << "\n\n";
system("pause");
return 0;
}

这里还有用户定义函数的函数定义

string getNextWord()
{
    int randomNum = 0;
    string word;


if (Used > 0)
{
    randomNum = rand() % Used;

    word = Words[randomNum];

    Words[randomNum] = Words[Used - 1];
    Used--;
    std::transform(word.begin(), word.end(), word.begin(), toupper);
    return(word);
}
else
    return("");
}

最佳答案

注意这里发生了什么:

for (int i = 0; i < getNextWord().length(); i++)
{
    if (getNextWord()[i] == letterGuess)

重复调用getNextWord 会得到新词。这会不断移动玩家的目标,因为每次他们去测试一个字母时,它都会针对不同的词。真是一场令人沮丧的比赛。

OP 想要做的是在每个游戏中调用一次 getNextWord 并存储结果。例如,

string wordToGuess = getNextWord();
cout << "\nEnter a letter to guess: ";
cin >> letterGuess;
letterGuess = toupper(letterGuess);
cout << "You guessed the letter: " << letterGuess << endl;
bool found = false;

for (int i = 0; i < wordToGuess.length(); i++)
{
    if (wordToGuess[i] == letterGuess)
    {

题外话:

这段代码可以变成一个从主例程调用的函数,而不是重复:

if (wrongGuess == 0)
    cout <<  "\n" << boardOne << endl;
if (wrongGuess == 1)
    cout << "\n" << boardTwo << endl;
if (wrongGuess == 2)
    cout << "\n" << boardThree << endl;
...

它也可以重写为使用 switch 语句,或者更好的是,使用数组。该数组在文件顶部定义,代替 boardOneboardSeven

const string boards[] = 
{
    " ------|\n |     |\n       |\n         |\n            |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n       |\n           |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n |     |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|     |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n  \\    |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n/ \\    |\n    -------\n\n"
};

然后使用

cout << boards[wrongGuess] << endl

题外话,续:

定义板的数据定义可以更具可读性。在 C++ 中,我们可以将一个字符串文字拆分为一个或多个相邻的字符串文字,它们不必位于同一行。例如,这两个定义是等价的:

const string name = "John Doe";
const string name = "John "
                    "Doe";

考虑到这一点,我们可以写

const string boardOne = " ------|\n"
                        " |     |\n"
                        "       |\n"
                        "         |\n"
                        "            |\n"
                        "    -------\n\n";

这样我们就可以在文本编辑器中看到程序输出字符串时将在控制台上布局的图片。如果某事是 错误,它在视觉上很明显并且易于修复。

一旦您开始运行游戏,您可能想要调整棋盘的外观,这可能会浪费时间并且对当前布局感到沮丧。

关于c++ - C++ 中循环的 Hangman 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34078112/

相关文章:

c++ - EXE 也导出静态库导出

javascript - 如何在node js中同步执行循环

c++ - boolean vector 的基于范围的 for 循环

javascript - 通过 JavaScript 映射 Json 数据

windows - 如何在 CMD/Batch 中将数字拆分为单个数字

python - 在for循环中获取下一个变量

java - Eclipse 问题 View 双击

c++ - 链接 C++ 和 C++/CLI 项目 (.NET Core) 时导入错误 (E0337)

c++ - 允许成员函数使用左值和右值引用限定符的目的是什么?

java - 一个java可执行文件可以同时运行多个循环吗