This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/main/java/dev/kske/chess/board/Pawn.java

132 lines
3.3 KiB
Java

package dev.kske.chess.board;
import java.util.ArrayList;
import java.util.List;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Pawn.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
*
* @since Chess v0.1-alpha
* @author Kai S. K. Engelbart
*/
public class Pawn extends Piece {
/**
* Creates pawn {@link Piece}.
*
* @param color the color of this pawn
* @param board the board on which this pawn will be placed
*/
public Pawn(Color color, Board board) {
super(color, board);
}
@Override
public boolean isValidMove(Move move) {
boolean step = move.isVertical() && move.getyDist() == 1;
boolean doubleStep = move.isVertical() && move.getyDist() == 2;
boolean strafe = move.isDiagonal() && move.getxDist() == 1;
boolean enPassant
= strafe && move.getDest().equals(board.getLog().getEnPassant());
if (getColor() == Color.WHITE)
doubleStep &= move.getPos().y == 6;
else
doubleStep &= move.getPos().y == 1;
return enPassant || step ^ doubleStep ^ strafe
&& move.getySign() == (getColor() == Color.WHITE ? -1 : 1)
&& isFreePath(move);
}
@Override
protected boolean isFreePath(Move move) {
// Two steps forward
if (move.getyDist() == 2)
return board.getBoardArr()[move.getPos().x][move.getDest().y
- move.getySign()] == null && board.getDest(move) == null;
// One step forward
else
if (move.getxDist() == 0)
return board.getDest(move) == null;
// Capture move
else
return board.getDest(move) != null
&& board.getDest(move).getColor() != getColor();
}
@Override
protected List<Move> getPseudolegalMoves(Position pos) {
List<Move> moves = new ArrayList<>();
int sign = getColor() == Color.WHITE ? -1 : 1;
boolean pawnPromotion
= sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1;
// Strafe left
if (pos.x > 0)
addMoveIfValid(
moves,
pos,
new Position(pos.x - 1, pos.y + sign),
pawnPromotion
);
// Strafe right
if (pos.x < 7)
addMoveIfValid(
moves,
pos,
new Position(pos.x + 1, pos.y + sign),
pawnPromotion
);
// Step forward
if (sign == 1 && pos.y < 7 || sign == -1 && pos.y > 0)
addMoveIfValid(
moves,
pos,
new Position(pos.x, pos.y + sign),
pawnPromotion
);
// Double step forward
if (sign == 1 && pos.y == 1 || sign == -1 && pos.y == 6)
addMoveIfValid(
moves,
pos,
new Position(pos.x, pos.y + 2 * sign),
pawnPromotion
);
// Add en passant move if necessary
if (board.getLog().getEnPassant() != null) {
Move move = new EnPassant(pos, board.getLog().getEnPassant());
if (move.isDiagonal() && move.getxDist() == 1)
moves.add(move);
}
return moves;
}
private void addMoveIfValid(
List<Move> moves, Position pos, Position dest, boolean pawnPromotion
) {
Move move = new Move(pos, dest);
if (isFreePath(move))
if (pawnPromotion)
try {
moves.add(new PawnPromotion(pos, dest, Queen.class));
moves.add(new PawnPromotion(pos, dest, Rook.class));
moves.add(new PawnPromotion(pos, dest, Knight.class));
moves.add(new PawnPromotion(pos, dest, Bishop.class));
} catch (Exception e) {
e.printStackTrace();
}
else
moves.add(move);
}
@Override
public int getValue() { return 10; }
}