very basic pgn export

This commit is contained in:
2025-04-14 11:56:19 +02:00
parent d8c927083a
commit 1a038a3de1
6 changed files with 180 additions and 7 deletions

View File

@@ -1,21 +1,34 @@
package chess.pgn;
import java.util.List;
import chess.controller.CommandExecutor;
import chess.controller.PlayerCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
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.Piece;
import chess.simulator.FoolCheckMate;
import chess.view.consolerender.Console;
public class PgnExport {
public static void main(String[] args) {
final Game game = new Game(new ChessBoard());
final CommandExecutor commandExecutor = new CommandExecutor(game);
final CommandExecutor commandExecutor = new CommandExecutor(game);
FoolCheckMate checkMate = new FoolCheckMate(commandExecutor);
commandExecutor.addListener(checkMate);
FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
commandExecutor.addListener(foolCheckMate);
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
// commandExecutor.addListener(ai1);
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
// commandExecutor.addListener(ai2);
commandExecutor.addListener(new GameAdaptator() {
@Override
@@ -29,6 +42,53 @@ public class PgnExport {
}
public static String exportGame(Game game) {
return "coucou";
ChessBoard board = new ChessBoard();
Game virtualGame = new Game(board);
CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher());
executor.executeCommand(new NewGameCommand());
List<PlayerCommand> commands = game.getMoves();
PiecePgnName piecePgnName = new PiecePgnName();
String result = "";
int tour = 1;
for (int i = 0; i < commands.size(); i++) {
PlayerCommand cmd = commands.get(i);
if (cmd instanceof MoveCommand move) {
if (virtualGame.getPlayerTurn() == Color.White) {
result += tour + ".";
tour++;
}
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
result += piecePgnName.visit(movingPiece);
if (move.getDeadPiece() != null)
result += "x";
result += toString(move.getMove().getFinish());
}
executor.executeCommand(cmd);
switch (virtualGame.checkGameStatus()) {
case CheckMate:
result += "#";
break;
case Check:
result += "+";
break;
default:
break;
}
result += " ";
}
return result;
}
public static String toString(Coordinate coordinate) {
String letters = "abcdefgh";
String numbers = "87654321";
return Character.toString(letters.charAt(coordinate.getX())) + numbers.charAt(coordinate.getY());
}
}