你能帮我生成一个Greedy Dice game的java程序吗
时间: 2023-05-24 17:04:39 浏览: 42
当然可以,以下是一个简单的Greedy Dice游戏java程序:
```java
import java.util.*;
public class GreedyDice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int currentPlayer = 1;
int[] scores = new int[2]; // 两名玩家的得分
int turnTotal = 0;
boolean isGameOver = false;
while (!isGameOver) {
System.out.println("-------------- Round " + (currentPlayer == 1 ? "One" : "Two") + " --------------");
System.out.println("Current scores: Player One - " + scores[0] + ", Player Two - " + scores[1]);
System.out.println("It's player " + currentPlayer + "'s turn.");
String choice = "";
int roll = 0;
while (choice != "h" && (roll == 0 || choice != "s")) {
roll = rollDie();
System.out.println("You rolled a " + roll);
if (roll == 1) {
System.out.println("Uh oh, you lose all your points this turn.");
turnTotal = 0;
choice = "s";
} else {
turnTotal += roll;
System.out.println("Your turn total is now " + turnTotal);
System.out.print("Do you want to (h)old or (r)oll? ");
choice = input.nextLine();
}
}
if (choice.equals("s")) { // 玩家选择停止掷骰子
System.out.println("Turn over. You earned " + turnTotal + " points this turn.");
scores[currentPlayer - 1] += turnTotal;
turnTotal = 0;
if (scores[currentPlayer - 1] >= 100) { // 判断是否获胜并结束游戏
isGameOver = true;
System.out.println("Congratulations, player " + currentPlayer + "! You won the game!");
} else {
currentPlayer = currentPlayer == 1 ? 2 : 1; // 切换到下一个玩家
}
} else { // 玩家选择继续掷骰子
System.out.println("You chose to roll again. Good luck!");
}
}
}
// 随机生成1~6的整数,模拟掷骰子
public static int rollDie() {
return (int) (Math.random() * 6) + 1;
}
}
```
该程序实现了简单的控制台界面,玩家可通过输入"h"或"s"来选择是否继续掷骰子,当其中一名玩家得分达到100分后,游戏结束,程序输出获胜者信息。
希望能帮到你!