Search code examples
javaiterationchess

Asking for someone to review Bishop movement logic


I'm making a chess program and currently have the movement done for the pawn and knight, however I'm having a little trouble with the bishop. I'm creating my chess game with the MVC approach and have it creating a 2D array of BoardSquares which store information such as location, a piece (if there is one), and if it's empty. For the moment I have the board take in input from a text file line-by-line. The current line I'm working on for the bishop movement is from c8 to a6, so moving Down and to the Left.

My movement is simple, where it takes the input from the file, translates it to 2D array coordinates and passes it into the model's move method (along with the model) which calls a piece's move method.

// 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]); 

As of now to move I have my pieces each generate their own array of valid moves; which is a list of BoardSquares by moving it VIA brute force one square at a time until it hits a piece or the end of the board. Then I have it compare it's desired destination square to that list.

@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]);
    }
}

For the bishop I have it grab it's position and manipulate it accordingly to move it. (To move up I subtract to go down the array, etc.).

However it seems my logic is flawed somewhere; since when it starts checking down and to the right it moves from 0/2 to 1/2. Then continues until it hits the right side wall and goes straight down into the corner.

@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;
}

Some notes: The tempCol and tempRow variable are just used to acquire the location the piece is on to move it to the "new" destination.

I'm still trying to figure it out, however I would like someone to review my code incase there's something I'm not noticing or forgetting. Any tips or suggestions are much appreciated. Thanks.


Solution

  • I think the problem you describe would happen if the moveValidator function returns always false until your piece reach the wall.

    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
    ...
    

    So you may want to validate my guess debugging and trying to verify the moveValidator function (you didn't give it in your code)