Note that there are some explanatory texts on larger screens.

plurals
  1. POAsking for someone to review Bishop movement logic
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <pre><code>// 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]); </code></pre> <p>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.</p> <pre><code>@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]); } } </code></pre> <p>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.). </p> <p>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.</p> <pre><code>@Override Collection&lt;BoardSquare&gt; getPossibleMove(BoardSquare[][] board) { BoardSquare[][] copy = board; ArrayList&lt;BoardSquare&gt; validMoves = new ArrayList&lt;BoardSquare&gt;(); // 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 &gt; -1; goUp--) { for (int goRight = getRow(); goRight &lt; 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 &gt; -1; goUp--) { for (int goLeft = getRow(); goLeft &gt; -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 &lt; 8; goDown++) { for (int goRight = getRow(); goRight &lt; 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 &lt; 8; goDown++) { for (int goLeft = getRow(); goLeft &gt; -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; } </code></pre> <p>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. </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload