Fully implemented creativeMode

TestingBranch
Justin 2025-06-24 01:47:00 +02:00
parent 6d3154d0e3
commit 8447ce5fe3
5 changed files with 116 additions and 38 deletions

View File

@ -47,6 +47,24 @@ public class MainController {
CreativeGui creativegui = new CreativeGui(); CreativeGui creativegui = new CreativeGui();
ChessEngine engine = new ChessEngine(); ChessEngine engine = new ChessEngine();
new CreativeController(creativegui, engine); new CreativeController(creativegui, engine);
creativegui.setStartGameCallback(fen -> {
ChessEngine newEngine = new ChessEngine();
newEngine.setPositionFromFEN(fen);
GameGui gameGui = new GameGui();
GameEndCallback callback = new GameEndCallback() {
public void onNewGameRequested() {
startCreativeMode();
}
public void onReturnToMenu() {
new MainController();
}
};
new GameController(gameGui, newEngine, callback);
});
} }
private void startLoadGameMode() { private void startLoadGameMode() {

View File

@ -25,6 +25,7 @@ import com.github.bhlangonijr.chesslib.Side;
public class ChessEngine { public class ChessEngine {
private Board board; private Board board;
private List<Move> moves = new ArrayList<>(); private List<Move> moves = new ArrayList<>();
private String initialFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName()); private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
private int currentMoveIndex = 0; private int currentMoveIndex = 0;
@ -188,9 +189,9 @@ public class ChessEngine {
} }
public void setPositionToMoveIndex(int idx) { public void setPositionToMoveIndex(int idx) {
// Neues Board erzeugen logger.info("Setze Board auf Zug-Index: " + idx);
logger.info("Setze Board auf Zug-Index: " + idx);
board = new Board(); board = new Board();
board.loadFromFen(initialFen); // Statt new Board() -> initialFen!
for (int i = 0; i < idx; i++) { for (int i = 0; i < idx; i++) {
board.doMove(moves.get(i)); board.doMove(moves.get(i));
} }
@ -199,7 +200,6 @@ public class ChessEngine {
String playedMovesUci = movesToUciString(moves.subList(0, idx)); String playedMovesUci = movesToUciString(moves.subList(0, idx));
detectedOpening = Opening.detect(playedMovesUci); detectedOpening = Opening.detect(playedMovesUci);
} }
public int getCurrentMoveIndex() { public int getCurrentMoveIndex() {
logger.info("Hole aktuellen Zug-Index: " + currentMoveIndex); logger.info("Hole aktuellen Zug-Index: " + currentMoveIndex);
return currentMoveIndex; return currentMoveIndex;
@ -220,6 +220,7 @@ public class ChessEngine {
public void setPositionFromFEN(String fen) { public void setPositionFromFEN(String fen) {
board.loadFromFen(fen); board.loadFromFen(fen);
initialFen = fen;
} }

View File

@ -5,6 +5,11 @@ import java.awt.*;
import java.util.HashMap; import java.util.HashMap;
public class CreativeGui { public class CreativeGui {
public interface StartGameCallback {
void onStartGame(String fen);
}
private boolean isFlipped = false; private boolean isFlipped = false;
private JFrame frame; private JFrame frame;
@ -14,6 +19,10 @@ public class CreativeGui {
private JButton updateBtn; private JButton updateBtn;
private JButton flipBoardButton; private JButton flipBoardButton;
private StartGameCallback startGameCallback;
private JButton startGameButton;
public static final HashMap<String, String> UNICODE_MAP = new HashMap<String, String>() {{ public static final HashMap<String, String> UNICODE_MAP = new HashMap<String, String>() {{
put("BLACK_KING", "\u265A"); put("BLACK_QUEEN", "\u265B"); put("BLACK_KING", "\u265A"); put("BLACK_QUEEN", "\u265B");
put("BLACK_ROOK", "\u265C"); put("BLACK_BISHOP", "\u265D"); put("BLACK_ROOK", "\u265C"); put("BLACK_BISHOP", "\u265D");
@ -41,7 +50,7 @@ public class CreativeGui {
// LINKS: chessPanel (Board+Toolbars) // LINKS: chessPanel (Board+Toolbars)
gbc.gridx = 0; gbc.gridx = 0;
gbc.gridy = 0; gbc.gridy = 0;
gbc.weightx = 0.6; gbc.weightx = 0.7;
gbc.weighty = 1.0; gbc.weighty = 1.0;
gbc.insets = new Insets(5, 5, 5, 0); gbc.insets = new Insets(5, 5, 5, 0);
gbc.fill = GridBagConstraints.BOTH; gbc.fill = GridBagConstraints.BOTH;
@ -50,7 +59,7 @@ public class CreativeGui {
// RECHTS: FEN & Optionen // RECHTS: FEN & Optionen
gbc.gridx = 1; gbc.gridx = 1;
gbc.gridy = 0; gbc.gridy = 0;
gbc.weightx = 0.4; gbc.weightx = 0.3;
gbc.weighty = 1.0; gbc.weighty = 1.0;
gbc.insets = new Insets(5, 0, 5, 5); gbc.insets = new Insets(5, 0, 5, 5);
gbc.fill = GridBagConstraints.BOTH; gbc.fill = GridBagConstraints.BOTH;
@ -72,7 +81,7 @@ public class CreativeGui {
btn.setFocusPainted(false); btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(70, 70)); btn.setPreferredSize(new Dimension(70, 70));
btn.setBackground(white ? LIGHT : DARK); btn.setBackground(white ? LIGHT : DARK);
btn.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0)); btn.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
String key = prefix + type; String key = prefix + type;
btn.addActionListener(e -> selectedPiece = key); btn.addActionListener(e -> selectedPiece = key);
panel.add(btn); panel.add(btn);
@ -105,48 +114,77 @@ public class CreativeGui {
} }
private JPanel chessPanel() { private JPanel chessPanel() {
JPanel chessPanel = new JPanel(new GridBagLayout()); JPanel chessPanel = new JPanel(new BorderLayout());
chessPanel.setBackground(new Color(0x1b263b)); chessPanel.setBackground(new Color(0x1b263b));
GridBagConstraints gbc = new GridBagConstraints();
// Toolbar oben // Board UND Toolbars in EINEM Panel, damit sie linksbündig mit dem Brett starten!
gbc.gridx = 0; JPanel boardAndToolbars = new JPanel(new BorderLayout());
gbc.gridy = 0; boardAndToolbars.setOpaque(false);
gbc.fill = GridBagConstraints.HORIZONTAL;
chessPanel.add(toolbarPanel(false), gbc);
// Board // Toolbar Schwarz (oben)
gbc.gridx = 0; JPanel blackToolbar = toolbarPanel(false);
gbc.gridy = 1; boardAndToolbars.add(blackToolbar, BorderLayout.NORTH);
gbc.fill = GridBagConstraints.BOTH;
chessPanel.add(boardPanel(), gbc);
// Toolbar unten // Board (zentriert im Panel)
gbc.gridx = 0; JPanel board = boardPanel();
gbc.gridy = 2; boardAndToolbars.add(board, BorderLayout.CENTER);
gbc.fill = GridBagConstraints.HORIZONTAL;
chessPanel.add(toolbarPanel(true), gbc);
// Drehknopf // Toolbar Weiß (unten)
flipBoardButton = new JButton("⇵"); JPanel whiteToolbar = toolbarPanel(true);
flipBoardButton.setPreferredSize(new Dimension(70, 70)); boardAndToolbars.add(whiteToolbar, BorderLayout.SOUTH);
flipBoardButton.setFont(new Font("SansSerif", Font.BOLD, 40));
flipBoardButton.setBackground(new Color(0x5500ff));
flipBoardButton.setForeground(Color.WHITE);
flipBoardButton.setFocusPainted(false);
GridBagConstraints btn = new GridBagConstraints(); // Board+Toolbars mittig in einem Panel mit FlowLayout, damit sie zentriert sind
btn.gridx = 0; JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
btn.gridy = 2; centerPanel.setOpaque(false);
btn.weightx = 0.0; centerPanel.add(boardAndToolbars);
btn.weighty = 0.0;
btn.anchor = GridBagConstraints.EAST; blackToolbar.setPreferredSize(new Dimension(0, 70)); // 70 px hoch
btn.insets = new Insets(5, 0, 0, 0); whiteToolbar.setPreferredSize(new Dimension(0, 70));
chessPanel.add(flipBoardButton, btn); // Dynamisch skalieren wie gehabt
centerPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
int totalHeight = centerPanel.getHeight();
int totalWidth = centerPanel.getWidth();
int toolbarHeight = blackToolbar.getPreferredSize().height + whiteToolbar.getPreferredSize().height;
// Falls Toolbars keine PrefSize haben, nimm getHeight()
if (toolbarHeight == 0) toolbarHeight = blackToolbar.getHeight() + whiteToolbar.getHeight();
int boardMaxHeight = totalHeight - toolbarHeight;
int size = Math.min(totalWidth, boardMaxHeight);
board.setPreferredSize(new Dimension(size, size));
board.revalidate();
}
});
chessPanel.add(centerPanel, BorderLayout.CENTER);
// Drehknopf rechts, schön mittig
JPanel eastPanel = new JPanel();
eastPanel.setOpaque(false);
eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
eastPanel.add(Box.createVerticalGlue());
eastPanel.add(flipBoardButton = createFlipButton());
eastPanel.add(Box.createVerticalGlue());
chessPanel.add(eastPanel, BorderLayout.EAST);
// Buffer links, wenn du willst
chessPanel.add(Box.createRigidArea(new Dimension(40, 0)), BorderLayout.WEST);
return chessPanel; return chessPanel;
} }
// Flip-Knopf Builder, damit dus an mehreren Stellen nutzen kannst:
private JButton createFlipButton() {
JButton btn = new JButton("⇵");
btn.setPreferredSize(new Dimension(70, 70));
btn.setFont(new Font("SansSerif", Font.BOLD, 40));
btn.setBackground(new Color(0x5500ff));
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
return btn;
}
private JPanel fenPanel() { private JPanel fenPanel() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
@ -160,7 +198,20 @@ public class CreativeGui {
updateBtn = new JButton("Update Board"); updateBtn = new JButton("Update Board");
updateBtn.setAlignmentX(Component.CENTER_ALIGNMENT); updateBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(updateBtn); panel.add(updateBtn);
startGameButton = new JButton("🟢 Partie starten");
startGameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
startGameButton.setBackground(new Color(0x218838));
startGameButton.setForeground(Color.WHITE);
panel.add(startGameButton);
startGameButton.addActionListener(e -> {
if (startGameCallback != null) {
startGameCallback.onStartGame(getFenText());
}
});
return panel; return panel;
} }
// Für Controller // Für Controller
@ -172,6 +223,12 @@ public class CreativeGui {
public void setSelectedPiece(String piece) { selectedPiece = piece; } public void setSelectedPiece(String piece) { selectedPiece = piece; }
public String getSelectedPiece() { return selectedPiece; } public String getSelectedPiece() { return selectedPiece; }
public JButton getStartGameButton() { return startGameButton; }
public void setStartGameCallback(StartGameCallback callback) {
this.startGameCallback = callback;
}
public boolean isFlipped() { return isFlipped; } public boolean isFlipped() { return isFlipped; }
public void setFlipped(boolean f) { isFlipped = f; } public void setFlipped(boolean f) { isFlipped = f; }
public JButton getFlipBoardButton() { return flipBoardButton; } public JButton getFlipBoardButton() { return flipBoardButton; }

View File

@ -238,6 +238,7 @@ public class GameGui {
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); 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);

View File

@ -268,6 +268,7 @@ public class PgnGui {
openingLabel.setText("Eröffnung: " + text); openingLabel.setText("Eröffnung: " + text);
} }
public JButton getBtnFirst() { return btnFirst; } public JButton getBtnFirst() { return btnFirst; }
public JButton getBtnPrev() { return btnPrev; } public JButton getBtnPrev() { return btnPrev; }
public JButton getBtnNext() { return btnNext; } public JButton getBtnNext() { return btnNext; }