您的当前位置:首页>全部文章>文章详情

用java给我写一个飞行棋游戏

发表于:2022-11-24 10:23:32浏览:28次TAG:
好的,下面是一个简单的飞行棋游戏的Java代码。 首先,我们需要定义几个重要的类:`Player`,`Board`和`Main`。 1. `Player`类:用于表示玩家,包含玩家的名称、当前所在位置等信息。 ```java public class Player { private String name; // 玩家名字 private int position; // 当前位置 private boolean finished; // 是否已经到达终点 public Player(String name) { this.name = name; this.position = 0; this.finished = false; } public String getName() { return name; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public boolean isFinished() { return finished; } public void setFinished(boolean finished) { this.finished = finished; } } ``` 2. `Board`类:用于表示飞行棋的游戏板,包含游戏的方格、道具等信息。 ```java public class Board { private static final int BOARD_SIZE = 50; // 游戏板大小 private static final int MAX_PLAYER = 4; // 最大玩家数 private static final int PLANE_COUNT = 4; // 飞机数量 private int[] board; // 游戏方格数组 private int[] prop; // 道具数组 private Player[] players; // 玩家数组 private int currentPlayer; // 当前玩家 private int[] planes; // 飞机数组 public Board() { board = new int[BOARD_SIZE]; prop = new int[BOARD_SIZE]; players = new Player[MAX_PLAYER]; planes = new int[PLANE_COUNT]; initBoard(); initProp(); initPlayers(); initPlanes(); } private void initBoard() { // 将游戏板初始化为普通方格 Arrays.fill(board, 0); // 将终点设为必经之路 board[BOARD_SIZE - 1] = -1; } private void initProp() { // 将道具初始化为普通方格 Arrays.fill(prop, 0); // 将跳步道具设为必经之路 prop[5] = 1; // 将暂停道具设为必经之路 prop[9] = 2; // 将奖励道具设为必经之路 prop[14] = 3; } private void initPlayers() { // 将玩家数组初始化为空 Arrays.fill(players, null); } private void initPlanes() { // 将飞机位置初始化为起点 Arrays.fill(planes, 0); } public void addPlayer(Player player) { // 添加新玩家 for (int i = 0; i < MAX_PLAYER; i++) { if (players[i] == null) { players[i] = player; break; } } } public void nextTurn() { // 切换到下一个玩家 currentPlayer++; if (currentPlayer >= MAX_PLAYER) { currentPlayer = 0; } } public Player getCurrentPlayer() { return players[currentPlayer]; } public boolean move(int steps) { // 当前玩家移动指定步数 int newPos = players[currentPlayer].getPosition() + steps; if (newPos >= BOARD_SIZE) { return false; } int oldPos = players[currentPlayer].getPosition(); players[currentPlayer].setPosition(newPos); // 如果经过奖励道具,则将奖励转移给下一个玩家 if (prop[newPos] == 3) { prop[newPos] = 0; nextTurn(); } // 如果经过跳步道具,则跳到指定方格 else if (prop[newPos] == 1) { prop[newPos] = 0; move(steps); } // 如果经过暂停道具,则当前玩家被暂停一回合 else if (prop[newPos] == 2) { prop[newPos] = 0; nextTurn(); } else { nextTurn(); } // 移动完成后检查是否到达终点 if (newPos == BOARD_SIZE - 1) { players[currentPlayer].setFinished(true); } // 比较新位置和旧位置,确定飞机方向 if (newPos > oldPos) { planes[currentPlayer] = (planes[currentPlayer] + steps) % 6; } else if (newPos < oldPos) { planes[currentPlayer] = (planes[currentPlayer] - steps + 6) % 6; } return true; } public int getPlane(int player) { return planes[player]; } public int getBoardSize() { return BOARD_SIZE; } public int getPropAt(int pos) { return prop[pos]; } public int getPosAt(int pos) { return board[pos]; } } ``` 3. `Main`类:用于控制游戏流程,包含游戏初始化、游戏循环等方法。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 创建游戏板和玩家 Board board = new Board(); board.addPlayer(new Player("A")); board.addPlayer(new Player("B")); board.addPlayer(new Player("C")); board.addPlayer(new Player("D")); // 游戏循环 while (true) { Player player = board.getCurrentPlayer(); System.out.println(player.getName() + " 的回合,当前位置为 " + player.getPosition()); // 等待玩家输入 System.out.println("请输入掷骰子所得点数(1-6):"); int steps = scanner.nextInt(); // 移动玩家并输出结果 boolean success = board.move(steps); if (success) { System.out.println(player.getName() + " 获得 " + steps + " 点前进。"); int pos = player.getPosition(); if (board.getPosAt(pos) != 0) { int prop = board.getPropAt(pos); switch (prop) { case 1: System.out.println(player.getName() + " 使用了跳一步道具并到达了 " + pos); break; case 2: System.out.println(player.getName() + " 踩到了暂停道具,下一回合将无法行动。"); break; case 3: System.out.println(player.getName() + " 捡到了奖励,将得到额外一次行动机会。"); break; default: System.out.println("无效方格。"); break; } } else { System.out.println(player.getName() + " 正常前进。"); } } else { System.out.println(player.getName() + " 超出了边界,本回合结束。"); } // 判断游戏是否结束 boolean allFinished = true; for (Player p : board.getPlayers()) { if (!p.isFinished()) { allFinished = false; break; } } if (allFinished) { System.out.println("游戏结束。"); break; } } } } ``` 这就是一个简单的飞行棋游戏Java代码的实现。
0.090757s