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

@@ -17,8 +17,12 @@ public class CommandExecutor {
}
public CommandExecutor(Game game) {
this(game, new GameDispatcher());
}
public CommandExecutor(Game game, GameDispatcher dispatcher) {
this.game = game;
this.dispatcher = new GameDispatcher();
this.dispatcher = dispatcher;
}
public synchronized CommandResult executeCommand(Command command) {

View File

@@ -23,6 +23,10 @@ public class MoveCommand extends PlayerCommand {
return move;
}
public Piece getDeadPiece() {
return deadPiece;
}
@Override
public CommandResult execute(Game game, GameListener outputSystem) {
CommandResult result = processMove(game, outputSystem);
@@ -98,5 +102,4 @@ public class MoveCommand extends PlayerCommand {
outputSystem.onPromotePawn(pawnPos);
return true;
}
}

View File

@@ -0,0 +1,61 @@
package chess.controller.event;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
public class EmptyGameDispatcher extends GameDispatcher {
@Override
public void onBoardUpdate() {
}
@Override
public void onDraw() {
}
@Override
public void onGameEnd() {
}
@Override
public void onGameStart() {
}
@Override
public void onKingInCheck() {
}
@Override
public void onKingInMat() {
}
@Override
public void onMove(Move move) {
}
@Override
public void onMoveNotAllowed(Move move) {
}
@Override
public void onPatSituation() {
}
@Override
public void onPlayerTurn(Color color) {
}
@Override
public void onPromotePawn(Coordinate pieceCoords) {
}
@Override
public void onSurrender(Color coward) {
}
@Override
public void onWin(Color winner) {
}
}

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());
}
}

View File

@@ -0,0 +1,43 @@
package chess.pgn;
import chess.model.PieceVisitor;
import chess.model.pieces.Bishop;
import chess.model.pieces.King;
import chess.model.pieces.Knight;
import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
public class PiecePgnName implements PieceVisitor<String> {
@Override
public String visitPiece(Bishop bishop) {
return "B";
}
@Override
public String visitPiece(King king) {
return "K";
}
@Override
public String visitPiece(Knight knight) {
return "N";
}
@Override
public String visitPiece(Pawn pawn) {
return "";
}
@Override
public String visitPiece(Queen queen) {
return "Q";
}
@Override
public String visitPiece(Rook rook) {
return "R";
}
}

View File

@@ -32,6 +32,8 @@ public class PromoteTest extends Simulator{
new Move(new Coordinate(4, 2), new Coordinate(4, 1)),
// black king
new Move(new Coordinate(5, 2), new Coordinate(6, 2))
// white pawn moves
// new Move(new Coordinate(4, 1), new Coordinate(4, 0))
);
}