我为我的一项作业创建了一个简单版本的国际象棋游戏。基本上,我们只需要能够根据国际象棋规则将棋子放置到空位即可,因此无需实施检查、将死和杀死其他棋子。但是不知道为什么每次输入坐标移动棋子都不行
这是我的代码:
public class ChessGame {
//Create 8x8 board
private ChessPiece[][] board;
public ChessGame() {
super();
//Initialize board
board = new ChessPiece[8][8];
//Place pieces on board
placePieces();
}
//Place pieces on board
private void placePieces() {
//Place Pawns
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(1, i);
board[6][i] = new Pawn(6, i);
}
//Place Knights
board[0][1] = new Knight(0, 1);
board[0][6] = new Knight(0, 6);
board[7][1] = new Knight(7, 1);
board[7][6] = new Knight(7, 6);
//Place Bishops
board[0][2] = new Bishop(0, 2);
board[0][5] = new Bishop(0, 5);
board[7][2] = new Bishop(7, 2);
board[7][5] = new Bishop(7, 5);
//Place Rooks
board[0][0] = new Rook(0, 0);
board[0][7] = new Rook(0, 7);
board[7][0] = new Rook(7, 0);
board[7][7] = new Rook(7, 7);
//Place King
board[0][3] = new King(0, 3);
board[7][3] = new King(7, 3);
//Place Queen
board[0][4] = new Queen(0, 4);
board[7][4] = new Queen(7, 4);
}
//Check if the piece can move to given coordinates
public boolean canMoveTo(int x, int y) {
if (board[x][y] == null) {
return true;
}
return false;
}
//Move the piece to given coordinates
public void moveTo(int x1, int y1, int x2, int y2) {
board[x2][y2] = board[x1][y1];
board[x1][y1] = null;
}
//Start the game
public void startGame() {
//Print the initial board
printBoard();
//Loop through player turns
while (true) {
//Get player input
int[] from = getInput("Player 1 - Enter coordinates to move from: ");
int[] to = getInput("Player 1 - Enter coordinates to move to: ");
//Check if the piece can move to given coordinates
if (canMoveTo(to[0], to[1])) {
//Move the piece
moveTo(from[0], from[1], to[0], to[1]);
//Print the updated board
printBoard();
} else {
System.out.println("Illegal move, try again!");
}
}
}
//Get player input
public int[] getInput(String msg) {
//Print message
System.out.println(msg);
//Get input
Scanner sc = new Scanner(System.in);
int[] coords = new int[2];
//Split input by space
String[] input = sc.nextLine().split(" ");
//Parse coordinates
coords[0] = Integer.parseInt(input[0]);
coords[1] = Integer.parseInt(input[1]);
return coords;
}
//Print the board
public void printBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == null) {
System.out.print("- ");
} else {
System.out.print(board[i][j].getName() + " ");
}
}
System.out.println("");
}
}
public static void main(String[] args) {
ChessGame game = new ChessGame();
game.startGame();
}
}
abstract class ChessPiece {
int x;
int y;
public ChessPiece(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public abstract boolean canMoveTo(int x, int y);
public abstract void moveTo(int x, int y);
public abstract String getName();
}
class Pawn extends ChessPiece {
public Pawn(int x, int y) {
super(x, y);
}
@Override
public boolean canMoveTo(int x, int y) {
if (x == this.getX() + 1 && y == this.getY()) {
return true;
}
return false;
}
@Override
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String getName() {
return "P";
}
}