如何显示两个骰子

时间:2019-02-03 07:27:19

标签: java objectinstantiation

我正在编写一个掷骰子程序。在上课时,我还是Java的新手。我在该程序的不同程序包中使用多个类,我试图找出的是,在一个类中,对于我的程序包pairOfDice,我在pairOfDice,die1和die2类中创建了对象。现在,我还有另一个包rollDice,我的目标是使用pairOfDice类滚动两个骰子并显示滚动。我正在努力的是如何做到这一点。当我滚动骰子时,结果显示就好像我只是滚动一个骰子一样。我已经进行过调整,每卷显示两个骰子,尽管感觉好像我没有以一种更熟练的方式来做。

package die;

import java.util.Scanner;

/**
 *
 * @author <a href= "mailto:adavi125@my.chemeketa.edu" >Aaron Davis</a>
 */
public class RollDice
{
    public static void main(String[] args)
    {


        Scanner scan = new Scanner(System.in);

        PairOfDice dice = new PairOfDice();

        // get amount of rolls from user
        System.out.print("How many rolls do you want? ");

        int numRolls = scan.nextInt();



        int diceOne, diceTwo;
        int boxCar, snakeEyes;
        int j = 0, k = 0;

        // rolls the dice the requested amount of times
        for (int i = 0; i < numRolls; i++)
        {
            // first die to roll
            diceOne = dice.roll();

            // second die to roll
            diceTwo = dice.roll();

            // display rolled dice
            System.out.println(diceOne + " " + diceTwo + "\n");

            // store and display pairs of 1 rolls
            if (diceOne == 1 && diceTwo == 1)
            {
                snakeEyes = ++j;

                System.out.println("\nThe number of snake eyes you have is: " 
                    + snakeEyes + "\n");
            }


            // store and display pairs of 6 rolls
            if (diceOne == 6 && diceTwo == 6)
            {
                boxCar = ++k;

                System.out.println("\nThe number of box cars you have is: " 
                    + boxCar + "\n");
            }



        }




    }    
}


******************************************************************************
/*
 the integers diceOne and diceTwo are my workarounds, my other package contains

public class PairOfDice extends Die
{
    Die die1, die2;

    public PairOfDice()
    {
        die1 = new Die();
        die2 = new Die();     
    }

    public PairOfDice(int face)
    {
        die1 = new Die(face);
        die2 = new Die(face);
    }
}

*/
******************************************************************************

// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die

1 个答案:

答案 0 :(得分:0)

PairOfDice类不代表您的模型,即“一对骰子”。 如果您有一对骰子,则在滚动它们时会得到两个不同的数字,即:

  1. 您分别对两个值都感兴趣,因此roll方法必须返回两个值。您可以使用包含两个值的RollResult bean
  2. 您对两个值都不感兴趣,而对总和不感兴趣。这样roll方法只能返回2到12之间的整数,并且可以基于它们的总和推测骰子滚动:在您的情况下,总是有可能的,因为当且仅当得到2的总和如果您的骰子是1,1;同样,如果且仅当您的骰子为6、6时,您的总和为12。 例如,如果您要针对条件“骰子1 = 3,骰子2 = 4”进行测试,则将不起作用,因为滚动的许多组合返回3 + 4 = 7

希望这会有所帮助。

根据评论,我们必须继续第一个解决方案。 这里是一个示例,该示例实现了域不变对象和roll域函数,它们针对骰子返回roll操作的结果。 在此示例中,我展示了具有多种类型的骰子的可能性。

import java.util.*;
import java.util.stream.Collectors;

public class RollingDices {
    private static final Random RND = new Random();
    private static interface Dice {
        public int roll();
    }
    private static class UniformDice implements Dice {
        public int roll() {
            return RND.nextInt(6) + 1;
        }
    }
    private static class TrickyDice implements Dice {
        private final int value;
        public TrickyDice(int value) {
            this.value = value;
        }
        public int roll() {
            return value;
        }
    }
    private static class ProbabilityTableDice implements Dice {
        private final Double[] probabilities;
        public ProbabilityTableDice(Double ... probabilities) {
            if (Arrays.stream(probabilities).mapToDouble(Double::doubleValue).sum() != 1.0) {
                throw new RuntimeException();
            }
            this.probabilities = probabilities;
        }

        public int roll() {
            final double randomValue = RND.nextDouble();
            double curValue = 0.0;
            for (int i = 0; i < this.probabilities.length; i++) {
                curValue += this.probabilities[i];
                if (curValue >= randomValue) {
                    return i + 1;
                }
            }
            throw new RuntimeException();
        }
    }
    private static class CollectionOfDices {
        private final Dice[] dices;

        public CollectionOfDices(Dice ... dices) {
            this.dices = dices;
        }

        public List<Integer> roll() {
            return Arrays.stream(dices).map(Dice::roll).collect(Collectors.toList());
        }
    }
    private static class DicesFactory {
        private static final DicesFactory INSTANCE = new DicesFactory();

        public static DicesFactory instance() {
            return INSTANCE;
        }

        private DicesFactory() {}

        private final Dice uniformDice = new UniformDice();
        public Dice createUniformDice() {
            return this.uniformDice;
        }
        public Dice createTrickyDice(int fixedValue) {
            return new TrickyDice(fixedValue);
        }
        public Dice createProbabilityTableDice(Double ... probabilities) {
            return new ProbabilityTableDice(probabilities);
        }
    }

    public static void main(String ... args) {
        final Scanner scan = new Scanner(System.in);

        final CollectionOfDices dice = new CollectionOfDices(
                DicesFactory.instance().createUniformDice(),
                DicesFactory.instance().createProbabilityTableDice(
                        0.15, 0.2, 0.3, 0.1, 0.25
                )
        );

        // get amount of rolls from user
        System.out.print("How many rolls do you want? ");

        int numRolls = scan.nextInt();



        int diceOne, diceTwo;
        int boxCar, snakeEyes;
        int j = 0, k = 0;

        // rolls the dice the requested amount of times
        for (int i = 0; i < numRolls; i++)
        {
            final List<Integer> rolls = dice.roll();
            // first die to roll
            diceOne = rolls.get(0);

            // second die to roll
            diceTwo = rolls.get(1);

            // display rolled dice
            System.out.println(diceOne + " " + diceTwo + "\n");

            // store and display pairs of 1 rolls
            if (diceOne == 1 && diceTwo == 1)
            {
                snakeEyes = ++j;

                System.out.println("\nThe number of snake eyes you have is: "
                        + snakeEyes + "\n");
            }


            // store and display pairs of 6 rolls
            if (diceOne == 6 && diceTwo == 6)
            {
                boxCar = ++k;

                System.out.println("\nThe number of box cars you have is: "
                        + boxCar + "\n");
            }

        }

    }
}