如何将第一个值存储在c ++骰子游戏中并在下一步中进行比较?

时间:2012-09-17 20:00:26

标签: c++ dice

我目前正在制作骰子游戏。用户首先滚动一对骰子,所以让我说他掷骰子,骰子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;
}

1 个答案:

答案 0 :(得分: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;
}