【问题标题】:How to make functions for reverse number guessing game?如何制作倒数猜谜游戏的功能?
【发布时间】:2020-06-29 04:44:45
【问题描述】:

对于我的作业,我需要编写一个程序,在 5 次尝试内猜出用户的号码,包括 1 到 19 之间。对于每次尝试,用户输入的数字是:a) 正确、b) 太高或 c) 太低

我们应该定义两个函数:

    1234563输入正确、高或低。
  • 第二个函数应该在被告知是过高还是过低后计算下一个猜测。

我能够使用嵌套的 switch 语句来完成此操作,但我在尝试提出第二个函数时遇到了麻烦。

感谢任何帮助。我将尝试用switch 语句附加我的第一个程序。我假设我需要生成一个带有最小值和最大值的随机数,但我不知道该怎么做。

#include<iostream>
using namespace std;

int main()
{
    int guess = 10;
    int input = 0;
    cout<<"Is this your number: "<< guess<<endl;
    cout<< "Correct? (1), High?(2), Low(3)"<<endl;
    cin>> input;
    switch(input) {
        case(1):
            cout<< "Thanks for playing";
            break;
        case(2):
            guess = 5;
            cout<<"Is this your number: "<< guess<<endl;
            cout<< "Correct? (1), High?(2), Low(3)"<<endl;
            cin>> input;
                switch(input){
                    case(1):
                        cout<< "Thanks for playing";
                        break;
                    case(2):
                        guess = 3;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case(1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 2;
                                cout<<"Is this your number: "<< guess<<endl;
                                                       cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                                       cin>> input;
                                switch(input){
                                    case(1):
                                        cout<< "Thanks for playing";
                                        break;
                                    case (2):
                                        guess = 1;
                                        cout<< "Your guess was: "<<guess<<endl;
                                        break;
                                    case (3):
                                        cout<< "Cheater..."<<endl;
                                        break;
                                }
                                break;
                            case(3):
                                guess = 4;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;

                              }
                        break;
                    case(3):
                        guess = 7;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case (1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 6;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                            case(3):
                                guess = 8;
                                cout<<"Is this your number: "<< guess<<endl;
                                cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                cin>> input;
                                switch (input){
                                        case (1):
                                        cout<< "Thanks for playing";
                                        break;
                                        case(2):
                                        cout<<"Cheater..."<<endl;
                                        case(3):
                                        guess = 9;
                                        cout<< "Your guess was: "<< guess<<endl;
                                        break;
                                }
                        }

                        break;
                }
            break;
        case(3):
            guess = 15;
            cout<<"Is this your number: "<< guess<<endl;
            cout<< "Correct? (1), High?(2), Low(3)"<<endl;
            cin>> input;
                switch(input){
                    case(1):
                        cout<< "Thanks for playing";
                        break;
                    case(2):
                        guess = 13;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case(1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 12;
                                cout<<"Is this your number: "<< guess<<endl;
                                                       cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                                       cin>> input;
                                switch(input){
                                    case(1):
                                        cout<< "Thanks for playing";
                                        break;
                                    case (2):
                                        guess = 11;
                                        cout<< "Your guess was: "<<guess<<endl;
                                        break;
                                    case (3):
                                        cout<< "Cheater..."<<endl;
                                        break;
                                }
                                break;
                            case(3):
                                guess = 14;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                              }
                        break;
                    case(3):
                        guess = 17;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case (1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 16;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                            case(3):
                                guess = 18;
                                cout<<"Is this your number: "<< guess<<endl;
                                cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                cin>> input;
                                switch (input){
                                        case (1):
                                        cout<< "Thanks for playing";
                                        break;
                                        case(2):
                                        cout<<"Cheater..."<<endl;
                                        case(3):
                                        guess = 19;
                                        cout<< "Your guess was: "<< guess<<endl;
                                        break;
                                }
                        }

                        break;
                }
            break;
    }

    return 0;
}

【问题讨论】:

  • 这种嵌套输入和 switch 语句的模式是糟糕的设计,应该从头开始。首先专注于编写函数,但只让您的程序调用每个函数一次(这是一次猜测,以及来自用户的一次输入)。完成该工作后,将其封装在一个循环中。
  • 我们来玩个游戏吧。您必须猜测 1 到 19 之间的数字。我会告诉你它是否正确,太高或太低。你先猜哪个数字,为​​什么?
  • 为什么你认为猜测是随机的?如果你自己玩,你会这样做吗?
  • 正如约翰所指出的 - 设计不是最优的。它没有规模。假设明天的作业是 0 到 1000 之间的数字 - 你会怎么做?有图案吗?如何避免一遍又一遍地编写模式?
  • 遵循作业的要求。编写两个函数,它们完全按照作业的要求执行,不多也不少。然后考虑如何将它们结合起来创建最终的算法。您正在尝试通过使用一堆 switch 语句对每个可能的特定情况进行硬编码来解决问题,而不是退后一步并思考如何编写适用于所有情况的通用算法。

标签: c++ function random


【解决方案1】:

试试这个:

#include <iostream>
using namespace std;
#define elif else if

void guessf(int guess) {
    cout << "Is this your number: " << guess << endl << "Correct? (1), High?(2), Low(3)" << endl;
}

int main(){
    unsigned int range = 10, guess = 10, input = 0, i = 0;
    while (i < 5 && input != 1) {
        guessf(guess);
        cin >> input;
        if (range > 1)
            range /= 2;
        else
            range = 1;
        if (input == 1)
            cout << "Thanks for playing";
        elif (input == 2)
            guess -= range;
        else
            guess += range;
    }
}

这是做什么的guessf() 非常简单,所以我们继续讨论main()range 变量是它将从guess 中添加或减去的变量然后,每次尝试,直到您的程序尝试 5 次或 guess 正确,询问用户是否正确,高或低,如果正确 cout &lt;&lt; "Thanks for playing";,如果 guess 高减去 range如果guess 较低,则将range 添加到guess。同样 bcs 我们处理intrange 的最小值必须为1,如果 bcs 较低,则不会从guess 中添加或减去任何内容。这种技术至少类似于 "Binary Search"

如果要删除elif,请删除#define elif else if并将elif替换为else ifelseif在同一行)

还有 2 个函数,你的意思是 2 个包括主函数还是没有函数?这是一个重要的事情要知道

【讨论】:

    【解决方案2】:

    以下是一些观察:

    • 问题集由 1 到 19 的数字组成,即范围 [1, 19]。
    • 需要在此范围内搜索/猜测给定的数字。
    • 范围是固定的,即始终为 [1, 19]。这意味着它是一个排序范围。
    • 猜数字的次数为5次。

    因此,鉴于上述特征,Binary Search algorithm 将提供最佳解决方案,即:

    • 范围 = [1, 19]
    • 没有。尝试次数 = 5
    • 二分搜索算法的最坏情况性能 = O(log n)
    • 即范围 = 19,O(log n) = O(log 19) = 4.25 = ~5 次尝试

    您可以对二分搜索算法进行一些研究以熟悉它。剩下的就是你维护highlowmid 点的逻辑。而且,您不需要随机数!

    您将使用自己的二进制搜索算法变体,通过调整问题集(即数字可能存在的范围)来猜测数字。


    就你的两个功能而言:

    1. 第一个函数将显示菜单并输入要猜测的数字。
    2. 第二个函数将执行猜测。

    对于其余的样板逻辑,您可以在 main() 函数中实现它。

    以下是您的代码的一般分类(只是概要,假设您使用的是 C++98 或 C++03):

    int showMenuAndInputNumber() { /* ... */ }
    int guessNumber(const int n) { /* ... */ }
    
    int main()
    {
        const int n = showMenuAndInput();
    
        // validate if n is in the range [1, 19]
    
        // given the search sorted range and the valid number,
        // then using Binary search you'll need max 5 tries
        // so, you don't need to keep track of the tries
        // but, for simplicity, you can use tries
    
        const int tries = 5;
        for ( int t = 0; t < tries; ++t )
        {
            // guess the number here
            // adjust the range either right or left
            // according to your own Binary search algorithm's variation
        }
    
        return 0;
    }
    
    

    这并不是针对您的问题的现成解决方案。这只是为了提供一些指导。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多