java - 请人审查 Bishop 的 Action 逻辑

标签 java iteration chess

我正在制作一个国际象棋程序,目前已经为棋子和马完成了移动,但是我在主教方面遇到了一些麻烦。我正在使用 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 列表,通过暴力方式一次移动一个方 block ,直到它碰到一 block 或棋盘的末端。然后我让它将所需的目的地方 block 与该列表进行比较。

@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 变量仅用于获取片段所在的位置,以将其移动到"new"目的地。

我仍在试图弄清楚,但是我希望有人检查我的代码,以防有一些我没有注意到或忘记的事情。非常感谢任何提示或建议。谢谢。

最佳答案

我认为如果 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 函数(你没有在代码中给出它)

关于java - 请人审查 Bishop 的 Action 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9631261/

相关文章:

java - 为什么 FileOutputStream 写入磁盘上的不同位置?

java - 如何减慢行星在轨道上的移动速度?

python - 如何从 bash 中的 python 脚本的一组不同输入文件中输出多个文件

swift - 如何快速添加时间延迟

javascript - 在 Javascript 中拖放 unicode 的问题

java - 如何避免解析奇怪的字符

java - 如何将代码从 gradle 任务配置步骤移至任务执行

iteration - 步进 D 中的范围

php - 图像未出现在棋盘中 - PHP

iphone - 如何将国际象棋 AI 移植到 iPhone