首页 > 解决方案 > java中象棋游戏设计中对OOP的误解

问题描述

我为练习 Java 技能而编写的代码有问题。我正在尝试编写一个国际象棋游戏。无论如何,在我的项目中,我有一个名为 ChessGUI 的类,其中包含 GUI 和我需要玩的方法(多人游戏)。我愿意稍后将代码更改为 MVC 模式,以便更容易阅读/编辑/理解。我有一个名为 Figure 的抽象类,我将它扩展为 12 个类(每种颜色 6 个数字):

像这样:

然而,在我的 ChessGui 类中,我有一个名为 CheckAvailableSquares() 的方法,只要选择了棋盘上的棋子(通过鼠标单击),我就会调用该方法,以在棋盘上显示我可以将所选棋子移动到的可用方格。

 private void CheckAvailableSquares(Figure figure){
    for (int row = 0; row < 8; row++){
        for (int col = 0; col < 8; col++){
            if (row == figure.getRowPos() && col == figure.getColPos()) //bypass the actual square where my piece stands
                continue;
            if (figure.isValidMove(board,figure.getRowPos(), figure.getColPos(),row,col)){
                board[row][col].setBackground(Color.GREEN);
            }
        }
    }
}

在每个图形子类中都有一个名为 isValidMove() 的方法,它成为棋子的当前位置和想要的位置,如果移动有效,则返回一个布尔值。

如您所知,pawn 在第一次移动时每次可以移动 2 个方格,这就是为什么我向 WhitePawn 和 BlackPawn 类添加了一个名为 firstMovePlayed 的布尔属性。它具有 false 值,并且每当调用 isMoveValid() 方法并且想要的移动有效时,它应该更改为 true。像那样:

public class WhitePawn extends Figure {
private boolean firstMovePlayed = false;

public WhitePawn(){
    super.setColor(Color.WHITE);
}

@Override
public boolean isValidMove(Square[][] board, int currentRow, int currentCol, int wantedRow, int wantedCol){
    int rowDelta = currentRow - wantedRow; //no Math.abs because a pawn can't move backwards.
    int colDelta = Math.abs(wantedCol - currentCol);

    //if we have an own piece on the wanted square
    if (!board[wantedRow][wantedCol].isEmpty() && board[wantedRow][wantedCol].getFigure().getColor().equals(Color.WHITE)) {
        return false;
    }
    //on the first move a pawn can move 2 square at a time, and when moving 2 square he can't capture. A pawn can't move two square if there is another piece in the way. TODO: en passant ??????
    if (rowDelta == 2 && colDelta == 0 && board[wantedRow][wantedCol].isEmpty() && board[wantedRow + 1][wantedCol].isEmpty() && !firstMovePlayed) {
        firstMovePlayed = true;
        return true;
    }
    else if (rowDelta == 1 && colDelta == 1 && !board[wantedRow][wantedCol].isEmpty()) { //capture
        if (board[wantedRow][wantedCol].getFigure().getColor() != board[currentRow][currentCol].getFigure().getColor())
            firstMovePlayed = true;
        return true;
    }
    else if (rowDelta == 1 && colDelta == 0 && board[wantedRow][wantedCol].isEmpty()) { //moving one square forward without a capture
        firstMovePlayed = true;
        return true;
    }
    //those were the only possibilities to move a pawn
    return false;
}}

问题是: 每当我调用方法 CheckAvailableSquares() 来检查正方形的可用性时,firstMovePlayed 布尔值将设置为 true!

我真的很抱歉花了你太长时间,但我真的想不通:/

这是 ChessGui 类的其余相关代码:

 public void actionPerformed(ActionEvent e) {
    //looking for the square which fired the ActionListener:
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++){
            if(board[i][j] == e.getSource()){
                processClick(i, j);
            }
        }
    }
}
private void processClick(int i, int j){
    if(bufRow == 99 && bufCol == 99){                        //select a figure to move and wait for the next click
        if (board[i][j].isEmpty()) {                              //if there is no figure on the square that has benn clicked
            System.out.println("no Figures on this square");
            return;
        }
        else {//save the chosen square (button) in buffers so that we can move it later.
            //decide who's turn it is:
            if (board[i][j].getFigure().getColor().equals(Color.WHITE) && !whiteToPlay)
                System.out.println("It's black's turn to play!");
            else if (board[i][j].getFigure().getColor().equals(Color.BLACK) && !blackToPlay)
                System.out.println("It's white's turn to play!");
            if ((board[i][j].getFigure().getColor().equals(Color.WHITE) && whiteToPlay) || board[i][j].getFigure().getColor().equals(Color.BLACK) && blackToPlay) {
                board[i][j].setHighleightedIcon(board[i][j].getFigure().getHilightedIcon()); //change the icon of the chosen square
                bufRow = i;     //save the chosen figure to move it later
                bufCol = j;
                System.out.println("Selectd figure on Raw: " + bufRow + " Column: " + bufCol);
                switchTurns();
                CheckAvailableSquares(board[bufRow][bufCol].getFigure());
            }
        }
    }
    else
        moveFigure(i, j);
}
private void moveFigure(int wantedRow, int wantedCol) {
    if (board[bufRow][bufCol].getFigure().isValidMove(board, bufRow, bufCol, wantedRow, wantedCol)) {            // check if the move is valid for the figure
        // if (board[wantedRow][wantedCol].isEmpty()) {                                                                                 //if the wanted square is empty (not a capture)
        //move the figure to the wanted square after deleting it from the old square
        board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
        board[wantedRow][wantedCol].setSquareIcon(board[bufRow][bufCol].getFigure().getIcon());
        board[bufRow][bufCol].emptySquare();
        //update the location of the piece
        board[wantedRow][wantedCol].getFigure().setNewPos(wantedRow,wantedCol);
        System.out.println("Moved [" + bufRow + ", " + bufCol + "] To: [" + wantedRow + ", " + wantedCol + "].");
        //reset the buffers
        bufRow = 99;
        bufCol = 99;
        resetSquaresColors();
        /*}
        else if (!board[wantedRow][wantedCol].isEmpty()){                                                                                         //if it's a capture
            if(board[wantedRow][wantedCol].getFigure().getColor() != board[bufRow][bufCol].getFigure().getColor()) {      //can't capture own pieces!
                board[wantedRow][wantedCol].setFigure(board[bufRow][bufCol].getFigure());
                board[wantedRow][wantedCol].setSquareIcon(board[wantedRow][wantedCol].getFigure().getIcon());
                board[bufRow][bufCol].emptySquare();
                //update the location of the piece
                board[wantedRow][wantedCol].getFigure().setRowPos(wantedRow);
                board[wantedRow][wantedCol].getFigure().setColPos(wantedCol);
                System.out.println("Moved [" + bufRow + ", " + bufCol + "] To: [" + wantedRow + ", " + wantedCol + "].");
                //reset the buffers
                bufRow = 99;
                bufCol = 99;
            }
        }*/
    } else {      //if it's not a valid move
        board[bufRow][bufCol].setIcon(board[bufRow][bufCol].getFigure().getIcon());
        //reset the buffers
        bufRow = 99;
        bufCol = 99;
        resetSquaresColors();
        System.out.println("Not a valid move");
        switchTurns();
    }
}
private void CheckAvailableSquares(Figure figure){
    for (int row = 0; row < 8; row++){
        for (int col = 0; col < 8; col++){
            if (row == figure.getRowPos() && col == figure.getColPos()) //bypass the actual square where my piece stands
                continue;
            if (figure.isValidMove(board,figure.getRowPos(), figure.getColPos(),row,col)){
                Square sqr = board[row][col];
                if (sqr.isEmpty()) //if the square is empty
                    sqr.setAvailableIcon(new ImageIcon("Icons/available_square.png"));
                else //if there is a piece on the square
                    sqr.setAvailableIcon(sqr.getFigure().getAvailableIcon());
            }
        }
    }
}

非常感谢并原谅我糟糕的英语中的任何错误!

标签: swingoopchess

解决方案


推荐阅读