如果骰子获得相同的五个随机值,我该如何编写方法,称之为 Yahtzee?
How can I write method for if dice gets same five random value, call it Yahtzee?
我正在尝试制作 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;
}
}
使用 class 随机数 (http://docs.oracle.com/javase/7/docs/api/java/util/Random.html) 生成随机数。结果骰子值的比较应该不会太难。
伪代码:
- 声明一个变量来保存数组中的第一个值。
- 遍历数组并将所有值与变量值进行比较
- 如果不相等,return false
- 退出循环,如果你做到了这一步,return正确
微优化,不要在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] ) {
当然假设你有五个骰子。
//添加骰子 rollsholder(例如下面的 rolledValues[])
private int [] diceRolls = new int [5];
public void RollDice() {
for(int i = 0; i < this.DiceCount; i++) {
diceRolls[i] = (this.die.Roll() +1)
System.out.print(diceRolls[i]);
}
}
public 布尔 IsAYahtzee() {
for (int i = 0; i < this.DiceCount; i++) {
if(i < (this.DiceCount-1) && diceRolls[i] != diceRolls[i+1] ){
return false;
}
}
return true;
}
我正在尝试制作 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;
}
}
使用 class 随机数 (http://docs.oracle.com/javase/7/docs/api/java/util/Random.html) 生成随机数。结果骰子值的比较应该不会太难。
伪代码:
- 声明一个变量来保存数组中的第一个值。
- 遍历数组并将所有值与变量值进行比较
- 如果不相等,return false
- 退出循环,如果你做到了这一步,return正确
微优化,不要在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] ) {
当然假设你有五个骰子。
//添加骰子 rollsholder(例如下面的 rolledValues[])
private int [] diceRolls = new int [5];
public void RollDice() {
for(int i = 0; i < this.DiceCount; i++) {
diceRolls[i] = (this.die.Roll() +1)
System.out.print(diceRolls[i]);
}
}
public 布尔 IsAYahtzee() {
for (int i = 0; i < this.DiceCount; i++) {
if(i < (this.DiceCount-1) && diceRolls[i] != diceRolls[i+1] ){
return false;
}
}
return true;
}