Compare commits
3 Commits
12bea41b1e
...
a669a4b2ff
| Author | SHA1 | Date |
|---|---|---|
|
|
a669a4b2ff | |
|
|
ea2c0066fd | |
|
|
ffbdb8b6e0 |
|
|
@ -8,6 +8,8 @@ import java.util.List;
|
|||
|
||||
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.MoveDTO;
|
||||
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||
|
|
@ -82,6 +84,25 @@ public class GameController {
|
|||
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 -> {
|
||||
//ALLE Highlights und Borders zurücksetzen
|
||||
|
|
|
|||
|
|
@ -14,9 +14,13 @@ import java.util.logging.SimpleFormatter;
|
|||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
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.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 {
|
||||
private Board board;
|
||||
|
|
@ -249,14 +253,17 @@ public class ChessEngine {
|
|||
}
|
||||
|
||||
public void saveAsPgn(Game game, String path, String dateiname) {
|
||||
String event = game.getRound().getEvent().getName();
|
||||
String site = game.getRound().getEvent().getSite();
|
||||
// Sicher alle Strings holen (nie null)
|
||||
String event = safe(game.getRound().getEvent().getName());
|
||||
String site = safe(game.getRound().getEvent().getSite());
|
||||
String round = "" + game.getRound().getNumber();
|
||||
String date = game.getRound().getEvent().getStartDate();
|
||||
String wName = game.getWhitePlayer().getName();
|
||||
String bName = game.getBlackPlayer().getName();
|
||||
String result = game.getResult().getDescription();
|
||||
// Datum für PGN-Format (YYYY.MM.DD)
|
||||
String date = safe(game.getRound().getEvent().getStartDate()).replace("-", ".");
|
||||
String wName = safe(game.getWhitePlayer().getName());
|
||||
String bName = safe(game.getBlackPlayer().getName());
|
||||
String result = safe(game.getResult().getDescription());
|
||||
|
||||
// PGN-Header zusammenbauen
|
||||
StringBuilder header = new StringBuilder();
|
||||
header.append("[Event \"" + event + "\"]\n");
|
||||
header.append("[Site \"" + site + "\"]\n");
|
||||
|
|
@ -264,23 +271,25 @@ public class ChessEngine {
|
|||
header.append("[Round \"" + round + "\"]\n");
|
||||
header.append("[White \"" + wName + "\"]\n");
|
||||
header.append("[Black \"" + bName + "\"]\n");
|
||||
header.append("[Result \"" + result + "\"]\n");
|
||||
header.append("\n");
|
||||
header.append("[Result \"" + result + "\"]\n\n");
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// Züge als SAN holen
|
||||
StringBuilder moves = new StringBuilder();
|
||||
String[] sanArray = game.getHalfMoves().toSanArray();
|
||||
|
||||
for (int i = 0; i < sanArray.length; i++) {
|
||||
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.toString() + sb.toString();
|
||||
String file = header + moves.toString();
|
||||
|
||||
// Datei schreiben
|
||||
try {
|
||||
Files.writeString(Path.of(path, dateiname), file, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
|
|
@ -288,6 +297,65 @@ public class ChessEngine {
|
|||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
board = new Board(); // Neues leeres Brett
|
||||
moves.clear();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.game.Player;
|
||||
import com.github.bhlangonijr.chesslib.game.PlayerType;
|
||||
|
||||
public class MyPlayer implements Player {
|
||||
private String id = "";
|
||||
private String name;
|
||||
private int elo = 0;
|
||||
private PlayerType type = PlayerType.HUMAN;
|
||||
private String description = "";
|
||||
private String longDescription = "";
|
||||
|
||||
public MyPlayer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getElo() {
|
||||
return elo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setElo(int elo) {
|
||||
this.elo = elo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setType(PlayerType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLongDescription() {
|
||||
return longDescription;
|
||||
}
|
||||
}
|
||||
|
|
@ -27,6 +27,7 @@ public class GameGui {
|
|||
private JLabel[][] fields = new JLabel[8][8];
|
||||
private JButton flipBoardButton;
|
||||
private boolean isFlipped = false;
|
||||
JButton btnSave = new JButton("💾");
|
||||
|
||||
private JLabel whiteTimerLabel;
|
||||
private JLabel blackTimerLabel;
|
||||
|
|
@ -177,15 +178,17 @@ public class GameGui {
|
|||
// Button-Leiste (SÜD)
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.setBackground(new Color(0x0d1b2a));
|
||||
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
|
||||
buttonPanel.setLayout(new GridLayout(1, 5, 10, 0)); // Jetzt 5 statt 4 Spalten!
|
||||
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
|
||||
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
|
||||
btnNext.setBackground(new Color(0x212529)); btnNext.setForeground(Color.WHITE);
|
||||
btnLast.setBackground(new Color(0x212529)); btnLast.setForeground(Color.WHITE);
|
||||
btnSave.setBackground(new Color(0x218838)); btnSave.setForeground(Color.WHITE);
|
||||
buttonPanel.add(btnFirst);
|
||||
buttonPanel.add(btnPrev);
|
||||
buttonPanel.add(btnNext);
|
||||
buttonPanel.add(btnLast);
|
||||
buttonPanel.add(btnSave);
|
||||
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
return statsPanel;
|
||||
|
|
@ -266,6 +269,7 @@ public class GameGui {
|
|||
public JButton getBtnPrev() { return btnPrev; }
|
||||
public JButton getBtnNext() { return btnNext; }
|
||||
public JButton getBtnLast() { return btnLast; }
|
||||
public JButton getBtnSave() { return btnSave; }
|
||||
|
||||
public void updateBoard(BoardDTO boardDTO) {
|
||||
PieceDTO[][] board = boardDTO.getBoard();
|
||||
|
|
|
|||
Loading…
Reference in New Issue