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();
ChessEngine engine = new ChessEngine();
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() {

View File

@ -25,6 +25,7 @@ import com.github.bhlangonijr.chesslib.Side;
public class ChessEngine {
private Board board;
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 int currentMoveIndex = 0;
@ -188,9 +189,9 @@ public class ChessEngine {
}
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.loadFromFen(initialFen); // Statt new Board() -> initialFen!
for (int i = 0; i < idx; i++) {
board.doMove(moves.get(i));
}
@ -199,7 +200,6 @@ public class ChessEngine {
String playedMovesUci = movesToUciString(moves.subList(0, idx));
detectedOpening = Opening.detect(playedMovesUci);
}
public int getCurrentMoveIndex() {
logger.info("Hole aktuellen Zug-Index: " + currentMoveIndex);
return currentMoveIndex;
@ -220,6 +220,7 @@ public class ChessEngine {
public void setPositionFromFEN(String fen) {
board.loadFromFen(fen);
initialFen = fen;
}

View File

@ -5,6 +5,11 @@ import java.awt.*;
import java.util.HashMap;
public class CreativeGui {
public interface StartGameCallback {
void onStartGame(String fen);
}
private boolean isFlipped = false;
private JFrame frame;
@ -13,6 +18,10 @@ public class CreativeGui {
private JTextField fenField;
private JButton updateBtn;
private JButton flipBoardButton;
private StartGameCallback startGameCallback;
private JButton startGameButton;
public static final HashMap<String, String> UNICODE_MAP = new HashMap<String, String>() {{
put("BLACK_KING", "\u265A"); put("BLACK_QUEEN", "\u265B");
@ -41,7 +50,7 @@ public class CreativeGui {
// LINKS: chessPanel (Board+Toolbars)
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.6;
gbc.weightx = 0.7;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 5, 5, 0);
gbc.fill = GridBagConstraints.BOTH;
@ -50,7 +59,7 @@ public class CreativeGui {
// RECHTS: FEN & Optionen
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.4;
gbc.weightx = 0.3;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 0, 5, 5);
gbc.fill = GridBagConstraints.BOTH;
@ -72,7 +81,7 @@ public class CreativeGui {
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(70, 70));
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;
btn.addActionListener(e -> selectedPiece = key);
panel.add(btn);
@ -105,47 +114,76 @@ public class CreativeGui {
}
private JPanel chessPanel() {
JPanel chessPanel = new JPanel(new GridBagLayout());
JPanel chessPanel = new JPanel(new BorderLayout());
chessPanel.setBackground(new Color(0x1b263b));
GridBagConstraints gbc = new GridBagConstraints();
// Toolbar oben
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
chessPanel.add(toolbarPanel(false), gbc);
// Board UND Toolbars in EINEM Panel, damit sie linksbündig mit dem Brett starten!
JPanel boardAndToolbars = new JPanel(new BorderLayout());
boardAndToolbars.setOpaque(false);
// Board
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
chessPanel.add(boardPanel(), gbc);
// Toolbar Schwarz (oben)
JPanel blackToolbar = toolbarPanel(false);
boardAndToolbars.add(blackToolbar, BorderLayout.NORTH);
// Toolbar unten
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
chessPanel.add(toolbarPanel(true), gbc);
// Board (zentriert im Panel)
JPanel board = boardPanel();
boardAndToolbars.add(board, BorderLayout.CENTER);
// Drehknopf
flipBoardButton = new JButton("⇵");
flipBoardButton.setPreferredSize(new Dimension(70, 70));
flipBoardButton.setFont(new Font("SansSerif", Font.BOLD, 40));
flipBoardButton.setBackground(new Color(0x5500ff));
flipBoardButton.setForeground(Color.WHITE);
flipBoardButton.setFocusPainted(false);
// Toolbar Weiß (unten)
JPanel whiteToolbar = toolbarPanel(true);
boardAndToolbars.add(whiteToolbar, BorderLayout.SOUTH);
// Board+Toolbars mittig in einem Panel mit FlowLayout, damit sie zentriert sind
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
centerPanel.setOpaque(false);
centerPanel.add(boardAndToolbars);
GridBagConstraints btn = new GridBagConstraints();
btn.gridx = 0;
btn.gridy = 2;
btn.weightx = 0.0;
btn.weighty = 0.0;
btn.anchor = GridBagConstraints.EAST;
btn.insets = new Insets(5, 0, 0, 0);
chessPanel.add(flipBoardButton, btn);
blackToolbar.setPreferredSize(new Dimension(0, 70)); // 70 px hoch
whiteToolbar.setPreferredSize(new Dimension(0, 70));
// 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;
}
// 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() {
JPanel panel = new JPanel();
@ -160,7 +198,20 @@ public class CreativeGui {
updateBtn = new JButton("Update Board");
updateBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
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;
}
// Für Controller
@ -172,6 +223,12 @@ public class CreativeGui {
public void setSelectedPiece(String piece) { selectedPiece = piece; }
public String getSelectedPiece() { return selectedPiece; }
public JButton getStartGameButton() { return startGameButton; }
public void setStartGameCallback(StartGameCallback callback) {
this.startGameCallback = callback;
}
public boolean isFlipped() { return isFlipped; }
public void setFlipped(boolean f) { isFlipped = f; }
public JButton getFlipBoardButton() { return flipBoardButton; }

View File

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

View File

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