在国际象棋游戏中实施“检查”

时间:2015-07-09 08:20:03

标签: java

您好我是Java的新手,我已经卡住了。我正在尝试制作国际象棋游戏,除了检查和将死方法之外我做了所有的事情,如果你有任何建议,我会很乐意阅读它们。这是我的工作到现在为止:

这是我们玩游戏的棋盘类:

public class Board {

    public static final int COLOR_WHITE = 1;
    public static final int COLOR_BLACK = 2;

    public static PlayingPiece[][] board;
    private boolean isFirstMove;
    private int color;

    public Board() {
        this.setBoard(new PlayingPiece[8][8]);
        this.isFirstMove = true;
        this.initializePieces();

    }

    // Initialize the chess pieces
    public void initializePieces() {

        for (int i = 0; i < 8; i++) {
            board[1][i] = new Pawn(1, i, COLOR_WHITE);
        }

        for (int i = 0; i < 8; i++) {
            board[6][i] = new Pawn(6, i, COLOR_BLACK);
        }

        board[0][0] = new Rook(0, 0, COLOR_WHITE);
        board[0][7] = new Rook(0, 7, COLOR_WHITE);
        board[7][0] = new Rook(7, 0, COLOR_BLACK);
        board[7][7] = new Rook(7, 7, COLOR_BLACK);

        board[0][1] = new Knight(0, 1, COLOR_WHITE);
        board[0][6] = new Knight(0, 6, COLOR_WHITE);
        board[7][1] = new Knight(7, 1, COLOR_BLACK);
        board[7][6] = new Knight(7, 6, COLOR_BLACK);

        board[0][2] = new Officer(0, 2, COLOR_WHITE);
        board[0][5] = new Officer(0, 5, COLOR_WHITE);
        board[7][2] = new Officer(7, 2, COLOR_BLACK);
        board[7][5] = new Officer(7, 5, COLOR_BLACK);

        board[0][3] = new Queen(3, 0, COLOR_WHITE);
        board[0][4] = new King(4, 0, COLOR_WHITE);
        board[7][3] = new Queen(7, 3, COLOR_BLACK);
        board[7][4] = new King(7, 4, COLOR_BLACK);

        this.printBoard();

    }

    public boolean play(int color, int fromX, int fromY, int toX, int toY) {


        boolean isTrue = false;
        // Check if this is the first turn and only white can move
        if (isFirstMove && color == COLOR_WHITE) {
            isTrue = true;

        } else if (isFirstMove && color == COLOR_BLACK) {
            return false;
        }
        // check if player plays 2 times in a raw and if you move the piece from
        // current possition
        if (color == this.color || (toX == fromX && toY == fromY)) {
            return false;
        }

        isTrue = true;

        if (isTrue == true) {

            this.isFirstMove = false;
            // Check if player plays with his own color
            if (((board[fromX][fromY]).getColor() != color)) {
                return false;
            }


            // Check the isLegal movement of every chess piece
            if ((board[fromX][fromY]).move(toX, toY)) {
                board[toX][toY] = board[fromX][fromY];
                board[fromX][fromY] = null;
            }

            this.printBoard();

        }
        return isTrue;
    }

    public PlayingPiece[][] getBoard() {
        return board;
    }

    public void setBoard(PlayingPiece[][] board) {
        Board.board = board;
    }

这是Pieces类,包含各种各样的部分:

    package com.chess.www;
public class PlayingPiece {

    public static final int COLOR_WHITE = 1;
    public static final int COLOR_BLACK = 2;
    public static final char BLACK_PAWN = '\u265F';
    public static final char BLACK_ROOK = '\u265C';
    public static final char BLACK_KNIGHT = '\u265E';
    public static final char BLACK_BISHOP = '\u265D';
    public static final char BLACK_QUEEN = '\u265B';
    public static final char BLACK_KING = '\u265A';
    public static final char WHITE_PAWN = '\u2659';
    public static final char WHITE_ROOK = '\u2656';
    public static final char WHITE_KNIGHT = '\u2658';
    public static final char WHITE_BISHOP = '\u2657';
    public static final char WHITE_QUEEN = '\u2655';
    public static final char WHITE_KING = '\u2654';
    public static final char NO_PIECE = ' ';

    private int x, y;
    private boolean isAlive;
    private int color;
    private char symbol;


    protected PlayingPiece (int newX, int newY, int newColor) {
        this.setX(newX);
        this.setY(newY);
        this.color = newColor;
        this.isAlive = true;

    }

    protected PlayingPiece(int newX, int newY) {
        this.setX(newX);
        this.setY(newY);
    }


    protected PlayingPiece() {

    }

    public int getX() {
        return x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    protected boolean moveIsLegal (int newX, int newY) {
        boolean isLegal = false;

        if ((0 <= newX && newX <= 7) && (0 <= newY && newY <= 7)){

            isLegal = true;
        }
        return isLegal;
    }

    public boolean move (int newX, int newY) {
        if (moveIsLegal(newX, newY)) {
            setX(newX);
            setY(newY);
            return true;
        }
        return false;
    }


    public int getColor() {
        return color;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    public char getSymbol() {
        return symbol;
    }

    public void setSymbol(char symbol) {
        this.symbol = symbol;
    }



}

这是国王班:

package com.chess.www;

public class King extends PlayingPiece {

    public King(int newX, int newY, int color) {
        super(newX, newY, color);

        if (color == COLOR_BLACK) {
            this.setSymbol(BLACK_KING);
        } else {
            this.setSymbol(WHITE_KING);
        }

    }

    @Override
    protected boolean moveIsLegal(int newX, int newY) {

        int newPositionX = newX - getX();
        int newPositionY = newY - getY();
        int checkX = this.getX();
        int checkY = this.getY();

        if (super.moveIsLegal(newX, newY)) {

            if ((Math.abs(newPositionX) == 1) && (newY == getY())) {

                while (checkX != newX) {

                    if (this.isValidTraceX(checkX, newY, newX)) {
                        return true;
                    }

                    if (checkX > newX) {
                        checkX--;
                    } else if (this.getX() < newX) {
                        checkX++;
                    }

                }

            } else if ((newX == getX()) && (Math.abs(newPositionY) == 1)) {

                while (checkY != newY) {

                    if (this.isValidTraceY(newX, checkY, newY)) {
                        return true;
                    }

                    if (checkY > newY) {
                        checkY--;
                    } else if (this.getY() < newY) {
                        checkY++;
                    }

                }

            } else if ((Math.abs(newPositionY) == 1) == (Math.abs(newPositionX) == 1)) {

                while (checkX != newX && checkY != newY) {

                    if (this.isValidTrace(checkX, checkY, newX, newY)) {
                        return true;
                    }

                    if (checkX > newX) {
                        checkX--;
                    } else if (this.getX() < newX) {
                        checkX++;
                    }

                    if (checkY > newY) {
                        checkY--;
                    } else if (this.getY() < newY) {
                        checkY++;
                    }
                }
            }

        }
        return false;
    }

    public boolean isValidTraceX(int newX, int newY, int lastX) {

        boolean isValid = true;
        if ((Board.board[newX][newY]) != null) {
            isValid = false;
        }
        if (((Board.board[lastX][newY]) != null)) {
            if (Board.board[lastX][newY].getColor() == this.getColor()) {
                isValid = false;
            } else {
                isValid = true;
            }
        }

        return isValid;

    }

    public boolean isValidTraceY(int newX, int newY, int lastY) {

        boolean isValid = true;
        if ((Board.board[newX][newY]) != null) {
            isValid = false;
        }
        if (((Board.board[newX][lastY]) != null)) {
            if (Board.board[newX][lastY].getColor() == this.getColor()) {
                isValid = false;
            } else {
                isValid = true;
            }
        }

        return isValid;

    }

    public boolean isValidTrace(int newX, int newY, int lastX, int lastY) {

        boolean isValid = true;
        if ((Board.board[newX][newY]) != null) {
            isValid = false;
        }
        if (((Board.board[lastX][lastY]) != null)) {
            if (Board.board[lastX][lastY].getColor() == this.getColor()) {
                isValid = false;
            } else {
                isValid = true;
            }
        }

        return isValid;

    }
}

我想实现check in board class的方法你有什么建议吗?

3 个答案:

答案 0 :(得分:0)

我实现的方式只是King类中的一个方法,叫做isChecked。

boolean isChecked() {
    /* Check straight lines */
    for (directions) { // up, down, left and right
        for (square in direction) { // square by square from the king and out in the current direction
            if (square contains opponent rook or queen)
                return true;
            else if (square contains friendly piece)
                continue;

    /* Check diagonals */
    for (directions) { // left-up, left-down, right-up and right-down
        for (square in direction) { // square by square from the king and out in the current direction
            if (square contains opponent bishop or queen)
                return true;
            else if (square contains friendly piece)
                continue;

    /* Check pawns */
    if (squares where pawns would threaten the king contains pawns)
        return true;

    /* Check king, this is to find if a square is legal to move to only */
    if (squares where a king would threaten the king contains king)
        return true;

    /* Check knights */
    if (squares where knights would threaten the king contains knights)
        return true;

抱歉,我现在没有时间编写完整的代码,但伪代码应该足以理解这个概念。如果你需要我可以在今天晚些时候解决它。

原则是,检查国王可以检查的每种可能方式。 如果检查了一个国王并且还检查了所有可能的移动,那么它就是一个将死的。

希望有所帮助:)

答案 1 :(得分:0)

你可以检查一种颜色的任何一件是否合法移动到对面的国王。

protected boolean isCheck(int color) {

    int oppositeColor = color == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE;

    // Get position of the opposite king
    int kingX = getKingXPosition(oppositeColor);
    int kingY = getKingYPosition(oppositeColor);

    // Iterate over the board and check if a piece of the color received has a legal move to the king
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 0; j++) {
            if (board[i][j].getColor() == color && board[i][j].moveIsLegal(kingX, kingY)) {
                return true;
            }
        }
    }
    return false;
}

protected int getKingXPosition(int color) {
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 0; j++) {
            if (board[i][j] instanceof King && board[i][j].getColor() == color) {
                return i;
            }
        }
    }
}

protected int getKingYPosition(int color) {
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 0; j++) {
            if (board[i][j] instanceof King && board[i][j].getColor() == color) {
                return j;
            }
        }
    }
}

答案 2 :(得分:0)

import java.util.Arrays;
    public class KingsChecker {
        public static void main(String[] args) {

            int n = Integer.parseInt(args[0]);
            String[][] chess = new String[n][n];
            print(chess,n);

}
 public static void print (String[][] chess,int n) {

        for(int i = 0; i< chess.length; i++) {

            double random = Math.random()*n; //to generate a random number.

            for(int j = 0; j < chess[i].length; j++) {

                int column = (int)random;   
                int[] count = new int[n];

                chess[i][j]= "+";
                count[column]++;

                if (count[column]<=1){
                    chess[i][column]="k";
                     System.out.print(chess[i][j]);
                 }
            }
            System.out.println();
        }
 }
    }