This commit is contained in:
@@ -50,6 +50,13 @@ public class PgnImport {
|
||||
return getMoves(parts[parts.length - 1]);
|
||||
}
|
||||
|
||||
private static final int COORDINATE_ANY = -1;
|
||||
|
||||
/**
|
||||
* Parse the moves from a PGN string.
|
||||
* @param unparsedMoves
|
||||
* @return
|
||||
*/
|
||||
private static List<PlayerCommand> getMoves(String unparsedMoves) {
|
||||
String[] moves = unparsedMoves.replaceAll("\\{.*?\\}", "") // Remove comments
|
||||
.replaceAll("\\n", " ") // Remove new lines
|
||||
@@ -79,6 +86,12 @@ public class PgnImport {
|
||||
return instructions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a move from the PGN format and plays it in the given game.
|
||||
* @param move
|
||||
* @param game
|
||||
* @return
|
||||
*/
|
||||
private static List<PlayerCommand> parseMove(String move, Game game) {
|
||||
if (move.equals("O-O-O"))
|
||||
return Arrays.asList(new CastlingCommand(true));
|
||||
@@ -104,7 +117,7 @@ public class PgnImport {
|
||||
|
||||
assert move.length() == 3 || move.length() == 2;
|
||||
|
||||
Coordinate ambiguity = new Coordinate(-1, -1);
|
||||
Coordinate ambiguity = new Coordinate(COORDINATE_ANY, COORDINATE_ANY);
|
||||
|
||||
// ambiguity
|
||||
if (move.length() == 3) {
|
||||
@@ -124,8 +137,17 @@ public class PgnImport {
|
||||
return cmds;
|
||||
}
|
||||
|
||||
private static Coordinate getStartCoord(Coordinate dest, Class<? extends Piece> pieceType, Coordinate ambiguity,
|
||||
Game game) {
|
||||
/**
|
||||
* Get the start coordinate of a piece.
|
||||
* @param dest the end position of the moving piece
|
||||
* @param pieceType the type of the piece
|
||||
* @param ambiguity coordinates of the moving piece, indicated with the constant COORDINATE_ANY.
|
||||
* @param game the game
|
||||
* @see COORDINATE_ANY
|
||||
* @see getAmbiguityPattern
|
||||
* @return the start coordinate of the piece
|
||||
*/
|
||||
private static Coordinate getStartCoord(Coordinate dest, Class<? extends Piece> pieceType, Coordinate ambiguity, Game game) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
List<Coordinate> starts = board.getAllowedStarts(dest, game.getPlayerTurn());
|
||||
@@ -152,10 +174,10 @@ public class PgnImport {
|
||||
}
|
||||
|
||||
private static boolean coordPatternMatch(Coordinate coord, Coordinate pattern) {
|
||||
if (pattern.getX() != -1 && coord.getX() != pattern.getX())
|
||||
if (pattern.getX() != COORDINATE_ANY && coord.getX() != pattern.getX())
|
||||
return false;
|
||||
|
||||
if (pattern.getY() != -1 && coord.getY() != pattern.getY())
|
||||
if (pattern.getY() != COORDINATE_ANY && coord.getY() != pattern.getY())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -163,8 +185,8 @@ public class PgnImport {
|
||||
|
||||
private static Coordinate getAmbiguityPattern(char amb) {
|
||||
if (Character.isDigit(amb))
|
||||
return new Coordinate(-1, getYCoord(amb));
|
||||
return new Coordinate(getXCoord(amb), -1);
|
||||
return new Coordinate(COORDINATE_ANY, getYCoord(amb));
|
||||
return new Coordinate(getXCoord(amb), COORDINATE_ANY);
|
||||
}
|
||||
|
||||
private static Coordinate stringToCoordinate(String coordinates) {
|
||||
|
||||
Reference in New Issue
Block a user