package dev.kske.chess.board; /** * Project: Chess
* File: Move.java
* Created: 02.07.2019
* Author: Kai S. K. Engelbart */ 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 String toSAN() { return pos.toSAN() + dest.toSAN(); } 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 } }