diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 5e45e49..2b070d7 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -7,7 +7,7 @@ public class Piece { private int y; private boolean pieceColor; private PieceType type; - private boolean hasMoved = false; + private boolean hasMoved = false; // useful for castling public boolean getHasMoved() { return hasMoved; @@ -17,7 +17,7 @@ public class Piece { this.hasMoved = moved; } - public Piece(int x,int y, PieceType type,boolean pieceColor) { + public Piece(int x,int y, PieceType type,boolean pieceColor) { //Piece constructor with all attributes this.x = x; this.y = y; this.pieceColor = pieceColor; @@ -49,12 +49,12 @@ public class Piece { return pieceColor; } - public ArrayList getValidMoves(Board board) { - ArrayList moves = new ArrayList<>(); + public ArrayList getValidMoves(Board board) { //List all valid moves to highlight them and enable play + ArrayList moves = new ArrayList<>(); int x = this.getX(); int y = this.getY(); - switch (type) { + switch (type) { //Switch just for simplicity but could be replaced by ifs. case Pawn: int direction = isWhite() ? -1 : 1; int nextY = y + direction; @@ -149,7 +149,7 @@ public class Piece { case Knight: int[][] jumps = { - {1, 2}, {2, 1}, {-1, 2}, {-2, 1}, + {1, 2}, {2, 1}, {-1, 2}, {-2, 1}, // L moves {-1, -2}, {-2, -1}, {1, -2}, {2, -1} }; for (int[] j : jumps) { @@ -171,9 +171,9 @@ public class Piece { private boolean canCastle(Board board, int kingX, int y, int rookX) { if (rookX == 7) { // identify if castle on king-side - if (board.getPiece(rookX, y) != null && board.getPiece(rookX, y).getType() == PieceType.Rook && !board.getPiece(rookX, y).getHasMoved()) { - if (board.getPiece(5, y) == null && board.getPiece(6, y) == null) { - return true; + if (board.getPiece(rookX, y) != null && board.getPiece(rookX, y).getType() == PieceType.Rook && !board.getPiece(rookX, y).getHasMoved()) { // Checks all necessary conditions for castling on rook + if (board.getPiece(5, y) == null && board.getPiece(6, y) == null) { // check if nothing between king and rook + return true; // include castle in valid moves } else { return false;