Aktuelles Spiel kann als pgn an gewünschten Speicherort, mit gewünschten
Namen gespeichert werden. Header bis auf Datum ist immer GleichSavePgn
parent
1dccba2e15
commit
ffbdb8b6e0
|
@ -8,6 +8,8 @@ import java.util.List;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
|
|
||||||
|
import com.github.bhlangonijr.chesslib.game.Game;
|
||||||
|
|
||||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||||
import de.hs_mannheim.informatik.chess.model.MoveDTO;
|
import de.hs_mannheim.informatik.chess.model.MoveDTO;
|
||||||
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||||
|
@ -68,6 +70,25 @@ public class GameController {
|
||||||
engine.setPositionToMoveIndex(engine.getMoveListSize());
|
engine.setPositionToMoveIndex(engine.getMoveListSize());
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
gui.getBtnSave().addActionListener(e -> {
|
||||||
|
System.out.println("Save-Button wurde geklickt!");
|
||||||
|
Game currentGame = engine.getCurrentGame();
|
||||||
|
javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
|
||||||
|
fileChooser.setDialogTitle("PGN speichern unter...");
|
||||||
|
int userSelection = fileChooser.showSaveDialog(null);
|
||||||
|
|
||||||
|
if (userSelection == javax.swing.JFileChooser.APPROVE_OPTION) {
|
||||||
|
java.io.File fileToSave = fileChooser.getSelectedFile();
|
||||||
|
try {
|
||||||
|
engine.saveAsPgn(currentGame, fileToSave.getParent(), fileToSave.getName());
|
||||||
|
gui.displayMessage("PGN gespeichert: " + fileToSave.getAbsolutePath());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
gui.displayMessage("Fehler beim Speichern: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
gui.getFlipBoardButton().addActionListener(e -> {
|
gui.getFlipBoardButton().addActionListener(e -> {
|
||||||
|
|
|
@ -14,9 +14,13 @@ import java.util.logging.SimpleFormatter;
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
import com.github.bhlangonijr.chesslib.Piece;
|
import com.github.bhlangonijr.chesslib.Piece;
|
||||||
import com.github.bhlangonijr.chesslib.Square;
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
import com.github.bhlangonijr.chesslib.game.Game;
|
|
||||||
import com.github.bhlangonijr.chesslib.move.Move;
|
import com.github.bhlangonijr.chesslib.move.Move;
|
||||||
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||||
|
import com.github.bhlangonijr.chesslib.game.*;
|
||||||
|
import com.github.bhlangonijr.chesslib.move.MoveList;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import com.github.bhlangonijr.chesslib.Side;
|
||||||
|
|
||||||
|
|
||||||
public class ChessEngine {
|
public class ChessEngine {
|
||||||
private Board board;
|
private Board board;
|
||||||
|
@ -242,14 +246,17 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveAsPgn(Game game, String path, String dateiname) {
|
public void saveAsPgn(Game game, String path, String dateiname) {
|
||||||
String event = game.getRound().getEvent().getName();
|
// Sicher alle Strings holen (nie null)
|
||||||
String site = game.getRound().getEvent().getSite();
|
String event = safe(game.getRound().getEvent().getName());
|
||||||
|
String site = safe(game.getRound().getEvent().getSite());
|
||||||
String round = "" + game.getRound().getNumber();
|
String round = "" + game.getRound().getNumber();
|
||||||
String date = game.getRound().getEvent().getStartDate();
|
// Datum für PGN-Format (YYYY.MM.DD)
|
||||||
String wName = game.getWhitePlayer().getName();
|
String date = safe(game.getRound().getEvent().getStartDate()).replace("-", ".");
|
||||||
String bName = game.getBlackPlayer().getName();
|
String wName = safe(game.getWhitePlayer().getName());
|
||||||
String result = game.getResult().getDescription();
|
String bName = safe(game.getBlackPlayer().getName());
|
||||||
|
String result = safe(game.getResult().getDescription());
|
||||||
|
|
||||||
|
// PGN-Header zusammenbauen
|
||||||
StringBuilder header = new StringBuilder();
|
StringBuilder header = new StringBuilder();
|
||||||
header.append("[Event \"" + event + "\"]\n");
|
header.append("[Event \"" + event + "\"]\n");
|
||||||
header.append("[Site \"" + site + "\"]\n");
|
header.append("[Site \"" + site + "\"]\n");
|
||||||
|
@ -257,29 +264,90 @@ public class ChessEngine {
|
||||||
header.append("[Round \"" + round + "\"]\n");
|
header.append("[Round \"" + round + "\"]\n");
|
||||||
header.append("[White \"" + wName + "\"]\n");
|
header.append("[White \"" + wName + "\"]\n");
|
||||||
header.append("[Black \"" + bName + "\"]\n");
|
header.append("[Black \"" + bName + "\"]\n");
|
||||||
header.append("[Result \"" + result + "\"]\n");
|
header.append("[Result \"" + result + "\"]\n\n");
|
||||||
header.append("\n");
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
// Züge als SAN holen
|
||||||
|
StringBuilder moves = new StringBuilder();
|
||||||
String[] sanArray = game.getHalfMoves().toSanArray();
|
String[] sanArray = game.getHalfMoves().toSanArray();
|
||||||
|
|
||||||
for (int i = 0; i < sanArray.length; i++) {
|
for (int i = 0; i < sanArray.length; i++) {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
sb.append((i / 2 + 1)).append(". ");
|
moves.append((i / 2 + 1)).append(". ");
|
||||||
}
|
}
|
||||||
sb.append(sanArray[i]).append(" ");
|
moves.append(sanArray[i]).append(" ");
|
||||||
|
// Optional: Zeilenumbruch für Lesbarkeit
|
||||||
|
// if (i > 0 && i % 8 == 0) moves.append("\n");
|
||||||
}
|
}
|
||||||
|
moves.append(result); // Ergebnis am Ende!
|
||||||
|
|
||||||
sb.append(result); // Endergebnis muss auch am Ende stehen!
|
String file = header + moves.toString();
|
||||||
|
|
||||||
String file = header.toString() + sb.toString();
|
|
||||||
|
|
||||||
|
// Datei schreiben
|
||||||
try {
|
try {
|
||||||
Files.writeString(Path.of(path, dateiname), file, StandardCharsets.UTF_8);
|
Files.writeString(Path.of(path, dateiname), file, StandardCharsets.UTF_8);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion für Null-Sicherheit
|
||||||
|
private String safe(String s) {
|
||||||
|
return s == null ? "?" : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game getCurrentGame() {
|
||||||
|
return getCurrentGame(this.board, this.moves, this.currentMoveIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game getCurrentGame(Board board, java.util.List<Move> moves, int currentMoveIndex) {
|
||||||
|
// Event und Turnierdaten setzen
|
||||||
|
Event event = new Event();
|
||||||
|
event.setName("Generated Game");
|
||||||
|
event.setSite("Local");
|
||||||
|
event.setStartDate(LocalDate.now().toString()); // Format: yyyy-MM-dd
|
||||||
|
|
||||||
|
// Runde anlegen
|
||||||
|
Round round = new Round(event);
|
||||||
|
round.setNumber(1);
|
||||||
|
|
||||||
|
// Spiel initialisieren
|
||||||
|
Game game = new Game("1", round); // "1" ist die Game-ID
|
||||||
|
|
||||||
|
// Spieler setzen (deine MyPlayer-Klasse)
|
||||||
|
game.setWhitePlayer(new MyPlayer("White"));
|
||||||
|
game.setBlackPlayer(new MyPlayer("Black"));
|
||||||
|
|
||||||
|
// Ergebnis setzen
|
||||||
|
if (board.isMated()) {
|
||||||
|
game.setResult(board.getSideToMove() == Side.WHITE ? GameResult.BLACK_WON : GameResult.WHITE_WON);
|
||||||
|
} else if (board.isStaleMate() || board.isDraw()) {
|
||||||
|
game.setResult(GameResult.DRAW);
|
||||||
|
} else {
|
||||||
|
game.setResult(GameResult.ONGOING);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Züge übernehmen
|
||||||
|
MoveList moveList = new MoveList();
|
||||||
|
for (Move move : moves) {
|
||||||
|
moveList.add(move);
|
||||||
|
}
|
||||||
|
game.setHalfMoves(moveList);
|
||||||
|
|
||||||
|
// Position auf aktuellen Zug setzen (letzter gespielter Halbzug)
|
||||||
|
if (currentMoveIndex > 0 && currentMoveIndex <= moveList.size()) {
|
||||||
|
game.setPosition(currentMoveIndex - 1);
|
||||||
|
} else {
|
||||||
|
game.setPosition(moveList.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FEN setzen: JETZT das aktuelle Board-FEN verwenden!
|
||||||
|
game.setBoard(new Board());
|
||||||
|
game.getBoard().loadFromFen(board.getFen());
|
||||||
|
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void loadMoves(List<Move> moveList) {
|
public void loadMoves(List<Move> moveList) {
|
||||||
board = new Board(); // Neues leeres Brett
|
board = new Board(); // Neues leeres Brett
|
||||||
|
|
|
@ -32,6 +32,7 @@ public class GameGui {
|
||||||
JButton btnPrev = new JButton("<");
|
JButton btnPrev = new JButton("<");
|
||||||
JButton btnNext = new JButton(">");
|
JButton btnNext = new JButton(">");
|
||||||
JButton btnLast = new JButton(">|");
|
JButton btnLast = new JButton(">|");
|
||||||
|
JButton btnSave = new JButton("SavePgn");
|
||||||
|
|
||||||
Color LIGHT = new Color(0xe0e1dd);
|
Color LIGHT = new Color(0xe0e1dd);
|
||||||
Color DARK = new Color(0x778da9);
|
Color DARK = new Color(0x778da9);
|
||||||
|
@ -179,12 +180,14 @@ public class GameGui {
|
||||||
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
|
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
|
||||||
btnNext.setBackground(new Color(0x212529)); btnNext.setForeground(Color.WHITE);
|
btnNext.setBackground(new Color(0x212529)); btnNext.setForeground(Color.WHITE);
|
||||||
btnLast.setBackground(new Color(0x212529)); btnLast.setForeground(Color.WHITE);
|
btnLast.setBackground(new Color(0x212529)); btnLast.setForeground(Color.WHITE);
|
||||||
|
btnLast.setBackground(new Color(0x212529)); btnSave.setForeground(Color.WHITE);
|
||||||
|
|
||||||
// Hinzufügen
|
// Hinzufügen
|
||||||
buttonPanel.add(btnFirst);
|
buttonPanel.add(btnFirst);
|
||||||
buttonPanel.add(btnPrev);
|
buttonPanel.add(btnPrev);
|
||||||
buttonPanel.add(btnNext);
|
buttonPanel.add(btnNext);
|
||||||
buttonPanel.add(btnLast);
|
buttonPanel.add(btnLast);
|
||||||
|
buttonPanel.add(btnSave);
|
||||||
|
|
||||||
// Unten ins BorderLayout
|
// Unten ins BorderLayout
|
||||||
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
||||||
|
@ -243,6 +246,7 @@ public class GameGui {
|
||||||
public JButton getBtnPrev() { return btnPrev; }
|
public JButton getBtnPrev() { return btnPrev; }
|
||||||
public JButton getBtnNext() { return btnNext; }
|
public JButton getBtnNext() { return btnNext; }
|
||||||
public JButton getBtnLast() { return btnLast; }
|
public JButton getBtnLast() { return btnLast; }
|
||||||
|
public JButton getBtnSave() { return btnSave; }
|
||||||
|
|
||||||
public void updateBoard(BoardDTO boardDTO) {
|
public void updateBoard(BoardDTO boardDTO) {
|
||||||
PieceDTO[][] board = boardDTO.getBoard();
|
PieceDTO[][] board = boardDTO.getBoard();
|
||||||
|
|
Loading…
Reference in New Issue