Compare commits

...

3 Commits
Main ... PGN

Author SHA1 Message Date
valen 57b68bb0d2 Das Game kann jetzt die einzelnen Spiele starten 2025-06-23 00:32:42 +02:00
valen 8e45acb0b3 PGN datei auswählbar
Einzelne Games als Button angezeigt(Man kann sie noch nicht starten)
2025-06-23 00:16:14 +02:00
valen a19130bb86 loadGamesFromPgn() wurde erstellt
saveAsPgn wurde erstellt
2025-06-22 23:28:05 +02:00
3 changed files with 146 additions and 16 deletions

View File

@ -1,4 +1,6 @@
package de.hs_mannheim.informatik.chess.main;
import java.io.IOException;
import de.hs_mannheim.informatik.chess.controller.Controller;
import de.hs_mannheim.informatik.chess.model.ChessEngine;
import de.hs_mannheim.informatik.chess.view.GameGui;

View File

@ -1,4 +1,8 @@
package de.hs_mannheim.informatik.chess.model;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.ConsoleHandler;
@ -10,7 +14,10 @@ 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.game.GameResult;
import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
public class ChessEngine {
private Board board;
@ -187,4 +194,71 @@ public class ChessEngine {
logger.info("ChessEngine wurde initialisiert.");
}
public List<Game> loadGamesFromPgn(String path) throws IOException {
PgnHolder pgnHolder = new PgnHolder(path);
try {
pgnHolder.loadPgn();
} catch (Exception e) {
e.printStackTrace();
}
List<Game> games = pgnHolder.getGames();
return games;
}
public void saveAsPgn(Game game, String path, String dateiname) {
String event = game.getRound().getEvent().getName();
String site = 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();
StringBuilder header = new StringBuilder();
header.append("[Event \"" + event + "\"]\n");
header.append("[Site \"" + site + "\"]\n");
header.append("[Date \"" + date + "\"]\n");
header.append("[Round \"" + round + "\"]\n");
header.append("[White \"" + wName + "\"]\n");
header.append("[Black \"" + bName + "\"]\n");
header.append("[Result \"" + result + "\"]\n");
header.append("\n");
StringBuilder sb = 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(". ");
}
sb.append(sanArray[i]).append(" ");
}
sb.append(result); // Endergebnis muss auch am Ende stehen!
String file = header.toString() + sb.toString();
try {
Files.writeString(Path.of(path, dateiname), file, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadMoves(List<Move> moveList) {
board = new Board(); // Neues leeres Brett
moves.clear();
currentMoveIndex = 0;
for (Move move : moveList) {
board.doMove(move);
moves.add(move);
currentMoveIndex++;
}
// Kein setPositionToMoveIndex() hier aufrufen!
}
}

View File

@ -1,22 +1,29 @@
package de.hs_mannheim.informatik.chess.view;
import java.awt.*;
import com.github.bhlangonijr.chesslib.game.Game;
import com.github.bhlangonijr.chesslib.move.Move;
import de.hs_mannheim.informatik.chess.controller.Controller;
import de.hs_mannheim.informatik.chess.model.ChessEngine;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.List;
public class MainGui {
private JFrame frame;
private Runnable onStartGame;
private ChessEngine engine = new ChessEngine();
public MainGui(Runnable onStartGame) {
this.onStartGame = onStartGame;
this.onStartGame = onStartGame;
frame = new JFrame("Chess - Hauptmenü");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1600, 1000);
frame.setLocationRelativeTo(null);
//Haupt-Panel mit GridBagLayout
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBackground(new Color(0xe0e1dd));
@ -25,20 +32,17 @@ public class MainGui {
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(15, 0, 15, 0);
//Title
JLabel title = new JLabel("ChessDE", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD, 150));
title.setForeground(new Color(0x1b263b));
gbc.gridy = 0;
gbc.ipady = 50;
gbc.ipady = 50;
mainPanel.add(title, gbc);
//Abstand nach unten
gbc.gridy = 1;
gbc.ipady = 15;
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)), gbc);
//Buttons
JButton btnMode1 = new JButton("Normal Mode");
JButton btnMode2 = new JButton("Mode 2 (coming soon)");
JButton btnMode3 = new JButton("Mode 3 (coming soon)");
@ -59,24 +63,74 @@ public class MainGui {
gbc.gridy = 4;
mainPanel.add(btnMode3, gbc);
gbc.gridy = 5;
gbc.gridy = 5;
mainPanel.add(btnCreative, gbc);
gbc.gridy = 6;
gbc.gridy = 6;
mainPanel.add(btnLoadGame, gbc);
frame.add(mainPanel);
frame.setVisible(true);
//Button ActionListener für "Normal Modus"
btnMode1.addActionListener(e -> {
frame.dispose(); // Hauptmenü schließen
onStartGame.run(); // **Ruft den Callback auf**
frame.dispose();
onStartGame.run();
});
btnLoadGame.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getAbsolutePath();
try {
List<Game> games = engine.loadGamesFromPgn(path);
zeigeGeladeneSpiele(games);
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "Fehler beim Laden der PGN-Datei:\n" + ex.getMessage());
}
}
});
}
// Helper für Button Styling
private void zeigeGeladeneSpiele(List<Game> games) {
JFrame gameListFrame = new JFrame("Geladene Partien");
gameListFrame.setSize(600, 800);
gameListFrame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(panel);
int index = 1;
for (Game game : games) {
String white = game.getWhitePlayer().getName();
String black = game.getBlackPlayer().getName();
String title = "Spiel " + index++ + ": " + white + " vs. " + black;
JButton gameButton = new JButton(title);
gameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
gameButton.addActionListener(e -> {
gameListFrame.dispose();
GameGui gui = new GameGui();
ChessEngine engine = new ChessEngine();
engine.loadMoves(game.getHalfMoves());
engine.setPositionToMoveIndex(engine.getMoveListSize()); // zeige Endstellung
new Controller(gui, engine);
});
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(gameButton);
}
gameListFrame.add(scrollPane);
gameListFrame.setVisible(true);
}
private void styleButton(JButton btn) {
btn.setFont(new Font("Serif", Font.BOLD, 30));
btn.setBackground(new Color(0x778da9));