Compare commits
No commits in common. "a669a4b2ff86c904f83e8f0845fec62c63b2ca3c" and "12bea41b1e9b189df4d9e2182b70b3fad854d94d" have entirely different histories.
a669a4b2ff
...
12bea41b1e
|
|
@ -8,8 +8,6 @@ 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;
|
||||||
|
|
@ -83,25 +81,6 @@ 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,13 +14,9 @@ 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;
|
||||||
|
|
@ -253,17 +249,14 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveAsPgn(Game game, String path, String dateiname) {
|
public void saveAsPgn(Game game, String path, String dateiname) {
|
||||||
// Sicher alle Strings holen (nie null)
|
String event = game.getRound().getEvent().getName();
|
||||||
String event = safe(game.getRound().getEvent().getName());
|
String site = game.getRound().getEvent().getSite();
|
||||||
String site = safe(game.getRound().getEvent().getSite());
|
|
||||||
String round = "" + game.getRound().getNumber();
|
String round = "" + game.getRound().getNumber();
|
||||||
// Datum für PGN-Format (YYYY.MM.DD)
|
String date = game.getRound().getEvent().getStartDate();
|
||||||
String date = safe(game.getRound().getEvent().getStartDate()).replace("-", ".");
|
String wName = game.getWhitePlayer().getName();
|
||||||
String wName = safe(game.getWhitePlayer().getName());
|
String bName = game.getBlackPlayer().getName();
|
||||||
String bName = safe(game.getBlackPlayer().getName());
|
String result = game.getResult().getDescription();
|
||||||
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");
|
||||||
|
|
@ -271,90 +264,29 @@ 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\n");
|
header.append("[Result \"" + result + "\"]\n");
|
||||||
|
header.append("\n");
|
||||||
|
|
||||||
// Züge als SAN holen
|
StringBuilder sb = new StringBuilder();
|
||||||
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) {
|
||||||
moves.append((i / 2 + 1)).append(". ");
|
sb.append((i / 2 + 1)).append(". ");
|
||||||
}
|
}
|
||||||
moves.append(sanArray[i]).append(" ");
|
sb.append(sanArray[i]).append(" ");
|
||||||
// Optional: Zeilenumbruch für Lesbarkeit
|
|
||||||
// if (i > 0 && i % 8 == 0) moves.append("\n");
|
|
||||||
}
|
}
|
||||||
moves.append(result); // Ergebnis am Ende!
|
|
||||||
|
|
||||||
String file = header + moves.toString();
|
sb.append(result); // Endergebnis muss auch am Ende stehen!
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
||||||
|
|
@ -1,72 +0,0 @@
|
||||||
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,7 +27,6 @@ public class GameGui {
|
||||||
private JLabel[][] fields = new JLabel[8][8];
|
private JLabel[][] fields = new JLabel[8][8];
|
||||||
private JButton flipBoardButton;
|
private JButton flipBoardButton;
|
||||||
private boolean isFlipped = false;
|
private boolean isFlipped = false;
|
||||||
JButton btnSave = new JButton("💾");
|
|
||||||
|
|
||||||
private JLabel whiteTimerLabel;
|
private JLabel whiteTimerLabel;
|
||||||
private JLabel blackTimerLabel;
|
private JLabel blackTimerLabel;
|
||||||
|
|
@ -178,17 +177,15 @@ public class GameGui {
|
||||||
// Button-Leiste (SÜD)
|
// Button-Leiste (SÜD)
|
||||||
JPanel buttonPanel = new JPanel();
|
JPanel buttonPanel = new JPanel();
|
||||||
buttonPanel.setBackground(new Color(0x0d1b2a));
|
buttonPanel.setBackground(new Color(0x0d1b2a));
|
||||||
buttonPanel.setLayout(new GridLayout(1, 5, 10, 0)); // Jetzt 5 statt 4 Spalten!
|
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
|
||||||
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
|
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
|
||||||
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);
|
||||||
btnSave.setBackground(new Color(0x218838)); btnSave.setForeground(Color.WHITE);
|
|
||||||
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);
|
|
||||||
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
return statsPanel;
|
return statsPanel;
|
||||||
|
|
@ -269,7 +266,6 @@ 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