fix pgn
This commit is contained in:
@@ -25,12 +25,6 @@ public class PgnExport {
|
||||
// final Game game = new Game(new ChessBoard());
|
||||
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
// // FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
|
||||
// // // commandExecutor.addListener(foolCheckMate);
|
||||
|
||||
// // PromoteTest promoteTest = new PromoteTest(commandExecutor);
|
||||
// // commandExecutor.addListener(promoteTest);
|
||||
|
||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai1);
|
||||
|
||||
@@ -48,6 +42,8 @@ public class PgnExport {
|
||||
// commandExecutor.executeCommand(new NewGameCommand());
|
||||
// }
|
||||
|
||||
private static final PiecePgnName piecePgnName = new PiecePgnName();
|
||||
|
||||
private static Piece pieceAt(CommandExecutor commandExecutor, Coordinate coordinate) {
|
||||
GetPieceAtCommand cmd = new GetPieceAtCommand(coordinate);
|
||||
commandExecutor.executeCommand(cmd);
|
||||
@@ -60,6 +56,22 @@ public class PgnExport {
|
||||
return cmd.getMoves();
|
||||
}
|
||||
|
||||
private static String gameEnd(Game game) {
|
||||
switch (game.checkGameStatus()) {
|
||||
case Draw:
|
||||
case Pat:
|
||||
return "1/2-1/2";
|
||||
|
||||
case CheckMate:
|
||||
if (game.getPlayerTurn() == Color.White)
|
||||
return "1-0";
|
||||
return "0-1";
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String resolveAmbiguity(CommandExecutor cmdExec, Move pieceMove) {
|
||||
Piece movingPiece = pieceAt(cmdExec, pieceMove.getStart());
|
||||
|
||||
@@ -93,9 +105,90 @@ public class PgnExport {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String exportGame(Game game) {
|
||||
private static String capture(MoveCommand move, Piece movingPiece) {
|
||||
String result = "";
|
||||
if (move.getDeadPiece() != null) {
|
||||
if (movingPiece instanceof Pawn) {
|
||||
result += toString(move.getMove().getStart()).charAt(0);
|
||||
}
|
||||
result += "x";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// saveToFile(game.getMoves());
|
||||
private static String promote(PlayerCommand nextCommand) {
|
||||
if (nextCommand != null && nextCommand instanceof PromoteCommand promoteCommand) {
|
||||
String result = "=";
|
||||
result += switch (promoteCommand.getPromoteType()) {
|
||||
case Bishop -> "B";
|
||||
case Knight -> "N";
|
||||
case Queen -> "Q";
|
||||
case Rook -> "R";
|
||||
};
|
||||
return result;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String castling(CastlingCommand castlingCommand) {
|
||||
String result = "O-O";
|
||||
if (castlingCommand.isBigCastling())
|
||||
result += "-O";
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String checkCheckMate(Game game) {
|
||||
switch (game.checkGameStatus()) {
|
||||
case CheckMate:
|
||||
return "#";
|
||||
|
||||
case Check:
|
||||
return "+";
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String printMove(PlayerCommand cmd, PlayerCommand nextCommand, Game virtualGame,
|
||||
CommandExecutor executor) {
|
||||
String result = "";
|
||||
if (cmd instanceof MoveCommand move) {
|
||||
|
||||
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||
|
||||
assert movingPiece != null;
|
||||
|
||||
// piece name
|
||||
result += piecePgnName.visit(movingPiece);
|
||||
|
||||
// ambiguious start
|
||||
result += resolveAmbiguity(executor, move.getMove());
|
||||
|
||||
// capture
|
||||
result += capture(move, movingPiece);
|
||||
|
||||
// end cell
|
||||
result += toString(move.getMove().getFinish());
|
||||
|
||||
// promote
|
||||
result += promote(nextCommand);
|
||||
|
||||
} else if (cmd instanceof CastlingCommand castlingCommand) {
|
||||
result += castling(castlingCommand);
|
||||
}
|
||||
|
||||
executor.executeCommand(cmd);
|
||||
|
||||
// check or checkmate
|
||||
result += checkCheckMate(virtualGame);
|
||||
|
||||
result += " ";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String exportGame(Game game) {
|
||||
|
||||
ChessBoard board = new ChessBoard();
|
||||
Game virtualGame = new Game(board);
|
||||
@@ -104,7 +197,6 @@ public class PgnExport {
|
||||
executor.executeCommand(new NewGameCommand());
|
||||
|
||||
List<PlayerCommand> commands = game.getMoves();
|
||||
PiecePgnName piecePgnName = new PiecePgnName();
|
||||
String result = "";
|
||||
|
||||
int tour = 1;
|
||||
@@ -115,66 +207,13 @@ public class PgnExport {
|
||||
if (i != commands.size() - 1) {
|
||||
nextCommand = commands.get(i + 1);
|
||||
}
|
||||
if (cmd instanceof MoveCommand move) {
|
||||
if (virtualGame.getPlayerTurn() == Color.White) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||
|
||||
assert movingPiece != null;
|
||||
|
||||
// piece name
|
||||
result += piecePgnName.visit(movingPiece);
|
||||
|
||||
// capture
|
||||
if (move.getDeadPiece() != null) {
|
||||
if (movingPiece instanceof Pawn) {
|
||||
result += toString(move.getMove().getStart()).charAt(0);
|
||||
}
|
||||
result += "x";
|
||||
}
|
||||
|
||||
// ambiguious start
|
||||
result += resolveAmbiguity(executor, move.getMove());
|
||||
|
||||
// end cell
|
||||
result += toString(move.getMove().getFinish());
|
||||
|
||||
// promote
|
||||
if (nextCommand != null && nextCommand instanceof PromoteCommand promoteCommand) {
|
||||
result += "=";
|
||||
result += switch (promoteCommand.getPromoteType()) {
|
||||
case Bishop -> "B";
|
||||
case Knight -> "N";
|
||||
case Queen -> "Q";
|
||||
case Rook -> "R";
|
||||
};
|
||||
}
|
||||
} else if (cmd instanceof CastlingCommand castlingCommand) {
|
||||
result += "O-O";
|
||||
if (castlingCommand.isBigCastling())
|
||||
result += "-O";
|
||||
if (virtualGame.getPlayerTurn() == Color.White) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
|
||||
executor.executeCommand(cmd);
|
||||
|
||||
// check or checkmate
|
||||
switch (virtualGame.checkGameStatus()) {
|
||||
case CheckMate:
|
||||
result += "#";
|
||||
break;
|
||||
|
||||
case Check:
|
||||
result += "+";
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
result += " ";
|
||||
result += printMove(cmd, nextCommand, virtualGame, executor);
|
||||
}
|
||||
return result;
|
||||
return result + " " + gameEnd(virtualGame);
|
||||
}
|
||||
|
||||
public static String toString(Coordinate coordinate) {
|
||||
|
||||
Reference in New Issue
Block a user