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

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

本文关键字:随机 变量 游戏 计算机 何使      更新时间:2023-10-16

所以这是我的代码,我的问题是,无论何时滚动"骰子",它都会持续滚动,直到达到限制,我设置了它成为。

对于人类用户而言,情况并非如此,而是用于计算机使用的情况。我该如何解决此问题,以便计算机每次在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++)'

那条线没有用。这条线没有道理:

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

在此行:

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

您打印了最后的"人类"卷。您的计算机滚动位于" diceroll"中的" ComputerDiceroll"