帮我写一个基于Java的飞行棋小游戏
时间: 2023-03-20 20:01:38 浏览: 104
飞行棋.rar_JAVA飞行棋_Java游戏_java 飞行棋_java写飞行棋_飞行棋游戏
好的,以下是一个简单的基于Java的飞行棋小游戏的代码示例:
```
import java.util.*;
public class FlyChess {
private static final int WINNING_SCORE = 30; // 获胜分数
private static final int BOARD_SIZE = 30; // 棋盘大小
private static final int MAX_PLAYERS = 4; // 最大玩家数
private static final String[] PLAYER_COLORS = {"红色", "黄色", "绿色", "蓝色"}; // 玩家颜色
private static final int[] PLAYER_START_POSITIONS = {0, 10, 20, 30}; // 玩家起始位置
private static final int[] DICE_VALUES = {1, 2, 3, 4, 5, 6}; // 骰子点数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来玩飞行棋游戏!");
// 创建玩家
System.out.print("请输入玩家数(最多" + MAX_PLAYERS + "人):");
int numPlayers = scanner.nextInt();
if (numPlayers > MAX_PLAYERS) {
numPlayers = MAX_PLAYERS;
}
Player[] players = new Player[numPlayers];
for (int i = 0; i < numPlayers; i++) {
System.out.print("请输入玩家" + (i + 1) + "的名称:");
String name = scanner.next();
players[i] = new Player(name, PLAYER_COLORS[i], PLAYER_START_POSITIONS[i]);
}
// 创建棋盘
Board board = new Board(BOARD_SIZE);
// 开始游戏
int currentPlayerIndex = 0;
while (true) {
Player currentPlayer = players[currentPlayerIndex];
System.out.println("轮到玩家 " + currentPlayer.getName() + ",颜色为 " + currentPlayer.getColor() + ",当前得分为 " + currentPlayer.getScore() + " 分。");
System.out.print("按 Enter 键开始掷骰子:");
scanner.nextLine(); // 读取 Enter 键
scanner.nextLine(); // 等待用户输入
int diceValue = DICE_VALUES[(int) (Math.random() * DICE_VALUES.length)];
System.out.println("掷出了 " + diceValue + " 点。");
// 玩家移动
int newPosition = currentPlayer.getPosition() + diceValue;
if (newPosition > board.getSize()) {
System.out.println("超出了棋盘范围,不能前进。");
} else {
currentPlayer.setPosition(newPosition);
int score = board.getScore(newPosition);
currentPlayer.addScore(score);
System.out.println("前进了 " + diceValue + " 步,获得了 " + score + " 分。");
}
// 判断游戏是否结束
if (currentPlayer.getScore() >= WINNING_SCORE) {
System.out.println("游戏结束,玩家 " + currentPlayer.getName() + " 获胜!");
break;
}
// 切换下一个玩家
currentPlayerIndex = (currentPlayerIndex + 1) % numPlayers;
}
}
}
class Player {
private String name;
private String
阅读全文