我正在制作一个有两个玩家的骰子游戏,每个玩家以100分开始。对于每次掷骰,玩家都会失去10分,但根据他们从两个骰子获得的总和,他们将获得一些积分。我的错误是循环没有停止(它保持循环)。这是代码:
public class DiceGameC {
private static Die die = new Die(12);
private static Player p1 = new Player("Wasi");
private static Player p2 = new Player("Pruthvi");
public static void main(String[] args) {
do {
p1.rollDie(die);
setPlayerScore(p1);
p2.rollDie(die);
setPlayerScore(p2);
}
while (p1.getScore() != 0 || p2.getScore() != 0);
if(p1.getScore() == 0) {
System.out.println(p1.getName() + " lost the game! The winner is: " + p2.getName());
}
else if(p2.getScore() == 0) {
System.out.println(p2.getName() + " lost the game! The winner is: " + p1.getName());
}
}
private static void setPlayerScore(Player player) {
int playerScore = player.getScore();
if(player.getDiceValue(die) == 2) {
player.setScore(playerScore);
}
else if (player.getDiceValue(die) == 3) {
player.setScore(playerScore + 2);
}
else if (player.getDiceValue(die) == 4) {
player.setScore(playerScore + 3);
}
else if (player.getDiceValue(die) == 5) {
player.setScore(playerScore + 5);
}
else if (player.getDiceValue(die) == 6) {
player.setScore(playerScore + 7);
}
else if (player.getDiceValue(die) == 7) {
player.setScore(playerScore + 10);
}
else if (player.getDiceValue(die) == 8) {
player.setScore(playerScore + 11);
}
else if (player.getDiceValue(die) == 9) {
player.setScore(playerScore + 13);
}
else if (player.getDiceValue(die) == 10) {
player.setScore(playerScore + 15);
}
else if (player.getDiceValue(die) == 11) {
player.setScore(playerScore + 17);
}
else if (player.getDiceValue(die) == 12) {
player.setScore(playerScore + 20);
}
}
}
玩家类:
public class Player {
private String name;
private int score;
private Die die;
public Player() {
this.setName("Anonymous");
this.setScore(100);
}
public Player(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void rollDie(Die die) {
die.rollDie();
setScore(score - 10);
}
public int getDiceValue(Die die) {
return die.getValue();
}
}
模具类:
public class Die {
// Instance variables (properties / attributes / data)
private int faces;
private int value;
/*
* Creates an instance of Die
*/
public Die() {
// regular die
this.faces = 6;
}
public Die(int faces) {
this.faces = faces;
}
// behaviour for a Die - a method roll my die
public void rollDie() {
this.value = (int)(Math.random() * this.faces + 1);
}
// getter method for getting the value
public int getValue() {
return this.value;
}
public static void main(String[] args) {
// create two Dice objects
Die d1 = new Die();
Die d2 = new Die();
d1.rollDie(); // roll the first die
d2.rollDie(); //roll the second die
System.out.println(d1.getValue());
System.out.println(d2.getValue());
Die d3 = new Die(20); // create a value with 20 faces
d3.rollDie();
System.out.println(d3.getValue());
}
public boolean equals(Die die) {
return this.value == die.getValue();
}
public boolean equalsOtherWay(Die die) {
if(this.value == die.getValue()) {
return true;
}
else {
return false;
}
}
}
答案 0 :(得分:3)
您必须替换while循环中的条件。你说:
while (p1.getScore() != 0 || p2.getScore() != 0);
但是球员的得分可能是< 0,所以玩家的分数永远不会是0。 所以你必须改变条件,以便在分数变为< 0,就像那样:
while (p1.getScore() > 0 && p2.getScore() > 0);