模拟掷骰子java游戏

时间:2016-11-12 22:31:37

标签: java

这是我的代码我应该模拟掷骰子的游戏:我得到了正确的胜利和失败,但我无法得到概率正确。有什么建议吗?

请帮助说明:在掷骰子游戏中,传球线下注如下进行。两个六面骰子滚动;掷骰子掷骰子的第一卷被称为“滚出来。”7或11的自动获胜卷自动获胜,2或3或12卷出来自动失败。如果在出来的卷筒上滚动4,5,6,8,9或10,则该数字成为该点。玩家继续滚动骰子直到7或点滚动。如果该点首先滚动,则玩家赢得下注。如果首先掷出7,则玩家输掉。使用这些规则编写一个模拟掷骰子游戏的程序,无需人工输入。该计划不应要求下注,而应计算玩家是赢还是输。

程序应模拟滚动两个骰子并计算总和。添加一个循环,以便程序播放10,000个游戏。添加计数器,计算玩家获胜的次数,以及玩家输了多少次。在10,000场比赛结束时,计算获胜的概率[即Wins /(Wins + Loss)]并输出该值。从长远来看,谁将赢得大多数比赛,你还是房子?注意:要生成随机数x,其中0x≤< 1,使用x = Math.random(); 。例如,乘以6并转换为整数会得到一个介于0和5之间的整数。

public class Dice 
{
    public static void main(String[]args)
    {
        //declaring variables
        int comeOutRoll1, comeOutRoll2;
        int roll1, roll2;
        int numW, numL;
        int sum, sum2 = 0;
        int thePoint = 0;
        double probability; 


        //initializing variables
        comeOutRoll1 = (int)(Math.random()*5);
        comeOutRoll2 = (int)(Math.random()*5);
        sum = comeOutRoll1 + comeOutRoll2;
        numW = 0;
        numL = 0;


        for(int timesPlayed = 0; timesPlayed <= 10000; timesPlayed++)
        {

            switch(sum)
            {
            //adds how many wins and losses
            case 2:
                numL = numL +1;
                break;
            case 3:
                numL = numL + 1;
                break;
            case 12:
                numL = numL + 1;
            break;
            case 7:
                numW = numW +1;
                break;
            case 11:
                numW = numW +1;
                break;
            case 4:
                thePoint = sum;
                break;
            case 5:
                thePoint = sum;
                break;
            case 6:
                thePoint = sum;
                break;
            case 8:
                thePoint = sum;
                break;
            case 9:
                thePoint = sum;
                break;
            case 10:
                thePoint = sum;
            break;
            //if not any of these cases roll again
            default:

                roll1 = (int)(Math.random()*5);
                roll2 = (int)(Math.random()*5);
                sum2 = roll1 + roll2;
                break;
            }

            if(sum2 == thePoint)
            {
                numW = numW +1;
            }
            else if(sum2 == 7)
            {
                numL = numL +1;
            }
        }


        probability = (numW/(numW+numL));

        System.out.println("Number of Wins: " + numW);
        System.out.println("Number of Losses: " + numL);
        System.out.println("The probability of winning is: " + probability + " percent");   


    }

}

2 个答案:

答案 0 :(得分:1)

2个问题......

  1. 你只分析1(随机)出来滚动。你需要在整个游戏中循环10000次,而不仅仅是二次滚动,这只应该循环才能得到结果。
  2. 你正在进行整数除法(截断以留下整数)。使用浮点数学代替将其中一个数字转换为floatdouble。 IE probability = (float)numW/(numW+numL);
  3. 在伪代码中,使用辅助方法:

    // returns the random integer between 1 and 6 inclusive
    method rollDie()
    
    // returns the sum of a random roll of 2 dice
    method rollDice()
        return rollDie() + rollDie() 
    
    // return true if the player won given the point
    method won(point)
        roll = rollDice()
        if roll == 7 return false
        if roll == point return true
        return won(point)
    
    // main
    define wins variable (you don't need a losses variable. losses = 10000 - wins 
    loop 10000 times {
        comeOut = rollDice()
        if comeOut in (7, 11) or (comeOut not in (2, 3 or 12) and won(comeOut))
            wins++
    }
    probability = (float)wins/10000
    

    将上面的内容转换为java,你应该好好去(你希望学到一些东西 - 见DRY)。

答案 1 :(得分:-1)

已编辑的工作解决方案

public static void main(String[]args)
{
    //declaring variables
    int roll1, roll2;
    int numW = 0;
    int numL = 0;
    int sum = 0;
    int thePoint = 0;
    double probability; 


    // Loop will run 1001 time due to <=
    for(int timesPlayed = 0; timesPlayed <= 1000; timesPlayed++)
    {
        roll1 = (int)(Math.random()*5)+1;
        roll2 = (int)(Math.random()*5)+1;
        sum = roll1 + roll2;

        switch(sum)
        {
            //adds how many wins and losses
            case 2:
            case 3:
                numL = numL + 1;
                break;
            case 12:
                numL = numL + 1;
            break;
            case 7:
            case 11:
                numW = numW +1;
                break;
            case 4:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
                thePoint = sum;
                break;
            default:
                // You should never logically reach here, so could remove.
        }

        if(thePoint!=0){
            do{
                roll1 = (int)(Math.random()*5)+1;
                roll2 = (int)(Math.random()*5)+1;
                sum = roll1 + roll2;
            }while(sum!=thePoint & sum!=7);

            if(sum == thePoint)
            {
                numW = numW +1;
            }else{
                numL = numL +1;
            }
        }
        thePoint = 0;
    }

    probability = (double)numW/(numW+numL); // (numW + numL) could just be total number of games if made into a variable and used as for loop condition aswell.

    System.out.println("Number of Wins: " + numW);
    System.out.println("Number of Losses: " + numL);
    System.out.println("The probability of winning is: " + probability + " percent");   

    }

你必须:

  • 在循环时包括重新滚动,否则它将使用相同的重复值。
  • 如果是4,5,6,8,9或10的情况,则必须继续滚动,直到新滚动等于7或thePoint
    • 在计算概率时,你正在进行整数除法,这可能导致舍入为0。

按照规范工作。一些整理变量。