working pgn
This commit is contained in:
@@ -4,45 +4,99 @@ import java.util.List;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
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.NewGameCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.event.EmptyGameDispatcher;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
import chess.simulator.FoolCheckMate;
|
||||
import chess.model.pieces.Pawn;
|
||||
|
||||
public class PgnExport {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Game game = new Game(new ChessBoard());
|
||||
final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
// public static void main(String[] args) {
|
||||
// final Game game = new Game(new ChessBoard());
|
||||
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
|
||||
commandExecutor.addListener(foolCheckMate);
|
||||
// // FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
|
||||
// // // commandExecutor.addListener(foolCheckMate);
|
||||
|
||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai1);
|
||||
// // PromoteTest promoteTest = new PromoteTest(commandExecutor);
|
||||
// // commandExecutor.addListener(promoteTest);
|
||||
|
||||
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
|
||||
// commandExecutor.addListener(ai2);
|
||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai1);
|
||||
|
||||
commandExecutor.addListener(new GameAdaptator() {
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
System.out.println(exportGame(game));
|
||||
commandExecutor.close();
|
||||
}
|
||||
});
|
||||
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
|
||||
// commandExecutor.addListener(ai2);
|
||||
|
||||
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) {
|
||||
|
||||
// saveToFile(game.getMoves());
|
||||
|
||||
ChessBoard board = new ChessBoard();
|
||||
Game virtualGame = new Game(board);
|
||||
|
||||
@@ -57,18 +111,55 @@ public class PgnExport {
|
||||
|
||||
for (int i = 0; i < commands.size(); 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 (virtualGame.getPlayerTurn() == Color.White) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||
|
||||
assert movingPiece != null;
|
||||
|
||||
// piece name
|
||||
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";
|
||||
}
|
||||
|
||||
// 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";
|
||||
}
|
||||
|
||||
executor.executeCommand(cmd);
|
||||
|
||||
// check or checkmate
|
||||
switch (virtualGame.checkGameStatus()) {
|
||||
case CheckMate:
|
||||
result += "#";
|
||||
|
||||
Reference in New Issue
Block a user