From ce61482b9e22443b829e936a876c2a3c48f695ed Mon Sep 17 00:00:00 2001 From: CT Date: Thu, 23 Apr 2026 16:18:28 -0500 Subject: [PATCH] refactored pawn movement + implemented knight movement --- Board.java | 6 +++++- Knight.java | 15 +++++++++++---- Pawn.java | 27 ++++++++++++++------------- Piece.java | 4 ++++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Board.java b/Board.java index 3ca5909..b95f324 100644 --- a/Board.java +++ b/Board.java @@ -26,6 +26,10 @@ public class Board { } public boolean isOpen(Position pos) { - return this.board[pos.x][pos.y] == null; + return this.getPiece(pos) == null; + } + + public Piece getPiece(Position pos) { + return this.board[pos.x][pos.y]; } } diff --git a/Knight.java b/Knight.java index 95c7fb0..a691ee0 100644 --- a/Knight.java +++ b/Knight.java @@ -12,10 +12,17 @@ public class Knight extends Piece { public ArrayList getLegalMoves(Board board) { ArrayList positions = new ArrayList(); - for (int i : xDir) { - for (int j : yDir) { - Position test = new Position(pos.x + i, pos.y + j); - if (!Board.inBounds(test)) continue; + for (int i = 0; i < xDir.length; i++) { + int xPlus = xDir[i]; + int yPlus = yDir[i]; + Position test = new Position(pos.x + xPlus, pos.y + yPlus); + if (!Board.inBounds(test)) continue; + if (board.isOpen(test)) { + positions.add(test); + continue; + } + if (!board.getPiece(test).colorMatches(this)) { + positions.add(test); } } diff --git a/Pawn.java b/Pawn.java index 6b35730..43ca65a 100644 --- a/Pawn.java +++ b/Pawn.java @@ -13,27 +13,28 @@ public class Pawn extends Piece { public ArrayList getLegalMoves(Board board) { ArrayList positions = new ArrayList(); + // diagonal moves (captures) - if (Board.inBounds(new Position(pos.x + 1, pos.y + colorDir)) - && !board.isOpen(new Position(pos.x + 1, pos.y + colorDir)) - && !board.board[pos.x + 1][pos.y + colorDir].color.equals(this.color)) { - positions.add(new Position(pos.x + 1, pos.y + colorDir)); + Position test = new Position(pos.x + 1, pos.y + colorDir); + if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) { + positions.add(test); } - if (Board.inBounds(new Position(pos.x - 1, pos.y + colorDir)) - && !board.isOpen(new Position(pos.x - 1, pos.y + colorDir)) - && !board.board[pos.x - 1][pos.y + colorDir].color.equals(this.color)) { - positions.add(new Position(pos.x - 1, pos.y + colorDir)); + + test = new Position(pos.x - 1, pos.y + colorDir); + if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) { + positions.add(test); } // one square in front: if blocked return early - if (Board.inBounds(new Position(pos.x, pos.y + colorDir)) - && board.isOpen(new Position(pos.x, pos.y + colorDir))) { - positions.add(new Position(pos.x, pos.y + colorDir)); + test = new Position(pos.x, pos.y + colorDir); + if (Board.inBounds(test) && board.isOpen(test)) { + positions.add(test); } else return positions; // two squares in front - if (!hasMoved && board.isOpen(new Position(pos.x, pos.y + 2 * colorDir))) { - positions.add(new Position(pos.x, pos.y + 2 * colorDir)); + test = new Position(pos.x, pos.y + 2 * colorDir); + if (!hasMoved && board.isOpen(test)) { + positions.add(test); } return positions; } diff --git a/Piece.java b/Piece.java index 4acc428..4465faf 100644 --- a/Piece.java +++ b/Piece.java @@ -21,4 +21,8 @@ public class Piece { System.out.println("parent method not overriden"); return null; } + + public boolean colorMatches(Piece p) { + return this.color.equals(p.color); + } }