Java 若骰子得到相同的五个随机值,我如何编写方法,称之为Yahtzee?

Java 若骰子得到相同的五个随机值,我如何编写方法,称之为Yahtzee?,java,netbeans,Java,Netbeans,我在试着让Yahtzee。。。但我有一个问题,我如何写掷骰子的方法,以及如果所有的骰子都必须相等并称之为Yahtzee,我如何写方法 public class YahtzeeDice { private final int DiceCount = 5; // There are 5 dice in the game private final Die die; // But we only need 1 p

我在试着让Yahtzee。。。但我有一个问题,我如何写掷骰子的方法,以及如果所有的骰子都必须相等并称之为Yahtzee,我如何写方法

public class YahtzeeDice {

    private final int DiceCount = 5;            // There are 5 dice in the game
    private final Die die;                      // But we only need 1
    private int rolledValues[];    // We roll it 5 times and record the values

    /**
     * This is the normal constructor for YahtzeeDice. It creates a new object 
     * and initializes the rolledValues by rolling the dice, since dice are always
     * showing some kind of value.
     * @param generator variable of type Random 
     */
    public YahtzeeDice(Random generator) {
        this.die = new Die(generator);
        this.rolledValues =  new int[this.DiceCount];
        this.RollDice();
    }

    /**
     * This constructor exists for unit testing purposes. You can create a new 
     * object with explicit values of each of the 5 dice. Useful for testing the
     * IsAYahtzee() method.
     * @param generator variable of type Random 
     * @param initialValues an array of initial dice values
     */
    public YahtzeeDice(Random generator, int initialValues[] ) {        
        this(generator); // call the other rconstructor                            
        for (int i = 0; i < this.DiceCount; i++) {
            this.rolledValues[i] = initialValues[i];        
        }
    }

    /**
     * Rolls all 5 Yahtzee dice. It should be implemented by rolling the 1 die
     * 5 times are recording the value of the roll 5 times.
     */
    public void RollDice() {
        //TODO: Implement this method here
       for(int i = 0; i < this.DiceCount; i++) {
           System.out.print((this.die.Roll() +1));
    }
    }
    /**
     * Checks for a Yahtzee condition - all 5 dice are the same value.
     * @return true when Yahtzee, false otherwise
     */
    public boolean IsAYahtzee() {
        // TODO: Implement this method here
        for (int i = 0; i < this.DiceCount; i++) {

        }
        return true; 
    }
    /**
     * Prints out the current values of the dice rolls for example if you
     * rolled a 1,5,3,2,2 it would return the String [1|5|3|2|2]
     * @return String representation of the dice values.
     */
    @Override
    public String toString() {
        String result = "[";
        for (int i = 0; i < this.DiceCount-1; i++) {
            result += this.rolledValues[i] + "|";
        }
        result += this.rolledValues[this.DiceCount-1] + "]";
        return result;
    }

}
使用类Random生成随机数。结果值的比较应该不会太难。

伪代码:

声明一个变量以保存数组中的第一个值。 循环遍历数组并将所有值与变量值进行比较 如果不相等,则返回false 退出循环,如果您完成了这一步,则返回true
微观优化,不要访问for循环中数组中的第一个值。

这有点冗长,但要确定一个Yahtzee,你就不能这样做吗

if ( this.rolledValues[0] == this.rolledValues[1] && this.rolledValues[1] == this.rolledValues[2] && this.rolledValues[2] == this.rolledValues[3] && this.rolledValues[3] == this.rolledValues[4] ) {

当然,假设您有五个骰子。

//添加骰子滚动夹,例如,在滚动值下方[]

private int [] diceRolls = new int [5];
公共空掷骰子{

   for(int i = 0; i < this.DiceCount; i++) {
       diceRolls[i] = (this.die.Roll() +1)
       System.out.print(diceRolls[i]);
}
}

公共布尔值IsAYahtzee{

    for (int i = 0; i < this.DiceCount; i++) {

         if(i < (this.DiceCount-1) &&  diceRolls[i] != diceRolls[i+1] ){
            return false;
        }
    }
    return true; 
}

For循环可以结束于this.DiceCount-1以简化代码:For int i=0;i