From 57b68bb0d28e59be8d637fcdac208ac78ea0a155 Mon Sep 17 00:00:00 2001 From: valen Date: Mon, 23 Jun 2025 00:32:42 +0200 Subject: [PATCH] Das Game kann jetzt die einzelnen Spiele starten --- .../informatik/chess/model/ChessEngine.java | 15 +++++ .../informatik/chess/view/MainGui.java | 59 ++++++++----------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java index 64c9eb4..c84df26 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java @@ -245,5 +245,20 @@ public class ChessEngine { e.printStackTrace(); } } + + public void loadMoves(List 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! + } + } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/MainGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/MainGui.java index 6fe0ec2..96d6b25 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/MainGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/MainGui.java @@ -1,19 +1,14 @@ package de.hs_mannheim.informatik.chess.view; -import de.hs_mannheim.informatik.chess.model.ChessEngine; -import com.github.bhlangonijr.chesslib.game.Game; -import java.util.List; -import java.awt.Color; -import java.awt.Component; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.awt.Cursor; -import java.io.IOException; +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 { @@ -22,14 +17,13 @@ public class MainGui { 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)); @@ -38,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)"); @@ -72,22 +63,21 @@ 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); @@ -102,7 +92,6 @@ public class MainGui { } } }); - } private void zeigeGeladeneSpiele(List games) { @@ -123,10 +112,15 @@ public class MainGui { JButton gameButton = new JButton(title); gameButton.setAlignmentX(Component.CENTER_ALIGNMENT); gameButton.addActionListener(e -> { - System.out.println("Ausgewähltes Spiel:\n" + game); - JOptionPane.showMessageDialog(gameListFrame, - "Noch keine Replay-Ansicht.\n" + - "Spiel: " + white + " vs. " + black); + 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))); @@ -137,7 +131,6 @@ public class MainGui { gameListFrame.setVisible(true); } - // Helper für Button Styling private void styleButton(JButton btn) { btn.setFont(new Font("Serif", Font.BOLD, 30)); btn.setBackground(new Color(0x778da9));