CyB3RC0nN0R
3dfc16943f
+ MoveProcessor and ProcessingResult classes for handling multithreaded move calculation + Separate package for AIPlayer and its components
77 lines
2.3 KiB
Java
77 lines
2.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>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class Pawn extends Piece {
|
|
|
|
public Pawn(Color color, Board board) {
|
|
super(color, board);
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidMove(Move move) {
|
|
// TODO: en passant, pawn promotion
|
|
boolean step = move.isVertical() && move.yDist == 1;
|
|
boolean doubleStep = move.isVertical() && move.yDist == 2;
|
|
boolean strafe = move.isDiagonal() && move.xDist == 1;
|
|
if (getColor() == Color.WHITE) doubleStep &= move.pos.y == 6;
|
|
else doubleStep &= move.pos.y == 1;
|
|
return (step ^ doubleStep ^ strafe) && move.ySign == (getColor() == Color.WHITE ? -1 : 1) && isFreePath(move);
|
|
}
|
|
|
|
@Override
|
|
protected boolean isFreePath(Move move) {
|
|
// Two steps forward
|
|
if (move.yDist == 2)
|
|
return board.getBoardArr()[move.pos.x][move.dest.y - move.ySign] == null && board.getDest(move) == null;
|
|
// One step forward
|
|
else if (move.xDist == 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;
|
|
|
|
if (sign == -1 && pos.y == 1 || sign == 1 && pos.y == 7) return moves;
|
|
|
|
// Strafe left
|
|
if (pos.x > 0) {
|
|
Move move = new Move(pos, new Position(pos.x - 1, pos.y + sign));
|
|
if (isFreePath(move)) moves.add(move);
|
|
}
|
|
|
|
// Strafe right
|
|
if (pos.x < 7) {
|
|
Move move = new Move(pos, new Position(pos.x + 1, pos.y + sign));
|
|
if (isFreePath(move)) moves.add(move);
|
|
}
|
|
|
|
// Step forward
|
|
if (sign == 1 && pos.y < 7 || sign == -1 && pos.y > 0) {
|
|
Move move = new Move(pos, new Position(pos.x, pos.y + sign));
|
|
if (isFreePath(move)) moves.add(move);
|
|
}
|
|
|
|
// Double step forward
|
|
if (sign == 1 && pos.y == 1 || sign == -1 && pos.y == 6) {
|
|
Move move = new Move(pos, new Position(pos.x, pos.y + 2 * sign));
|
|
if (isFreePath(move)) moves.add(move);
|
|
}
|
|
return moves;
|
|
}
|
|
|
|
@Override
|
|
public Type getType() { return Type.PAWN; }
|
|
}
|