首页 > 解决方案 > Jeopardy Dice 在用户和计算机都离开后显示总回合数的问题

问题描述

所以我的代码有一个问题,我无法让我的任何一个函数(int human_turn 或 int computer_turn)返回其轮数总数的正确值。我想要做的是让两个 int 函数在完成轮到后返回它们各自的轮次总数,然后有一个单独的函数来计算这些值并充当记分牌并显示是否满足获胜条件. 目前在每个回合的while循环结束时,我有 turnTotal_1 = roll_1 + turnTotal_1; 它更新总数,然后循环检查 while 是否仍然为真。然后我把一个 return turnTotal_1; 循环之后的语句,因此它会返回这个值,并且它将始终返回 1。无论回合总数的值是多少,return 语句将始终返回 1。任何帮助或建议将不胜感激。

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
using namespace std; 

/**
 * rollDie 
 * returns a random integer between 1 and 6, works as rolling a dice.
 * return value, int number (1-6)
 */

int rollDie()
{
    return random() % 6 + 1; 
}


int human_turn(int turnTotal_1)
{
    cout << "It is now human's turn" << endl;
    cout << "Do you want to roll a dice(Y/N)?:" << endl;
    char user_choice;
    cin >> user_choice;
    while ((user_choice == 'y') || (user_choice == 'Y'))
    {
        int roll_1 = rollDie();
        if ((roll_1 == 2) || (roll_1 == 5))
        {
            cout << "You rolled a " << roll_1 << endl;
            roll_1 = 0;
        }
        else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
        {
            cout << "You rolled a " << roll_1 << endl;
        }
        else if (roll_1 == 4)
        {
            cout << "You rolled a " << roll_1 << endl;
            roll_1 = 15;
        }
        turnTotal_1 = roll_1 + turnTotal_1;
        cout << "Your turn total is " << turnTotal_1 << endl;
        cout << "Do you want to roll a dice(Y/N)?:" << endl;
        cin >> user_choice;
    }
    if ((user_choice == 'n') || (user_choice == 'N')) 
    {
        return turnTotal_1;
    }
}
int computer_turn()
{
    int turnTotal_2 = 0;
    cout << "It is now computer's turn" << endl;
    int roll_1 = rollDie();
    while (turnTotal_2 <= 10)
    {
        int roll_1 = rollDie();
        if ((roll_1 == 2) || (roll_1 == 5))
        {
            cout << "Computer rolled a " << roll_1 << endl;
            roll_1 = 0;
        }
        else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
        {
            cout << "Computer rolled a " << roll_1 << endl;
        }
        else if (roll_1 == 4)
        {
            cout << "Computer rolled a " << roll_1 << endl;
            roll_1 = 15;
        }
        turnTotal_2 = roll_1 + turnTotal_2;
        cout << "Computer turn total is " << turnTotal_2 << endl;
    }
}
void scoreboard()
{
cout << human_turn << endl;
cout << computer_turn << endl;
}

void game()
{
    cout << "Welcome to Jeopardy Dice!" << endl;
    human_turn(0);
    cout << human_turn << endl;;
    computer_turn();
    scoreboard();
}


int main()
{
    // start the game! 
    game();
    return 0;
}

这是它如何运行的示例。

Welcome to Jeopardy Dice!
It is now human's turn
Do you want to roll a dice(Y/N)?:
n
1
It is now computer's turn
Computer rolled a 5
Computer turn total is 0
Computer rolled a 4
Computer turn total is 15

标签: c++dice

解决方案


推荐阅读