feat: pgn parser
This commit is contained in:
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.PlayerCommand;
|
||||
import chess.controller.Command.CommandResult;
|
||||
import chess.controller.commands.CastlingCommand;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.GetPlayerMovesCommand;
|
||||
@@ -88,7 +89,7 @@ public class PgnExport {
|
||||
Piece otherPiece = pieceAt(cmdExec, move.getStart());
|
||||
|
||||
// checking type of piece
|
||||
if (otherPiece.hashCode() != movingPiece.hashCode())
|
||||
if (!otherPiece.getClass().equals(movingPiece.getClass()))
|
||||
continue;
|
||||
|
||||
String startPos = toString(pieceMove.getStart());
|
||||
@@ -149,7 +150,10 @@ public class PgnExport {
|
||||
}
|
||||
}
|
||||
|
||||
private static String printMove(PlayerCommand cmd, PlayerCommand nextCommand, Game virtualGame,
|
||||
private record MoveResult(String move, CommandResult commandResult) {
|
||||
}
|
||||
|
||||
private static MoveResult printMove(PlayerCommand cmd, PlayerCommand nextCommand, Game virtualGame,
|
||||
CommandExecutor executor) {
|
||||
String result = "";
|
||||
if (cmd instanceof MoveCommand move) {
|
||||
@@ -177,14 +181,14 @@ public class PgnExport {
|
||||
result += castling(castlingCommand);
|
||||
}
|
||||
|
||||
executor.executeCommand(cmd);
|
||||
CommandResult commandResult = executor.executeCommand(cmd);
|
||||
|
||||
// check or checkmate
|
||||
result += checkCheckMate(virtualGame);
|
||||
|
||||
result += " ";
|
||||
|
||||
return result;
|
||||
return new MoveResult(result, commandResult);
|
||||
}
|
||||
|
||||
public static String exportGame(Game game) {
|
||||
@@ -199,17 +203,33 @@ public class PgnExport {
|
||||
|
||||
int tour = 1;
|
||||
|
||||
String lastMove = null;
|
||||
|
||||
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 (virtualGame.getPlayerTurn() == Color.White) {
|
||||
MoveResult moveResult = printMove(cmd, nextCommand, virtualGame, executor);
|
||||
if (moveResult.commandResult() == CommandResult.Moved && virtualGame.getPlayerTurn() == Color.Black) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
result += printMove(cmd, nextCommand, virtualGame, executor);
|
||||
|
||||
if (moveResult.commandResult() == CommandResult.ActionNeeded) {
|
||||
lastMove = moveResult.move();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastMove != null && moveResult.commandResult() == CommandResult.Moved){
|
||||
result += lastMove;
|
||||
lastMove = null;
|
||||
continue;
|
||||
}
|
||||
result += moveResult.move();
|
||||
|
||||
}
|
||||
return result + " " + gameEnd(virtualGame);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user