简体   繁体   English

如何在猪游戏中每次使计算机随机变量

[英]How to make computer take random variable each time in pig game

So here is my code, the problem I am having is that for whatever reason when ever the "dice" is rolled it rolls whatever that number is continously until it reaches the limit I set it up to be. 所以这是我的代码,我遇到的问题是,无论出于何种原因,什么时候滚动“骰子”它会连续滚动任何数字,直到它达到我设置的极限。

This is isn't the case for the Human user but it is for computer use. 对于Human用户来说情况并非如此,但它是供计算机使用的。 How can i fix this so that the computer will take a random variable between 1-6 each time. 我该如何解决这个问题,以便计算机每次使用1-6之间的随机变量。

int main () 
{
    int humanTurn=0;
    int humanTotalScore=0;
    int computerTotalScore=0;
    int computerTurn=3;
    float diceRoll = 0;
    int score = 0;
    int computerScore = 0;
    int answer = 0;
    int humanScore = 0;
    int pause = 0;
    float computerDiceRoll = 0;

    srand(time(0));

    cout << "Welcome to the game of pig, the first player to reach to 100 wins."<< endl;
    cout << "if you roll a one no score will be recorded and your turn will go the computer"<< endl;
    cout << " If you choose to hold, the total score accumilated will be recorded" << endl;

    while ((humanTotalScore < 100) && (computerTotalScore < 100)) 
    {
        cout << endl;
        cout << endl;
        cout << "press 1 to  roll or 2 to save your score: ";
        cin >> answer;

        for (int i=0; i<1; i++) 
        {
            diceRoll = rand() % 6 + 1;
        }

        if ((diceRoll > 1) && ( answer == 1))
        {
            cout << endl;
            cout << endl;
            cout << "your  current roll is: " << diceRoll << endl; 
            cout << endl;
            cout << endl;

            score += diceRoll;
            cout << "Score for this turn is: " << score << endl;
            cout << "your total score is: " << humanTotalScore << endl;
            cout << endl;
            cout << endl;
        }
        else if (answer == 2)
        {
            cout << "your score of: " << score << " will be saved" << endl;
            humanTotalScore += score;
            score = 0;
        } 
        else 
        {
            cout << endl;
            cout << endl;
            cout << "you  rolled a ONE. You lose your turn and any points that were with it"   <<endl;
            cout << endl;
            cout << endl;
            cout << "enter any key to continue." << endl; // necessary to let user know that there     point were discontinued
            cin >> pause;
            score = 0;
            computerTurn = score;
        }
        if ((computerTurn == 0) || (answer ==2))
        {
            cout << "it is now the Computers turn" << endl;

            for (int i=0; i<1; i++) 
            {
                srand(time(0));
                computerDiceRoll = rand() % 6 + 1;
            }
            while ((computerDiceRoll > 1)&& (computerScore <= 20))
            {
                cout << endl;
                cout << endl;
                cout << "computers roll is : " << diceRoll << endl; 
                cout << endl;
                cout << endl;

                computerScore += diceRoll;
                computerTotalScore += computerScore;

                cout << "computer current score is : " << computerScore << endl;
                cout << "computer total score is: " << computerTotalScore << endl;
                cout << endl;
                cout << endl;
                if (diceRoll == 1)
                {
                    computerScore = 0;
                }
            }
        }
    }
    return 0;
}
'for (int i=0; i<1; i++)'

that line is useless. 那条线没用。 and this line makes no sense: 这条线毫无意义:

'while ((computerDiceRoll > 1)&& (computerScore <= 20)){'

and at this line: 在这一行:

cout << "computers roll is : " << diceRoll << endl; 

you print the last "human" roll. 你打印最后一个“人”卷。 your computer roll is at "computerDiceRoll" not in "diceRoll" 你的电脑卷是“computerDiceRoll”而不是“diceRoll”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM