50 lines
1,016 B
Java
50 lines
1,016 B
Java
/**
|
|
* Piece position class
|
|
*/
|
|
public class PiecePosition {
|
|
|
|
/**
|
|
* Piece's X location
|
|
*/
|
|
public int x;
|
|
|
|
/**
|
|
* Piece's Y location
|
|
*/
|
|
public int y;
|
|
|
|
/**
|
|
* Conversion to the string
|
|
* @return string representation
|
|
*/
|
|
public String toString() {
|
|
return String.valueOf((char) (x+97)) + (8-y);
|
|
}
|
|
|
|
/**
|
|
* Parse PiecePosition from String
|
|
* @param position parsed String
|
|
* @return PiecePosition or null
|
|
*/
|
|
public static PiecePosition fromString(String position) {
|
|
try {
|
|
int x = position.charAt(0) - 97;
|
|
int y = 8 - Integer.parseInt(String.valueOf(position.charAt(1)));
|
|
return new PiecePosition(x, y);
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PiecePosition constructor
|
|
* @param x piece's X location
|
|
* @param y piece's Y location
|
|
*/
|
|
public PiecePosition(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
}
|