53 lines
1.3 KiB
Java
53 lines
1.3 KiB
Java
package dev.kske.chess.board;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>Move.java</strong><br>
|
|
* Created: <strong>02.07.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class Move {
|
|
|
|
public final Position pos, dest;
|
|
public final int xDist, yDist, xSign, ySign;
|
|
public Type type;
|
|
|
|
public Move(Position pos, Position dest, Type type) {
|
|
this.pos = pos;
|
|
this.dest = dest;
|
|
this.type = type;
|
|
xDist = Math.abs(dest.x - pos.x);
|
|
yDist = Math.abs(dest.y - pos.y);
|
|
xSign = (int) Math.signum(dest.x - pos.x);
|
|
ySign = (int) Math.signum(dest.y - pos.y);
|
|
}
|
|
|
|
public Move(Position pos, Position dest) {
|
|
this(pos, dest, Type.NORMAL);
|
|
}
|
|
|
|
public Move(int xPos, int yPos, int xDest, int yDest) {
|
|
this(new Position(xPos, yPos), new Position(xDest, yDest));
|
|
}
|
|
|
|
public static Move fromSAN(String move) {
|
|
return new Move(Position.fromSAN(move.substring(0, 2)),
|
|
Position.fromSAN(move.substring(2)));
|
|
}
|
|
|
|
public boolean isHorizontal() { return yDist == 0; }
|
|
|
|
public boolean isVertical() { return xDist == 0; }
|
|
|
|
public boolean isDiagonal() { return xDist == yDist; }
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("%s -> %s", pos, dest);
|
|
}
|
|
|
|
public static enum Type {
|
|
NORMAL, PAWN_PROMOTION, CASTLING, EN_PASSANT, UNKNOWN
|
|
}
|
|
}
|