【问题标题】:Beginner needing help using functions in a game of rock, paper, scissors初学者在玩石头、剪纸、玩游戏时需要帮助
【发布时间】:2021-11-19 14:59:56
【问题描述】:

我们应该使用函数编写一个石头、剪子布的游戏。除了确定获胜者并展示这一点之外,我已经完成了所有工作。 “无效结果”是我遇到的问题。

目前,我可以选择石头剪刀布,让电脑显示用户的选择和电脑的选择。这是我当前的代码:

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

int user(int);
int pc(int);
int result(int);
string userChoice, pcChoice;
int userNum, pcNum;
void result(int, int);

int main()
{
    user(userNum);
    pc(pcNum);
    
    cout << "You chose " << userChoice << endl << "They chose " << pcChoice;
    return 0;
    
    void result();
}

int user(int userNum)
{
    cout << "Choose rock, paper, or scissors: ";
    cin >> userChoice;
    
    if(userChoice == "rock")
    {
        userNum = 1;
    }
    else if(userChoice == "paper")
    {
        userNum = 2;
    }
    else if(userChoice == "scissors")
    {
        userNum = 3;
    }
    else
    {
        cout << "Input invalid. Run again and enter rock, paper, or scissors." << endl;
        
        exit(0);
    }
    
    return userNum;
}

int pc(int pcNum)
{
    srand(time(0));
    pcNum = (rand() % 3 + 1);
    
    if (pcNum == 1)
    {
        pcChoice = "rock";
    }
    else if (pcNum == 2)
    {
        pcChoice = "paper";
    }
    else if (pcNum == 3)
    {
        pcChoice = "scissors";
    }
    
    return pcNum;
}

void result(int pcNum, int userNum)
{
    if (pcNum == 1)
    {
        if (userNum == 1)
        {
            cout << "Tie. Play again.";
        }
        else if (userNum == 2)
        {
            cout << "You win. Paper covers rock.";
        }
        else if (userNum == 3)
        {
            cout << "You lose. Rock breaks scissors.";
        }
    }
    else if (pcNum == 2)
    {
        if (userNum == 1)
        {
            cout << "You lose. Paper covers rock.";
        }
        else if (userNum == 2)
        {
            cout << "Tie. Play again.";
        }
        else if (userNum == 3)
        {
            cout << "You win. Scissors cut paper.";
        }
    }
    else if (pcNum == 3)
    {
        if (userNum == 1)
        {
            cout << "You win. Rock breaks scissors.";
        }
        else if (userNum == 2)
        {
            cout << "You lose. Scissors cut paper";
        }
        else if (userNum == 3)
        {
            cout << "Tie. Play again.";
        }
    }
}

【问题讨论】:

  • 旁注:pcNum in pc 隐藏了全局 pcNum 并且您不使用返回值。 user 中的 userNum 相同。最好避免使用全局变量。

标签: c++


【解决方案1】:
void result();

这是一个函数声明,而不是函数调用。

您不能在函数中声明任何函数。此外,您已经声明了该功能。所以,我认为你想要的是:

result(userNum, pcNum);

【讨论】:

    【解决方案2】:

    请阅读标记为// CHANGE HERE的cmets

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    // CHANGE HERE:
    // 1. Changed function signatures
    // 2. Removed unnecessary global variables
    // 3. Removed unnecessary functions
    
    int user();
    int pc();
    void result(int, int);
    
    string userChoice, pcChoice;
    
    int main()
    {
        // CHANGE HERE: added local variables to store return values
        int userNum = user();
        int pcNum = pc();
        
        // CHANGE HERE: added endl
        cout << "You chose " << userChoice << endl << "They chose " << pcChoice << endl;
        
        // CHANGE HERE: call result function here
        result(pcNum, userNum);
        return 0;
    }
    
    int user()
    {
        cout << "Choose rock, paper, or scissors: ";
        cin >> userChoice;
        
        if(userChoice == "rock")
        {
            return 1;
        }
        else if(userChoice == "paper")
        {
            return 2;
        }
        else if(userChoice == "scissors")
        {
            return 3;
        }
        
        cout << "Input invalid. Run again and enter rock, paper, or scissors." << endl;
        exit(0);
    }
    
    int pc()
    {
        srand(time(0));
        // CHANGE HERE: make pcNum local variable
        int pcNum = (rand() % 3 + 1);
        
        if (pcNum == 1)
        {
            pcChoice = "rock";
        }
        else if (pcNum == 2)
        {
            pcChoice = "paper";
        }
        else if (pcNum == 3)
        {
            pcChoice = "scissors";
        }
        
        return pcNum;
    }
    
    void result(int pcNum, int userNum)
    {
        if (pcNum == 1)
        {
            if (userNum == 1)
            {
                cout << "Tie. Play again.";
            }
            else if (userNum == 2)
            {
                cout << "You win. Paper covers rock.";
            }
            else if (userNum == 3)
            {
                cout << "You lose. Rock breaks scissors.";
            }
        }
        else if (pcNum == 2)
        {
            if (userNum == 1)
            {
                cout << "You lose. Paper covers rock.";
            }
            else if (userNum == 2)
            {
                cout << "Tie. Play again.";
            }
            else if (userNum == 3)
            {
                cout << "You win. Scissors cut paper.";
            }
        }
        else if (pcNum == 3)
        {
            if (userNum == 1)
            {
                cout << "You win. Rock breaks scissors.";
            }
            else if (userNum == 2)
            {
                cout << "You lose. Scissors cut paper";
            }
            else if (userNum == 3)
            {
                cout << "Tie. Play again.";
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 2015-06-17
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多