Hangman程序for循环用c ++编写

时间:2015-12-03 23:17:25

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

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

#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("");
}

1 个答案:

答案 0 :(得分:2)

请注意这里发生的事情:

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";

这样我们在文本编辑器中可以看到图片,因为当程序输出字符串时它将被放置在控制台上。如果是的话 错了,它在视觉上很明显,很容易修复。

一旦你开始运作游戏,你可能想要调整电路板的外观,这可能会浪费时间并且对当前的布局感到沮丧。