From 9063667ca548ec9644a97a0ce5c3f359d913fa49 Mon Sep 17 00:00:00 2001 From: jylkyle Date: Wed, 15 Mar 2023 22:57:06 -0700 Subject: [PATCH] Movement restriction for Knight --- src/Pawn.java | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Pawn.java b/src/Pawn.java index 513aad7..44e2068 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -12,11 +12,32 @@ public class Pawn extends Piece { public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); if (this.black) { - possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); - if (!moved && board.get(position.x, position.y + 1) == null) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); + if (board.get(position.x, position.y + 1) == null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); + if (!moved) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); + } + } + if (board.get(position.x - 1, position.y + 1) != null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1,position.y + 1))); + } + if (board.get(position.x + 1, position.y + 1) != null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1,position.y + 1))); + } + } else { - possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); - if (!moved && board.get(position.x, position.y - 1) == null) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); + if (board.get(position.x, position.y - 1) == null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); + if (!moved) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); + } + } + if (board.get(position.x - 1, position.y - 1) != null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1,position.y - 1))); + } + if (board.get(position.x + 1, position.y - 1) != null) { + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1,position.y - 1))); + } } return possibleMoves; }