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 isBlackPromotion = isPawn && piece.getColor().equals("BLACK") && move.getToRow() == 7;
boolean success = false;
if (isWhitePromotion || isBlackPromotion) {
String color = piece.getColor().equals("WHITE") ? "Weiß" : "Schwarz";
String promotion = gui.showPromotionDialog(color);
if (engine.moveWithPromotion(move, promotion)) {
updateGuiBoard();
gui.updateMoveList(engine.getMoveListStringsGrouped());
} else {
success = engine.moveWithPromotion(move, promotion);
if (!success) {
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:
if (engine.move(move)) {
updateGuiBoard();
gui.updateMoveList(engine.getMoveListStringsGrouped());
} else {
gui.displayMessage("Ungültiger Zug!");
updateGuiBoard();
gui.updateMoveList(engine.getMoveListStringsGrouped());
// ---- HIER ist die Matt/Patt/Remis-Prüfung ----
if (engine.isMated()) {
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() {
BoardDTO board = engine.getBoardAsDTO();
gui.updateBoard(board);
@ -179,10 +189,13 @@ public class GameController {
private void resetFieldBackground(int row, int col) {
Color LIGHT = new Color(0xe0e1dd);
Color DARK = new Color(0x778da9);
if ((row + col) % 2 == 0) {
gui.getField(row, col).setBackground(new Color(0x778da9));
gui.getField(row, col).setBackground(LIGHT);
} 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 btnLast = new JButton(">|");
Color LIGHT = new Color(0xe0e1dd);
Color DARK = new Color(0x778da9);
private JPanel moveListPanel;
private JScrollPane moveListScroll;
@ -54,20 +57,21 @@ public class GameGui {
}
private void initFields() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 40));
if ((row + col) % 2 == 0) {
label.setBackground(new Color(0x778da9));
} else {
label.setBackground(new Color(0xe0e1dd));
}
fields[row][col] = label;
}
}
}
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 40));
// Richtige Schachfärbung:
if ((row + col) % 2 == 0) {
label.setBackground(LIGHT); // a1 ist jetzt hell!
} else {
label.setBackground(DARK);
}
fields[row][col] = label;
}
}
}
public JPanel mainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
@ -97,29 +101,27 @@ public class GameGui {
}
public JPanel boardPanel() {
JPanel boardPanel = new JPanel(new GridLayout(8, 8));
boardPanel.setPreferredSize(new Dimension(800, 800));
JPanel boardPanel = new JPanel(new GridLayout(8, 8));
boardPanel.setPreferredSize(new Dimension(800, 800));
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 70));
if ((row + col) % 2 == 0) {
label.setBackground(new Color(0x778da9));
} else {
label.setBackground(new Color(0xe0e1dd));
}
fields[row][col] = label;
boardPanel.add(label);
}
}
boardPanel.setBackground(new Color(0x1b263b));
return boardPanel;
}
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 70));
// Richtige Schachfärbung:
if ((row + col) % 2 == 0) {
label.setBackground(LIGHT);
} else {
label.setBackground(DARK);
}
fields[row][col] = label;
boardPanel.add(label);
}
}
boardPanel.setBackground(new Color(0x1b263b));
return boardPanel;
}
public JPanel chessPanel(JPanel panel) {
JPanel chessPanel = new JPanel(new GridBagLayout());