working pgn

This commit is contained in:
2025-04-15 21:36:05 +02:00
parent edf95a1691
commit 577ef0545f

View File

@@ -4,45 +4,99 @@ import java.util.List;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.PlayerCommand; import chess.controller.PlayerCommand;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand; import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.event.EmptyGameDispatcher; import chess.controller.event.EmptyGameDispatcher;
import chess.controller.event.GameAdaptator;
import chess.model.ChessBoard; import chess.model.ChessBoard;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
import chess.model.Game; import chess.model.Game;
import chess.model.Move;
import chess.model.Piece; import chess.model.Piece;
import chess.simulator.FoolCheckMate; import chess.model.pieces.Pawn;
public class PgnExport { public class PgnExport {
public static void main(String[] args) { // public static void main(String[] args) {
final Game game = new Game(new ChessBoard()); // final Game game = new Game(new ChessBoard());
final CommandExecutor commandExecutor = new CommandExecutor(game); // final CommandExecutor commandExecutor = new CommandExecutor(game);
FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor); // // FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
commandExecutor.addListener(foolCheckMate); // // // commandExecutor.addListener(foolCheckMate);
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White); // // PromoteTest promoteTest = new PromoteTest(commandExecutor);
// commandExecutor.addListener(ai1); // // commandExecutor.addListener(promoteTest);
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black); // DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
// commandExecutor.addListener(ai2); // commandExecutor.addListener(ai1);
commandExecutor.addListener(new GameAdaptator() { // DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
@Override // commandExecutor.addListener(ai2);
public void onGameEnd() {
System.out.println(exportGame(game));
commandExecutor.close();
}
});
commandExecutor.executeCommand(new NewGameCommand()); // commandExecutor.addListener(new GameAdaptator() {
// @Override
// public void onGameEnd() {
// System.out.println(exportGame(game));
// commandExecutor.close();
// }
// });
// commandExecutor.executeCommand(new NewGameCommand());
// }
private static Piece pieceAt(CommandExecutor commandExecutor, Coordinate coordinate) {
GetPieceAtCommand cmd = new GetPieceAtCommand(coordinate);
commandExecutor.executeCommand(cmd);
return cmd.getPiece();
}
private static List<Move> playerMoves(CommandExecutor commandExecutor) {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
commandExecutor.executeCommand(cmd);
return cmd.getMoves();
}
private static String resolveAmbiguity(CommandExecutor cmdExec, Move pieceMove) {
Piece movingPiece = pieceAt(cmdExec, pieceMove.getStart());
assert movingPiece != null;
if (movingPiece instanceof Pawn)
return "";
List<Move> moves = playerMoves(cmdExec);
for (Move move : moves) {
if (move.equals(pieceMove) || !move.getFinish().equals(pieceMove.getFinish()))
continue;
Piece otherPiece = pieceAt(cmdExec, move.getStart());
// checking type of piece
if (otherPiece.hashCode() != movingPiece.hashCode())
continue;
String startPos = toString(pieceMove.getStart());
if (move.getStart().getX() != pieceMove.getStart().getX())
// not on the same column
return Character.toString(startPos.charAt(0));
else
return Character.toString(startPos.charAt(1));
}
return "";
} }
public static String exportGame(Game game) { public static String exportGame(Game game) {
// saveToFile(game.getMoves());
ChessBoard board = new ChessBoard(); ChessBoard board = new ChessBoard();
Game virtualGame = new Game(board); Game virtualGame = new Game(board);
@@ -57,18 +111,55 @@ public class PgnExport {
for (int i = 0; i < commands.size(); i++) { for (int i = 0; i < commands.size(); i++) {
PlayerCommand cmd = commands.get(i); PlayerCommand cmd = commands.get(i);
PlayerCommand nextCommand = null;
if (i != commands.size() - 1) {
nextCommand = commands.get(i + 1);
}
if (cmd instanceof MoveCommand move) { if (cmd instanceof MoveCommand move) {
if (virtualGame.getPlayerTurn() == Color.White) { if (virtualGame.getPlayerTurn() == Color.White) {
result += tour + "."; result += tour + ".";
tour++; tour++;
} }
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart()); Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
assert movingPiece != null;
// piece name
result += piecePgnName.visit(movingPiece); result += piecePgnName.visit(movingPiece);
if (move.getDeadPiece() != null)
// capture
if (move.getDeadPiece() != null) {
if (movingPiece instanceof Pawn) {
result += toString(move.getMove().getStart()).charAt(0);
}
result += "x"; result += "x";
}
// ambiguious start
result += resolveAmbiguity(executor, move.getMove());
// end cell
result += toString(move.getMove().getFinish()); 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";
} }
executor.executeCommand(cmd); executor.executeCommand(cmd);
// check or checkmate
switch (virtualGame.checkGameStatus()) { switch (virtualGame.checkGameStatus()) {
case CheckMate: case CheckMate:
result += "#"; result += "#";