基于功能的猜字游戏

时间:2017-03-17 01:30:41

标签: c++ function

我正在尝试为类项目做一个基于函数的猜字游戏。我之前完成了一个可以产生随机字母的程序,现在我需要使它基于函数,但它不起作用。任何帮助,将不胜感激。


let EventEmitter = require('events').EventEmitter;
class Person extends EventEmitter {
  constructor(name) {
    super();
    this.name = name;
  }
}
let mary = new Person('mary');
mary.on('speak', function(s) {
  console.log(this);
});
mary.emit('speak', 'you may delay, but time will not');

2 个答案:

答案 0 :(得分:0)

您的代码中存在大量语法错误:

首先,您的srand(time(NULL))功能应该在您生成随机数之前,而不是之后。因此,您应该移动它以使代码看起来像:

srand(time(NULL));
// Add the rest of your code here
char n = (char)(rand() % 26 + 1);

其次,函数playOneGame()中的while循环:

while (games <= number_of_games)

没有打开或关闭大括号来包含所有代码;因此,在while循环条件下的行被认为是while循环的一部分。因此,您应该将大括号添加到要包含在while循环中的代码部分,例如:

while (games <= number_of_games)
{
    // Add necessary code
}

您的代码中还有其他逻辑和语法错误,但根据您的程序,您可能需要相应地修复它们,因为没有固定的答案。祝你好运!

答案 1 :(得分:0)

正如很多人已经说过的,代码中存在很多语法错误。

首先,您应该在使用rand()函数之前为随机值生成器(使用srand(time(0)))播种。因此,char n = (char)(rand() % 26 + 1);应该在main()之前,而不是之后。

第二次,当您调用函数时,不需要添加返回类型。例如,在void introduction();函数中,函数introduction();应更改为compareTwoCharacters(char,char);,与接下来的3个函数调用相同(请参阅:https://www.tutorialspoint.com/cplusplus/cpp_functions.htm)。

第三次,调用函数时,需要一个参数,而不是一个类型。例如,您的函数调用char不应将compareTwoCharacters(n,'A');作为其参数,但应具有变量或文字,如下所示:char compareTwoCharacters(char answer, char guess)。在您编写的3个函数调用中进行这些更改。

Forth ,在您的char(第57行)函数中,您应该返回return 0;,但在您的代码中,您返回了一个整数:{{ 1}}和return 2;以及return -2;。确保您的return语句和返回类型匹配。这是char playOneGame(char answer)函数中的相同问题;您将返回true,但返回类型为char

第五,在char playOneGame(char answer)函数中,您缺少while循环的括号。如果没有括号,它将只循环下一个命令,即char answer = 97 + rand() % 27;并且永远不会结束(无限循环),因为永远不会达到退出条件。

第六次,在同一个函数中,永远不会声明变量game。它是在一个不同的函数中声明的(第26行int games = 1;),但是一旦你退出该函数,game就会停止退出(更多信息:https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm)。

我修复了一些错误,尝试保留代码的相同通用结构,并使用// CHANGED标记更改。但是,它可能不是您打算运行程序的方式:

#include<iostream> 
#include<cstdlib>
#include<ctime> 
using namespace std;

void introduction();
//Display the introduction

int getNumberOfGames(int& number_of_games);
//Ask individual how many games they want to play
//

int compareTwoCharacters(char answer, char guess); // CHANGED
//

int playOneGame(int games, int number_of_games, char guess); // CHANGED
//function returns true if user won;otherwise will return false. going to call the 3rd function to decide if correct or not



int main()
{
    char alphabet [27];
    int number_of_games;
    char guess;
    int games = 1;
    srand(time(0)); // CHANGED
    char n = (char)(rand() % 26 + 1); // CHANGED

    introduction(); // CHANGED
    getNumberOfGames(number_of_games); // CHANGED
    compareTwoCharacters(n,'A'); // CHANGED
    playOneGame(games, number_of_games, guess); // CHANGED

    system("pause");

}

void introduction()
{
    cout<<"Weclome to the Letter Guessing game!\n";
    cout<<"You have 5 chances to guess each letter.\n \n";
    cout<<"Let's begin!\n";
    cout<<"**************************************************\n\n";

}

int getNumberOfGames(int& number_of_games)
{
    cout<<"How many games do you want to play? \n";
    cin >> number_of_games;
    return 0;

}

int compareTwoCharacters(char answer, char guess) // CHANGED
{
    cout << "Enter your guess: ";
    cin >> guess;
    if (guess == answer)
        return 0;
    else if (guess < answer)
        return 2;
    else
        return -2;
}

int playOneGame(int games, int number_of_games, char guess) // CHANGED
{
    //return true is guess = answer otherwise return false
    // return true; // CHANGED
    char answer = 'a';  // CHANGED
    while (games <= number_of_games)
    { // CHANGED
        answer = 97 + rand() % 27;  // CHANGED
        cout << "Lets play game " << games++ << '\n';
        for (int number_of_guesses = 0; number_of_guesses < 5; number_of_guesses++)
        {

            cout << "Enter your guess: ";
            cin >> guess;
            if (guess > answer)
            {
                cout << "The letter you are trying to guess is before " << guess << "\n";

            }
            else if (guess < answer)
            {
                cout << "The letter you are trying to guess is after " << guess << "\n";

            }
            else
            {
                cout << "Your guess is correct! \n";

                break;
            }

        }
    } // CHANGED
    cout << "The answer you were looking for was " << answer << '\n';
    return 0;
}