From ab5f369cf09c2139746192ecaf313375ca389d74 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 19 Dec 2022 23:03:15 -0500 Subject: [PATCH] Add pawn Attack --- src/chess/Pawn.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/chess/Pawn.java b/src/chess/Pawn.java index 4c210f1..0509c71 100644 --- a/src/chess/Pawn.java +++ b/src/chess/Pawn.java @@ -25,10 +25,14 @@ public class Pawn extends ChessPiece { ArrayList moves = new ArrayList(); if (isWhite) { moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y + 1)), isWhite)); + moves.add(pawnAttack(new Move(x + 1, y + 1))); + moves.add(pawnAttack(new Move(x - 1, y + 1))); if (isFirstMove()) moves.add(b.checkIfMoveValid(new Move(x, y + 2), isWhite)); } else { moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y - 1)), isWhite)); + moves.add(pawnAttack(new Move(x + 1, y - 1))); + moves.add(pawnAttack(new Move(x - 1, y - 1))); if (isFirstMove()) moves.add(b.checkIfMoveValid(new Move(x, y - 2), isWhite)); } @@ -54,6 +58,12 @@ public class Pawn extends ChessPiece { return prune(moves); } + private Move pawnAttack(Move m){ + if (b.get(m) != null && b.get(m).isWhite != isWhite) + return m; + return null; + } + private Move pawnForwardMoveValid(Move m){ // basically prevent a pawn from moving into the enemy head on. if (b.get(m) != null)