java代码具体实现华容道
时间: 2023-11-24 15:54:58 浏览: 51
华容道是一种非常经典的益智游戏,以下是一个简单的Java实现示例:
```java
import java.util.Scanner;
public class HuaRongDao {
// 棋盘大小为5x4
private static final int ROW = 5;
private static final int COLUMN = 4;
// 棋子类型
private static final int CAO_CAO = 1;
private static final int ZHANG_FEI = 2;
private static final int GUAN_YU = 3;
private static final int HUANG_ZHONG = 4;
private static final int SOLDIER = 5;
// 棋子名称
private static final String[] NAME = {"", "曹操", "张飞", "关羽", "黄忠", "士兵"};
// 棋盘
private static int[][] chessboard = new int[ROW][COLUMN];
// 初始化棋盘
private static void initChessboard() {
// 初始化棋子位置
chessboard[0][0] = CAO_CAO; // 曹操
chessboard[0][1] = SOLDIER; // 士兵
chessboard[0][2] = SOLDIER; // 士兵
chessboard[0][3] = SOLDIER; // 士兵
chessboard[1][0] = SOLDIER; // 士兵
chessboard[1][1] = SOLDIER; // 士兵
chessboard[1][2] = ZHANG_FEI; // 张飞
chessboard[1][3] = SOLDIER; // 士兵
chessboard[2][0] = SOLDIER; // 士兵
chessboard[2][1] = GUAN_YU; // 关羽
chessboard[2][2] = SOLDIER; // 士兵
chessboard[2][3] = SOLDIER; // 士兵
chessboard[3][0] = SOLDIER; // 士兵
chessboard[3][1] = SOLDIER; // 士兵
chessboard[3][2] = HUANG_ZHONG; // 黄忠
chessboard[3][3] = SOLDIER; // 士兵
chessboard[4][0] = SOLDIER; // 士兵
chessboard[4][1] = SOLDIER; // 士兵
chessboard[4][2] = SOLDIER; // 士兵
chessboard[4][3] = SOLDIER; // 士兵
}
// 打印棋盘
private static void printChessboard() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
System.out.print(NAME[chessboard[i][j]] + "\t");
}
System.out.println();
}
}
// 判断是否可以移动
private static boolean canMove(int x, int y, int nx, int ny) {
if (nx < 0 || ny < 0 || nx >= ROW || ny >= COLUMN) {
return false; // 超出边界
}
if (chessboard[x][y] == CAO_CAO && chessboard[nx][ny] == HUANG_ZHONG) {
return false; // 曹操不能与黄忠交换位置
}
if (chessboard[x][y] == HUANG_ZHONG && chessboard[nx][ny] == CAO_CAO) {
return false; // 曹操不能与黄忠交换位置
}
if (chessboard[x][y] == ZHANG_FEI && chessboard[nx][ny] == GUAN_YU) {
return false; // 张飞不能与关羽交换位置
}
if (chessboard[x][y] == GUAN_YU && chessboard[nx][ny] == ZHANG_FEI) {
return false; // 张飞不能与关羽交换位置
}
if (chessboard[x][y] == SOLDIER && chessboard[nx][ny] != 0) {
return false; // 士兵只能移动到空位置
}
return true;
}
// 移动棋子
private static void moveChessman(int x, int y, int nx, int ny) {
int tmp = chessboard[x][y];
chessboard[x][y] = chessboard[nx][ny];
chessboard[nx][ny] = tmp;
}
// 判断是否胜利
private static boolean isWin() {
return chessboard[0][1] == CAO_CAO && chessboard[0][2] == CAO_CAO;
}
public static void main(String[] args) {
initChessboard();
printChessboard();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要移动的棋子的位置和目标位置,如 1 1 1 2 表示将(1,1)位置的棋子移动到(1,2)位置:");
int x = scanner.nextInt() - 1;
int y = scanner.nextInt() - 1;
int nx = scanner.nextInt() - 1;
int ny = scanner.nextInt() - 1;
if (canMove(x, y, nx, ny)) {
moveChessman(x, y, nx, ny);
printChessboard();
if (isWin()) {
System.out.println("恭喜你获得胜利!");
break;
}
} else {
System.out.println("无法移动,请重新输入!");
}
}
}
}
```
这个程序实现了一个基本的华容道游戏,你可以自己尝试一下。
阅读全文