Add pawn Attack

main
Brett 2022-12-19 23:03:15 -05:00
parent b442d9227d
commit ab5f369cf0
1 changed files with 10 additions and 0 deletions

View File

@ -25,10 +25,14 @@ public class Pawn extends ChessPiece {
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
if (isWhite) { if (isWhite) {
moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y + 1)), 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()) if (isFirstMove())
moves.add(b.checkIfMoveValid(new Move(x, y + 2), isWhite)); moves.add(b.checkIfMoveValid(new Move(x, y + 2), isWhite));
} else { } else {
moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y - 1)), 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()) if (isFirstMove())
moves.add(b.checkIfMoveValid(new Move(x, y - 2), isWhite)); moves.add(b.checkIfMoveValid(new Move(x, y - 2), isWhite));
} }
@ -54,6 +58,12 @@ public class Pawn extends ChessPiece {
return prune(moves); 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){ private Move pawnForwardMoveValid(Move m){
// basically prevent a pawn from moving into the enemy head on. // basically prevent a pawn from moving into the enemy head on.
if (b.get(m) != null) if (b.get(m) != null)