用Java实现的象棋游戏

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;
            }
        }
    }
}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值