帮我写一个基于Java的飞行棋小游戏
时间: 2023-05-15 17:07:17 浏览: 175
java飞行棋游戏(全套源码)
好的,我可以为您提供一个基于Java的飞行棋小游戏的代码示例,您可以参考以下代码:
```
import java.util.Scanner;
public class FlightChessGame {
private static final int BOARD_SIZE = 30;
private static final int[] LUCKY_SQUARES = {5, 10, 15, 20, 25};
private static final int[] DANGER_SQUARES = {4, 9, 14, 19, 24};
private static final int[] FLIGHT_SQUARES = {6, 12, 18, 24};
private static final int[] BACK_SQUARES = {11, 17, 23, 29};
private static final int[] WINNING_SQUARES = {30};
private static final int MAX_ROLL = 6;
private int[] playerPositions;
private int currentPlayer;
public FlightChessGame() {
playerPositions = new int[2];
currentPlayer = 0;
}
public void play() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Flight Chess Game!");
while (true) {
System.out.println("Player " + (currentPlayer + 1) + ", it's your turn. Press enter to roll the dice.");
scanner.nextLine();
int roll = rollDice();
System.out.println("You rolled a " + roll + ".");
int newPosition = playerPositions[currentPlayer] + roll;
if (newPosition > BOARD_SIZE) {
System.out.println("You need to roll exactly " + (BOARD_SIZE - playerPositions[currentPlayer]) + " to reach the end.");
continue;
}
playerPositions[currentPlayer] = newPosition;
System.out.println("You moved to square " + newPosition + ".");
if (isLuckySquare(newPosition)) {
System.out.println("You landed on a lucky square! Roll again.");
continue;
}
if (isDangerSquare(newPosition)) {
System.out.println("You landed on a danger square! Move back 2 squares.");
playerPositions[currentPlayer] -= 2;
}
if (isFlightSquare(newPosition)) {
System.out.println("You landed on a flight square! Move forward 3 squares.");
playerPositions[currentPlayer] += 3;
}
if (isBackSquare(newPosition)) {
System.out.println("You landed on a back square! Move back 3 squares.");
playerPositions[currentPlayer] -= 3;
}
if (isWinningSquare(newPosition)) {
System.out.println("Congratulations, player " + (currentPlayer + 1) + ", you won!");
break;
}
currentPlayer = (currentPlayer + 1) % 2;
}
}
private int rollDice() {
return (int) (Math.random() * MAX_ROLL) + 1;
}
private boolean isLuckySquare(int square) {
for (int luckySquare : LUCKY_SQUARES) {
if (square == luckySquare) {
return true;
}
}
return false;
}
private boolean isDangerSquare(int square) {
for (int dangerSquare : DANGER_SQUARES) {
if (square == dangerSquare) {
return true;
}
}
return false;
}
private boolean isFlightSquare(int square) {
for (int flightSquare : FLIGHT_SQUARES) {
if (square == flightSquare) {
return true;
}
}
return false;
}
private boolean isBackSquare(int square) {
for (int backSquare : BACK_SQUARES) {
if (square == backSquare) {
return true;
}
}
return false;
}
private boolean isWinningSquare(int square) {
for (int winningSquare : WINNING_SQUARES) {
if (square == winningSquare) {
return true;
}
}
return false;
}
}
```
您可以将以上代码保存为 FlightChessGame.java 文件,并在命令行中运行该文件以启动游戏。希望这可以帮助到您!
阅读全文