import java.util.Scanner;
public class ChessGame {
private static char[][] board;public static void main(String[] args) {
initializeBoard();
printBoard();Scanner scanner = new Scanner(System.in);
boolean isWhiteTurn = true;while (true) {
String player = isWhiteTurn ? "White" : "Black";
System.out.println(player + "'s turn");System.out.print("Enter the current position of the piece (e.g., a1): ");
String currentPosition = scanner.nextLine();
int currentRow = 8 - (currentPosition.charAt(1) - '0');
int currentCol = currentPosition.charAt(0) - 'a';System.out.print("Enter the new position of the piece (e.g., a2): ");
String newPosition = scanner.nextLine();
int newRow = 8 - (newPosition.charAt(1) - '0');
int newCol = newPosition.charAt(0) - 'a';if (isValidMove(currentRow, currentCol, newRow, newCol, isWhiteTurn)) {
movePiece(currentRow, currentCol, newRow, newCol);
promotePawn(newRow, newCol, isWhiteTurn);
printBoard();
isWhiteTurn = !isWhiteTurn;
} else {
System.out.println("Invalid move! Try again.");
}
}
}private static void initializeBoard() {
board = new char[8][8];
// 初始化棋盘
// 'w'代表白棋,'b'代表黑棋
board[0] = new char[]{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'};
board[1] = new char[]{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'};
board[6] = new char[]{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'};
board[7] = new char[]{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'};
for (int i = 2; i < 6; i++) {
board[i] = new char[]{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
}
}private static void printBoard() {
System.out.println(" a b c d e f g h");
for (int i = 0; i < 8; i++) {
System.out.print(8 - i + " ");
for (int j = 0; j < 8; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println(8 - i);
}
System.out.println(" a b c d e f g h");
}private static boolean isValidMove(int currentRow, int currentCol, int newRow, int newCol, boolean isWhiteTurn) {
char piece = board[currentRow][currentCol];
char targetPiece = board[newRow][newCol];// 检查是否是合法的移动
// 在这个示例中,我们假设所有移动都是合法的
// 实际上,您需要编写更复杂的逻辑来验证移动是否符合象棋规则if (isWhiteTurn) {
return Character.isUpperCase(targetPiece) || targetPiece == ' ';
} else {
return Character.isLowerCase(targetPiece) || targetPiece == ' ';
}
}private static void movePiece(int currentRow, int currentCol, int newRow, int newCol) {
char piece = board[currentRow][currentCol];
board[currentRow][currentCol] = ' ';
board[newRow][newCol] = piece;
}private static void promotePawn(int row, int col, boolean isWhiteTurn) {
char piece = board[row][col];if (Character.toLowerCase(piece) == 'p' && (row == 0 || row == 7)) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the promotion piece (Q, R, B, or N): ");
char promotionPiece = scanner.nextLine().charAt(0);// 根据用户输入的升变棋子类型进行升变
switch (Character.toUpperCase(promotionPiece)) {
case 'Q':
board[row][col] = isWhiteTurn ? 'Q' : 'q';
break;
case 'R':
board[row][col] = isWhiteTurn ? 'R' : 'r';
break;
case 'B':
board[row][col] = isWhiteTurn ? 'B' : 'b';
break;
case 'N':
board[row][col] = isWhiteTurn ? 'N' : 'n';
break;
default:
System.out.println("Invalid promotion piece! Using Queen as default.");
board[row][col] = isWhiteTurn ? 'Q' : 'q';
break;
}
}
}
}
用Java实现的象棋游戏
最新推荐文章于 2024-07-10 21:37:49 发布