要求有人审查Bishop运动逻辑

时间:2012-03-09 08:57:24

标签: java iteration chess

我正在制作国际象棋程序,目前已为棋子和骑士完成了运动,但是我对主教有点麻烦。我正在使用MVC方法创建我的国际象棋游戏并创建一个BoardSquares的2D数组,它存储位置,一块(如果有的话)以及它是否为空的信息。目前我让电路板逐行接收来自文本文件的输入。我正在为主教运动工作的当前线是从c8到a6,所以向下移动到左边。

我的动作很简单,它从文件中获取输入,将其转换为2D数组坐标,并将其传递到模型的移动方法(以及模型)中,该方法调用片段的移动方法。

// 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的列表,通过一次一个方向移动它VIA蛮力,直到它击中一块或一块板的末端。然后我将它与该列表的目标广场进行比较。

@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变量仅用于获取片段所在的位置以将其移动到“新”目标。

我仍然想弄明白,但我希望有人能够审查我的代码,因为有些东西我没有注意到或遗忘。任何提示或建议都非常感谢。感谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我认为单个计数器的逻辑可能更简单。另外,既然你正在爆发,你可以通过数到8来进一步简化逻辑。当我阅读副本[goUp] [goRight]时,我想知道你是否意味着复制[goRight] [goUp],因为通常我设置我的游戏板变量,如x,y和row,col。我是新来的,如果我误解了任何或所有问题,我会提前道歉。祝你的国际象棋比赛好运!

    // 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 go = 1; go < 8; go++){
        int testRow = getRow();
        int testCol = getCol();
        testRow-=go; //looking upwards
        testCol+=go; //looking rightwards