86 lines
1.8 KiB
Java
86 lines
1.8 KiB
Java
package dev.kske.chess.board;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>Position.java</strong><br>
|
|
* Created: <strong>02.07.2019</strong><br>
|
|
*
|
|
* @since Chess v0.1-alpha
|
|
* @author Kai S. K. Engelbart
|
|
*/
|
|
public class Position {
|
|
|
|
/**
|
|
* The horizontal component of this position.
|
|
*/
|
|
public final int x;
|
|
|
|
/**
|
|
* The vertical component of this position.
|
|
*/
|
|
public final int y;
|
|
|
|
/**
|
|
* Initializes a position.
|
|
*
|
|
* @param x the horizontal component of this position
|
|
* @param y the vertical component of this position
|
|
*/
|
|
public Position(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
/**
|
|
* Constructs a position from Long Algebraic Notation (LAN)
|
|
*
|
|
* @param pos the LAN string to construct a position from
|
|
* @return the position constructed from LAN
|
|
*/
|
|
public static Position fromLAN(String pos) {
|
|
return new Position(
|
|
pos.charAt(0) - 97,
|
|
8 - Character.getNumericValue(pos.charAt(1))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Converts this position to Long Algebraic Notation (LAN)
|
|
*
|
|
* @return a LAN string representing this position
|
|
*/
|
|
public String toLAN() {
|
|
return String.valueOf((char) (x + 97)) + String.valueOf(8 - y);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("[%d, %d]", x, y);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
final int prime = 31;
|
|
int result = 1;
|
|
result = prime * result + x;
|
|
result = prime * result + y;
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
Position other = (Position) obj;
|
|
if (x != other.x)
|
|
return false;
|
|
if (y != other.y)
|
|
return false;
|
|
return true;
|
|
}
|
|
}
|