用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代码的实现。
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2024-11-24 07:59:25 HTTP/1.1 GET : http://xn.iwdd.cn/home/article/detail/id/47.html
- 运行时间 : 0.083508s [ 吞吐率:11.97req/s ] 内存消耗:2,051.13kb 文件加载:151
- 查询信息 : 30 queries
- 缓存信息 : 16 reads,0 writes
- 会话信息 : SESSION_ID=5197ca4bff16c35897c71ea5eaf0f52b
- CONNECT:[ UseTime:0.000462s ] mysql:host=127.0.0.1;port=3306;dbname=xn_iwdd_cn;charset=utf8mb4
- SHOW FULL COLUMNS FROM `cms_article` [ RunTime:0.000594s ]
- SELECT * FROM `cms_article` WHERE `id` = 47 LIMIT 1 [ RunTime:0.000350s ]
- SHOW FULL COLUMNS FROM `cms_article_keywords` [ RunTime:0.000443s ]
- SELECT `i`.`aid`,`i`.`keywords_id`,`k`.`title` FROM `cms_article_keywords` `i` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`i`.`keywords_id` WHERE `i`.`aid` = '47' AND `k`.`status` = '1' ORDER BY `i`.`create_time` ASC [ RunTime:0.000445s ]
- UPDATE `cms_article` SET `read` = `read` + 1 WHERE `id` = 47 [ RunTime:0.004579s ]
- SHOW FULL COLUMNS FROM `cms_article_cate` [ RunTime:0.000563s ]
- SELECT * FROM `cms_article_cate` ORDER BY `sort` DESC [ RunTime:0.000339s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 1 AND `status` = 1 LIMIT 1 [ RunTime:0.002158s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 2 AND `status` = 1 LIMIT 1 [ RunTime:0.002125s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 3 AND `status` = 1 LIMIT 1 [ RunTime:0.001945s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 4 AND `status` = 1 LIMIT 1 [ RunTime:0.001903s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 5 AND `status` = 1 LIMIT 1 [ RunTime:0.001914s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 6 AND `status` = 1 LIMIT 1 [ RunTime:0.001751s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 7 AND `status` = 1 LIMIT 1 [ RunTime:0.001750s ]
- SELECT COUNT(*) AS think_count FROM `cms_article` WHERE `cate_id` = 8 AND `status` = 1 LIMIT 1 [ RunTime:0.001668s ]
- SHOW FULL COLUMNS FROM `cms_keywords` [ RunTime:0.000491s ]
- SELECT * FROM `cms_keywords` WHERE `status` = 1 ORDER BY rand() , id desc LIMIT 20 [ RunTime:0.000380s ]
- SELECT `a`.`id`,`a`.`title`,`a`.`thumb`,`a`.`create_time`,`a`.`read`,`a`.`desc`,`f`.`filepath` FROM `cms_article` `a` LEFT JOIN `cms_file` `f` ON `a`.`thumb`=`f`.`id` WHERE `a`.`status` = '1' ORDER BY rand() , a.id desc LIMIT 10 [ RunTime:0.015109s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '438' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000479s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '1142' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000271s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '3251' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000249s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '1110' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000248s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '3279' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000206s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '4323' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000236s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '3713' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000225s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '1069' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000237s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '1218' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000257s ]
- SELECT `k`.`id`,`k`.`title` FROM `cms_article_keywords` `a` LEFT JOIN `cms_keywords` `k` ON `k`.`id`=`a`.`keywords_id` WHERE `a`.`aid` = '4385' AND `k`.`status` = '1' LIMIT 4 [ RunTime:0.000294s ]
- SHOW FULL COLUMNS FROM `cms_user_log` [ RunTime:0.000567s ]
- INSERT INTO `cms_user_log` SET `uid` = 0 , `nickname` = '游客' , `type` = 'view' , `title` = '查看' , `content` = '游客在2024-11-24 07:59:25查看了用java给我写一个飞行棋游戏文章详情' , `param_id` = 47 , `param` = '[]' , `module` = 'home' , `controller` = 'article' , `function` = 'detail' , `ip` = '3.138.69.101' , `create_time` = 1732406365 [ RunTime:0.004903s ]
0.090757s