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

时间:2014-03-07 21:10:14

标签: c++

所以这是我的代码,我遇到的问题是无论出于何种原因,无论什么时候滚动“骰子”,它都会连续滚动任何数字,直到达到我设定的极限为止。

对于Human用户来说情况并非如此,但它是供计算机使用的。我该如何解决这个问题,以便计算机每次在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;
}

1 个答案:

答案 0 :(得分:2)

'for (int i=0; i<1; i++)'

那条线没用。 这条线毫无意义:

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

并在此行:

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

你打印最后一个“人”卷。 您的计算机卷位于“computerDiceRoll”而不是“diceRoll”