This commit is contained in:
lrave 2025-05-22 18:49:45 +02:00
parent 2db30292f2
commit f6d3140ac1
1 changed files with 9 additions and 9 deletions

View File

@ -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<int[]> getValidMoves(Board board) {
ArrayList<int[]> moves = new ArrayList<>();
public ArrayList<int[]> getValidMoves(Board board) { //List all valid moves to highlight them and enable play
ArrayList<int[]> 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;