2019-07-06 18:05:00 +02:00
|
|
|
package dev.kske.chess.board;
|
2019-07-01 19:37:15 +02:00
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-07-01 19:37:15 +02:00
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>Pawn.java</strong><br>
|
|
|
|
* Created: <strong>01.07.2019</strong><br>
|
2019-11-24 21:31:32 +01:00
|
|
|
*
|
2019-10-26 07:55:21 +02:00
|
|
|
* @since Chess v0.1-alpha
|
|
|
|
* @author Kai S. K. Engelbart
|
2019-07-01 19:37:15 +02:00
|
|
|
*/
|
|
|
|
public class Pawn extends Piece {
|
|
|
|
|
2020-01-19 22:12:33 +01:00
|
|
|
/**
|
|
|
|
* Creates pawn {@link Piece}.
|
|
|
|
*
|
|
|
|
* @param color the color of this pawn
|
|
|
|
* @param board the board on which this pawn will be placed
|
|
|
|
*/
|
2020-05-19 16:50:12 +02:00
|
|
|
public Pawn(Color color, Board board) {
|
|
|
|
super(color, board);
|
|
|
|
}
|
2019-07-02 13:49:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValidMove(Move move) {
|
2020-05-19 16:50:12 +02:00
|
|
|
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;
|
2019-07-10 18:54:53 +02:00
|
|
|
|
2020-05-19 16:50:12 +02:00
|
|
|
return enPassant || step ^ doubleStep ^ strafe
|
|
|
|
&& move.getySign() == (getColor() == Color.WHITE ? -1 : 1)
|
|
|
|
&& isFreePath(move);
|
2019-07-01 19:37:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:46:30 +02:00
|
|
|
@Override
|
2019-07-02 13:49:36 +02:00
|
|
|
protected boolean isFreePath(Move move) {
|
|
|
|
// Two steps forward
|
2019-10-31 19:09:43 +01:00
|
|
|
if (move.getyDist() == 2)
|
2020-05-19 16:50:12 +02:00
|
|
|
return board.getBoardArr()[move.getPos().x][move.getDest().y
|
|
|
|
- move.getySign()] == null && board.getDest(move) == null;
|
2019-07-02 13:49:36 +02:00
|
|
|
// One step forward
|
2020-05-19 16:50:12 +02:00
|
|
|
else
|
|
|
|
if (move.getxDist() == 0)
|
|
|
|
return board.getDest(move) == null;
|
|
|
|
// Capture move
|
|
|
|
else
|
|
|
|
return board.getDest(move) != null
|
|
|
|
&& board.getDest(move).getColor() != getColor();
|
2019-07-01 21:46:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
@Override
|
|
|
|
protected List<Move> getPseudolegalMoves(Position pos) {
|
2020-05-19 16:50:12 +02:00
|
|
|
List<Move> moves = new ArrayList<>();
|
|
|
|
int sign = getColor() == Color.WHITE ? -1 : 1;
|
|
|
|
boolean pawnPromotion
|
|
|
|
= sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1;
|
2019-07-05 14:14:48 +02:00
|
|
|
|
|
|
|
// Strafe left
|
2020-05-19 16:50:12 +02:00
|
|
|
if (pos.x > 0)
|
|
|
|
addMoveIfValid(
|
|
|
|
moves,
|
|
|
|
pos,
|
|
|
|
new Position(pos.x - 1, pos.y + sign),
|
|
|
|
pawnPromotion
|
|
|
|
);
|
2019-07-05 14:14:48 +02:00
|
|
|
|
|
|
|
// Strafe right
|
2020-05-19 16:50:12 +02:00
|
|
|
if (pos.x < 7)
|
|
|
|
addMoveIfValid(
|
|
|
|
moves,
|
|
|
|
pos,
|
|
|
|
new Position(pos.x + 1, pos.y + sign),
|
|
|
|
pawnPromotion
|
|
|
|
);
|
2019-07-05 14:14:48 +02:00
|
|
|
|
|
|
|
// Step forward
|
2020-05-19 16:50:12 +02:00
|
|
|
if (sign == 1 && pos.y < 7 || sign == -1 && pos.y > 0)
|
|
|
|
addMoveIfValid(
|
|
|
|
moves,
|
|
|
|
pos,
|
|
|
|
new Position(pos.x, pos.y + sign),
|
|
|
|
pawnPromotion
|
|
|
|
);
|
2019-07-05 14:14:48 +02:00
|
|
|
|
|
|
|
// Double step forward
|
2020-05-19 16:50:12 +02:00
|
|
|
if (sign == 1 && pos.y == 1 || sign == -1 && pos.y == 6)
|
|
|
|
addMoveIfValid(
|
|
|
|
moves,
|
|
|
|
pos,
|
|
|
|
new Position(pos.x, pos.y + 2 * sign),
|
|
|
|
pawnPromotion
|
|
|
|
);
|
2019-07-10 18:54:53 +02:00
|
|
|
|
2019-07-28 13:51:10 +02:00
|
|
|
// Add en passant move if necessary
|
2019-08-12 06:44:55 +02:00
|
|
|
if (board.getLog().getEnPassant() != null) {
|
2019-11-03 15:46:08 +01:00
|
|
|
Move move = new EnPassant(pos, board.getLog().getEnPassant());
|
2020-05-19 16:50:12 +02:00
|
|
|
if (move.isDiagonal() && move.getxDist() == 1)
|
|
|
|
moves.add(move);
|
2019-07-28 13:51:10 +02:00
|
|
|
}
|
2019-07-05 14:14:48 +02:00
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:50:12 +02:00
|
|
|
private void addMoveIfValid(
|
|
|
|
List<Move> moves, Position pos, Position dest, boolean pawnPromotion
|
|
|
|
) {
|
2019-11-03 15:46:08 +01:00
|
|
|
Move move = new Move(pos, dest);
|
2020-05-19 16:50:12 +02:00
|
|
|
if (isFreePath(move))
|
|
|
|
if (pawnPromotion)
|
2019-11-03 15:46:08 +01:00
|
|
|
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));
|
2019-11-24 21:31:32 +01:00
|
|
|
} catch (Exception e) {
|
2019-11-03 15:46:08 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-05-19 16:50:12 +02:00
|
|
|
else
|
|
|
|
moves.add(move);
|
2019-11-03 15:46:08 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 19:37:15 +02:00
|
|
|
@Override
|
2019-11-04 05:51:11 +01:00
|
|
|
public int getValue() { return 10; }
|
2019-07-01 19:37:15 +02:00
|
|
|
}
|