Fixed wrong chessboard square colors

SavePgn
Justin 2025-06-23 00:48:30 +02:00
parent 532059ba0b
commit 4fe9a8ad8a
2 changed files with 64 additions and 49 deletions

View File

@ -144,27 +144,37 @@ public class GameController {
boolean isWhitePromotion = isPawn && piece.getColor().equals("WHITE") && move.getToRow() == 0; boolean isWhitePromotion = isPawn && piece.getColor().equals("WHITE") && move.getToRow() == 0;
boolean isBlackPromotion = isPawn && piece.getColor().equals("BLACK") && move.getToRow() == 7; boolean isBlackPromotion = isPawn && piece.getColor().equals("BLACK") && move.getToRow() == 7;
boolean success = false;
if (isWhitePromotion || isBlackPromotion) { if (isWhitePromotion || isBlackPromotion) {
String color = piece.getColor().equals("WHITE") ? "Weiß" : "Schwarz"; String color = piece.getColor().equals("WHITE") ? "Weiß" : "Schwarz";
String promotion = gui.showPromotionDialog(color); String promotion = gui.showPromotionDialog(color);
if (engine.moveWithPromotion(move, promotion)) { success = engine.moveWithPromotion(move, promotion);
updateGuiBoard(); if (!success) {
gui.updateMoveList(engine.getMoveListStringsGrouped());
} else {
gui.displayMessage("Ungültiger Promotionszug!"); gui.displayMessage("Ungültiger Promotionszug!");
return;
}
} else {
success = engine.move(move);
if (!success) {
gui.displayMessage("Ungültiger Zug!");
return;
} }
return;
} }
// Normale Züge wie gehabt: updateGuiBoard();
if (engine.move(move)) { gui.updateMoveList(engine.getMoveListStringsGrouped());
updateGuiBoard();
gui.updateMoveList(engine.getMoveListStringsGrouped()); // ---- HIER ist die Matt/Patt/Remis-Prüfung ----
} else { if (engine.isMated()) {
gui.displayMessage("Ungültiger Zug!"); String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
} else if (engine.isStalemate() || engine.isDraw()) {
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
} }
} }
public void updateGuiBoard() { public void updateGuiBoard() {
BoardDTO board = engine.getBoardAsDTO(); BoardDTO board = engine.getBoardAsDTO();
gui.updateBoard(board); gui.updateBoard(board);
@ -179,10 +189,13 @@ public class GameController {
private void resetFieldBackground(int row, int col) { private void resetFieldBackground(int row, int col) {
Color LIGHT = new Color(0xe0e1dd);
Color DARK = new Color(0x778da9);
if ((row + col) % 2 == 0) { if ((row + col) % 2 == 0) {
gui.getField(row, col).setBackground(new Color(0x778da9)); gui.getField(row, col).setBackground(LIGHT);
} else { } else {
gui.getField(row, col).setBackground(new Color(0xe0e1dd)); gui.getField(row, col).setBackground(DARK);
} }
} }
} }

View File

@ -33,6 +33,9 @@ public class GameGui {
JButton btnNext = new JButton(">"); JButton btnNext = new JButton(">");
JButton btnLast = new JButton(">|"); JButton btnLast = new JButton(">|");
Color LIGHT = new Color(0xe0e1dd);
Color DARK = new Color(0x778da9);
private JPanel moveListPanel; private JPanel moveListPanel;
private JScrollPane moveListScroll; private JScrollPane moveListScroll;
@ -54,20 +57,21 @@ public class GameGui {
} }
private void initFields() { private void initFields() {
for (int row = 0; row < 8; row++) { for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) { for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER); JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true); label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 40)); label.setFont(new Font("Serif", Font.BOLD, 40));
if ((row + col) % 2 == 0) { // Richtige Schachfärbung:
label.setBackground(new Color(0x778da9)); if ((row + col) % 2 == 0) {
} else { label.setBackground(LIGHT); // a1 ist jetzt hell!
label.setBackground(new Color(0xe0e1dd)); } else {
} label.setBackground(DARK);
fields[row][col] = label; }
} fields[row][col] = label;
} }
} }
}
public JPanel mainPanel() { public JPanel mainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout()); JPanel mainPanel = new JPanel(new GridBagLayout());
@ -97,29 +101,27 @@ public class GameGui {
} }
public JPanel boardPanel() { public JPanel boardPanel() {
JPanel boardPanel = new JPanel(new GridLayout(8, 8)); JPanel boardPanel = new JPanel(new GridLayout(8, 8));
boardPanel.setPreferredSize(new Dimension(800, 800)); boardPanel.setPreferredSize(new Dimension(800, 800));
for (int row = 0; row < 8; row++) { for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
for (int col = 0; col < 8; col++) { JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
JLabel label = new JLabel("", SwingConstants.CENTER); label.setFont(new Font("Serif", Font.BOLD, 70));
label.setOpaque(true); // Richtige Schachfärbung:
label.setFont(new Font("Serif", Font.BOLD, 70)); if ((row + col) % 2 == 0) {
if ((row + col) % 2 == 0) { label.setBackground(LIGHT);
label.setBackground(new Color(0x778da9)); } else {
} else { label.setBackground(DARK);
label.setBackground(new Color(0xe0e1dd)); }
} fields[row][col] = label;
fields[row][col] = label; boardPanel.add(label);
boardPanel.add(label); }
}
} boardPanel.setBackground(new Color(0x1b263b));
} return boardPanel;
boardPanel.setBackground(new Color(0x1b263b)); }
return boardPanel;
}
public JPanel chessPanel(JPanel panel) { public JPanel chessPanel(JPanel panel) {
JPanel chessPanel = new JPanel(new GridBagLayout()); JPanel chessPanel = new JPanel(new GridBagLayout());