用Java写一个军棋的算法代码
时间: 2024-06-09 07:07:20 浏览: 171
军棋是一种棋类游戏,也称为陆战棋。下面是一个简单的Java代码,实现了基本的军棋规则:
```java
public class JunQiGame {
private final static int BOARD_SIZE = 9;
private final static int FLAG_RANK = 8;
private final static int BOMB_RANK = 11;
private int[][] board;
private boolean redTurn;
public JunQiGame() {
board = new int[BOARD_SIZE][BOARD_SIZE];
redTurn = true;
initializeBoard();
}
private void initializeBoard() {
// 初始化红方棋子
board[0][0] = board[0][8] = board[2][6] = board[3][7] = board[5][7] = board[6][6] = board[8][0] = board[8][8] = 1;
board[0][1] = board[0][7] = board[2][5] = board[3][6] = board[5][6] = board[6][5] = board[8][1] = board[8][7] = 2;
board[0][2] = board[0][6] = board[2][4] = board[3][5] = board[5][5] = board[6][4] = board[8][2] = board[8][6] = 3;
board[0][3] = board[0][5] = board[2][3] = board[3][4] = board[5][4] = board[6][3] = board[8][3] = board[8][5] = 4;
board[0][4] = board[2][2] = board[3][3] = board[5][3] = board[6][2] = board[8][4] = 5;
board[2][1] = board[3][2] = board[5][2] = board[6][1] = 6;
board[2][0] = board[3][1] = board[5][1] = board[6][0] = 7;
board[1][0] = board[1][2] = board[1][4] = board[1][6] = board[1][8] = board[7][0] = board[7][2] = board[7][4] = board[7][6] = board[7][8] = 9;
board[1][1] = board[1][7] = board[7][1] = board[7][7] = 10;
board[1][3] = board[1][5] = board[7][3] = board[7][5] = BOMB_RANK;
board[1][9] = board[7][9] = FLAG_RANK;
// 初始化蓝方棋子
for (int i = 0; i < BOARD_SIZE; i++) {
board[BOARD_SIZE - 1][i] = -board[0][i];
}
}
public void move(int fromX, int fromY, int toX, int toY) throws InvalidMoveException {
if (board[fromX][fromY] == 0) {
throw new InvalidMoveException("No piece at (" + fromX + "," + fromY + ")");
}
if (board[fromX][fromY] < 0 && redTurn || board[fromX][fromY] > 0 && !redTurn) {
throw new InvalidMoveException("It's not your turn");
}
int rank = Math.abs(board[fromX][fromY]);
if (rank == FLAG_RANK || rank == BOMB_RANK) {
throw new InvalidMoveException("Flag and bomb can not move");
}
if (board[toX][toY] > 0 && redTurn || board[toX][toY] < 0 && !redTurn) {
throw new InvalidMoveException("Can not capture your own piece");
}
if (!isMoveLegal(fromX, fromY, toX, toY)) {
throw new InvalidMoveException("Illegal move");
}
int capturedPiece = board[toX][toY];
board[toX][toY] = board[fromX][fromY];
board[fromX][fromY] = 0;
if (capturedPiece == FLAG_RANK) {
System.out.println("Red wins!");
System.exit(0);
} else if (capturedPiece == -FLAG_RANK) {
System.out.println("Blue wins!");
System.exit(0);
}
redTurn = !redTurn;
}
private boolean isMoveLegal(int fromX, int fromY, int toX, int toY) {
int dx = Math.abs(toX - fromX);
int dy = Math.abs(toY - fromY);
int rank = Math.abs(board[fromX][fromY]);
if (rank == 1) { // 炸弹特殊规则
if (dx + dy != 1) {
return false;
}
if (board[toX][toY] == 0) {
return true;
}
if (board[toX][toY] == BOMB_RANK) {
return true;
}
return false;
} else if (rank == 2) { // 工兵特殊规则
if (dx + dy != 1) {
return false;
}
if (board[toX][toY] == 0) {
return true;
}
if (board[toX][toY] == BOMB_RANK) {
return true;
}
if (board[toX][toY] == FLAG_RANK) {
return true;
}
if (Math.abs(board[toX][toY]) == 11) { // 工兵可以拆掉炸弹
return true;
}
return false;
} else if (rank == 3) { // 地雷特殊规则
if (board[toX][toY] == 0) {
return true;
}
return false;
} else { // 其他棋子规则相同
if ((dx == 0 && dy == 1) || (dx == 1 && dy == 0)) {
return true;
}
return false;
}
}
public void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
}
}
```
上面的代码实现了军棋的基本规则,包括棋盘的初始化、移动规则、胜负判断等。你可以在此基础上进行扩展,添加更多的功能和规则。
阅读全文