-1

我目前正在做一个骰子游戏。用户首先掷一对骰子,假设他掷骰子 1 = 2 和骰子 2 = 3。所以现在总数是 5。现在,他需要再次获得 5(总数)才能获胜,如果他在下一步中没有获得 5,那么他会再次滚动并继续比赛。但是,如果在任何时候,他一共掷了两个,他就输了。

所以,请告诉我如何存储第一轮的价值并将其与下一步行动进行比较。我尝试了一些东西,但它似乎不起作用。

#include<iostream>
#include<ctime>      // for the time() function
#include<cstdlib>    // for the srand() and rand() functions
using namespace std;

// Declare variables
//int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int dieTotal = 0;
int Dice ()
{
    // roll the first die
    die1 = (rand() % 6 ) + 1;
    // roll the second die
    die2 = (rand() % 6 ) + 1;


}

// iniating a second two pair dice function.
int compDice()

{
    Dice();
    dieTotal = die1 + die2;
    return (dieTotal);
}



// User Rolling the dice and calucalting the total here

int userGame()
{
    cout << "\nUser turn --- Press 2 to roll" << endl;
    cin >> userInput;

    if ( userInput == 2 )
    {
        Dice ();
        cout << "\nThe user rolled        Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
        cout << "Total = " << die1 + die2 << endl;
    }

    else {
        cout << "Wrong input.";
        //userGame();
    }
    return (die1 + die2 );
}

int checkForWin ()
{
    while (true)
    {

        int result1 = compDice();
        int result = userGame();

        // int finalResult = dieTotal;
        if (result == result1 )
        {
            cout << "\nUser won. Computer looses....m " << endl;
            break;
        }

        else if (result == 2)
        {
            cout << "\nUser looses. Computer won." <<endl;
            break;
        }

        else
        {
        }
    }
}

// Calling for the checkForWin() function in main and the srand.
int main ()
{
    cout << "This is the Dice game. " << endl;

    // set the seed
    srand(time(0));
    checkForWin(); // Initiating the game.
    return 0;
}
4

1 回答 1

0

在我们的评论聊天/误解之后,我冒昧地复制了您的代码并对其进行了修改(尽可能少地保持您的编码风格 - 我不会在任何未来的项目中推荐这种风格)以产生您想要的结果。让我知道它是否有效(简单的测试表明它有效,可能错过了其他一些怪癖)

#include<iostream>
#include<ctime>      // for the time() function
#include<cstdlib>    // for the srand() and rand() functions
using namespace std;

// Declare variables
//int compInput;
int userInput;
int firstRoll = 1;
int die1 = 0;
int die2 = 0;
int dieTotalToMatch = 0;
void Dice ()
{
    // roll the first die
    die1 = (rand() % 6 ) + 1;
    // roll the second die
    die2 = (rand() % 6 ) + 1;

}

// iniating a second two pair dice function.
void compDice()
{
    Dice();
    dieTotalToMatch = die1 + die2;
}



// User Rolling the dice and calucalting the total here

int userGame()
{
    cout << "\nUser turn --- Press 2 to roll" << endl;
    cin >> userInput;

    if ( userInput == 2 )
    {
        Dice ();
        cout << "\nThe user rolled        Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
        cout << "Total = " << die1 + die2 << endl;
    }

    else {
        cout << "Wrong input.";
        //userGame();
    }
    return (die1 + die2 );
}

void checkForWin ()
{

    while (true)
    {

        int result = userGame();
        if (firstRoll)
        {
            dieTotalToMatch = result;
            firstRoll = 0;
            continue;
        }
        // int finalResult = dieTotal;
        if (result == dieTotalToMatch )
        {
            cout << "\nUser won. Computer looses....m " << endl;
            break;
        }

        else if (result == 2)
        {
            cout << "\nUser looses. Computer won." <<endl;
            break;
        }

        else
        {
        }
    }
}

// Calling for the checkForWin() function in main and the srand.
int main ()
{
    cout << "This is the Dice game. " << endl;

    // set the seed
    srand(time(0));
    checkForWin(); // Initiating the game.
    cin.ignore();

    return 0;
}
于 2012-09-17T21:13:00.670 回答