我正在制作一个国际象棋程序,目前已经为棋子和骑士完成了动作,但是我在主教方面遇到了一些麻烦。我正在使用MVC方法创建国际象棋游戏,并使其创建2D BoardSquares数组,该数组存储诸如位置,一块(如果有)以及是否为空等信息。目前,我让董事会逐行从文本文件接收输入。我正在为主教运动使用的当前行是从c8到a6,因此向下和向左移动。

我的动作很简单,它从文件中获取输入,将其转换为2D数组坐标,然后将其传递到模型的move方法(与模型一起)中,该方法调用块的move方法。

// In the File I/O Class
public void passinMove(BoardModel bm) {
    generateOriginRC(input, START_LOC1, END_LOC1);
    generateDestRC(input, START_LOC2, END_LOC2);
    System.out.println("Origin C/R: "+oCol+"/"+oRow);
    System.out.println("Dest C/R: "+dCol+"/"+dRow);
    bm.movePiece(bm.getBoard(), oRow, oCol, dRow, dCol);
}

// In the BoardModel Class
public void movePiece(BoardSquare[][] board, int oRow, int oCol, int dRow, int dCol){

    board[oRow][oCol].getPiece().move(board, board[dRow][dCol]);


到目前为止,我的棋子各产生了自己的有效棋子数组。这是BoardSquares的列表,方法是通过蛮力一次将其移动一个正方形,直到碰到一块或一块木板的末端。然后,我将它与该列表所需的目标正方形进行比较。

@Override
void move(BoardSquare[][] board, BoardSquare target) {
    if (!getPossibleMove(board).contains(target)) {
        System.out
                .println("Sorry. Not a valid move, please enter a new move");

    } else {
        target.setSquare(board[col][row]);
    }
}


对于主教,我可以抓住它的位置并相应地操纵它以移动它。 (要向上移动,我要减去向下的数组,依此类推)。

但是,似乎我的逻辑在某处存在缺陷。因为当它开始向下和向右检查时,它从0/2移到1/2。然后继续直到它撞到右侧壁并直接向下进入角落。

@Override
Collection<BoardSquare> getPossibleMove(BoardSquare[][] board) {
    BoardSquare[][] copy = board;
    ArrayList<BoardSquare> validMoves = new ArrayList<BoardSquare>();

    // Checks spaces to the Up-Right of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goUp = getCol(); goUp > -1; goUp--) {
        for (int goRight = getRow(); goRight < 8; goRight++) {
            System.out.println("Doing Up-Right C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goUp+" / "+goRight);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goUp][goRight])) {
                validMoves.add(copy[goUp][goRight]);
                copy[goUp][goRight].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Up-Left of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goUp = getCol(); goUp > -1; goUp--) {
        for (int goLeft = getRow(); goLeft > -1; goLeft--) {
            System.out.println("Doing Up-Left C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goUp+" / "+goLeft);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goUp][goLeft])) {
                validMoves.add(copy[goUp][goLeft]);
                copy[goUp][goLeft].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Down-Right of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goDown = getCol(); goDown < 8; goDown++) {
        for (int goRight = getRow(); goRight < 8; goRight++) {
            System.out.println("Doing Down-Right C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goDown+" / "+goRight);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goDown][goRight])) {
                validMoves.add(copy[goDown][goRight]);
                copy[goDown][goRight].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Down-Left of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goDown = getCol(); goDown < 8; goDown++) {
        for (int goLeft = getRow(); goLeft > -1; goLeft--) {
            System.out.println("Doing Down-Left C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goDown+" / "+goLeft);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goDown][goLeft])) {
                validMoves.add(copy[goDown][goLeft]);
                copy[goDown][goLeft].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }
    return validMoves;
}


一些注意事项:
tempCol和tempRow变量仅用于获取工件所处的位置,以将其移动到“新”目标。

我仍在设法弄清楚,但是我希望有人查看我的代码,以防万一我没有注意到或忘记。任何提示或建议,不胜感激。谢谢。

最佳答案

我认为,如果moveValidator函数始终返回false直到您的文章到达墙面,您所描述的问题就会发生。

You'll have
For1 --> goDown=0
For2 --> goRight=2 --> moveValidator is false so break;
For1 --> goDown=1
For2 --> goRight=2 --> moveValidator is false so break;
...
And when goDown = 8
For2 --> goRight=2 --> moveValidator is true
For2 --> goRight=3 --> moveValidator is true
...


因此,您可能想验证我的猜测调试并尝试验证moveValidator函数(您未在代码中提供它)

09-25 20:45