Compare commits

...

11 Commits

Author SHA1 Message Date
Justin 5ae9aef605 Better looking flipButton 2025-06-19 22:56:25 +02:00
Justin 11cede7678 Updated code comments for better clarity 2025-06-19 22:32:57 +02:00
Justin 93570f2c1c Board flip feature implemented with proper highlight mapping 2025-06-19 22:28:10 +02:00
Justin 0e417bdca6 Fixed flip board feature 2025-06-19 21:57:17 +02:00
Justin 7da371cd9b Fully implemented the flip feature in Controller 2025-06-19 21:30:18 +02:00
Justin 994f03067c Setter for boolean isFlipped in GameGui 2025-06-19 21:27:38 +02:00
Justin d577d6843f Changed updateBoard method for board flip feature 2025-06-19 21:26:13 +02:00
Justin 1167b54260 New flip button in GameGui 2025-06-19 21:24:35 +02:00
Justin 241a532327 Added getter/setter to PlayerDTO 2025-06-19 21:14:45 +02:00
Justin f6cfc8fd9b Added constructor to PlayerDTO 2025-06-19 21:08:06 +02:00
Justin c0bff9e7c6 New class PlayerDTO 2025-06-19 21:05:53 +02:00
3 changed files with 145 additions and 36 deletions

View File

@ -10,6 +10,7 @@ import javax.swing.BorderFactory;
import de.hs_mannheim.informatik.chess.model.ChessEngine; import de.hs_mannheim.informatik.chess.model.ChessEngine;
import de.hs_mannheim.informatik.chess.model.MoveDTO; import de.hs_mannheim.informatik.chess.model.MoveDTO;
import de.hs_mannheim.informatik.chess.model.PieceDTO;
import de.hs_mannheim.informatik.chess.model.BoardDTO; import de.hs_mannheim.informatik.chess.model.BoardDTO;
import de.hs_mannheim.informatik.chess.view.GameGui; import de.hs_mannheim.informatik.chess.view.GameGui;
@ -26,6 +27,13 @@ public class Controller {
updateGuiBoard(); updateGuiBoard();
} }
private int flipRow(int row) {
return gui.isFlipped() ? 7 - row : row;
}
private int flipCol(int col) {
return gui.isFlipped() ? 7 - col : col;
}
private void initListeners() { private void initListeners() {
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++) {
@ -37,48 +45,82 @@ public class Controller {
}); });
} }
} }
gui.getFlipBoardButton().addActionListener(e -> {
//ALLE Highlights und Borders zurücksetzen
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
resetFieldBackground(row, col);
gui.getField(row, col).setBorder(null);
}
}
highlightedFields.clear();
selectedRow = -1;
selectedCol = -1;
// 2. Flip-Zustand ändern
gui.setFlipped(!gui.isFlipped());
// 3. Board neu zeichnen
updateGuiBoard();
});
} }
private void handleClick(int row, int col) { private void handleClick(int guiRow, int guiCol) {
if (selectedRow == -1 && selectedCol == -1) { int modelRow = flipRow(guiRow);
// Erstes Feld gewählt int modelCol = flipCol(guiCol);
selectedRow = row;
selectedCol = col;
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
// Mögliche Ziele highlighten (DTOs!) //Figur am Feld
String fromSquare = coordToChessNotation(row, col); // z.B. "E2" BoardDTO boardDTO = engine.getBoardAsDTO();
PieceDTO piece = boardDTO.getBoard()[modelRow][modelCol];
//Ist eine Figur da und hat sie die aktuelle Farbe
String amZug = engine.getCurrentPlayer(); // "WHITE" oder "BLACK"
if (selectedRow == -1 && selectedCol == -1) {
if (piece == null || !piece.getColor().equals(amZug)) {
// Falsche Farbe oder leeres Feld -> abbrechen, keine Highlights!
return;
}
selectedRow = modelRow;
selectedCol = modelCol;
gui.getField(guiRow, guiCol).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
String fromSquare = coordToChessNotation(modelRow, modelCol);
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare); List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
for (MoveDTO move : moves) { for (MoveDTO move : moves) {
int toRow = move.getToRow(); int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
int toCol = move.getToCol(); int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
gui.getField(toRow, toCol).setBackground(Color.YELLOW); gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
highlightedFields.add(new int[]{toRow, toCol}); highlightedFields.add(new int[]{guiToRow, guiToCol});
} }
} else { } else {
// Zweites Feld: Zug probieren
// Highlight entfernen
for (int[] xy : highlightedFields) { for (int[] xy : highlightedFields) {
resetFieldBackground(xy[0], xy[1]); resetFieldBackground(xy[0], xy[1]);
} }
highlightedFields.clear(); highlightedFields.clear();
int oldGuiRow = gui.isFlipped() ? 7 - selectedRow : selectedRow;
int oldGuiCol = gui.isFlipped() ? 7 - selectedCol : selectedCol;
gui.getField(oldGuiRow, oldGuiCol).setBorder(null);
gui.getField(selectedRow, selectedCol).setBorder(null); MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
// **Neues MoveDTO statt String-Kram!**
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
handleMove(move); handleMove(move);
selectedRow = -1; selectedRow = -1;
selectedCol = -1; selectedCol = -1;
} }
} }
public void handleMove(MoveDTO move) { public void handleMove(MoveDTO move) {
if (engine.move(move)) { if (engine.move(move)) {
updateGuiBoard(); updateGuiBoard();
// Jetzt: Spielstatus prüfen! //Spielstatus prüfen
if (engine.isMated()) { if (engine.isMated()) {
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß"; String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!"); gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
@ -92,16 +134,17 @@ public class Controller {
public void updateGuiBoard() { public void updateGuiBoard() {
BoardDTO board = engine.getBoardAsDTO(); BoardDTO board = engine.getBoardAsDTO();
gui.updateBoard(board); // Passe die GUI an, damit sie ein BoardDTO nimmt! gui.updateBoard(board); // Passe die GUI an
} }
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen // Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
private String coordToChessNotation(int x, int y) { private String coordToChessNotation(int modelRow, int modelCol) {
char file = (char)('A' + y); char file = (char)('A' + modelCol);
int rank = 8 - x; int rank = 8 - modelRow;
return "" + file + rank; return "" + file + rank;
} }
// ... resetFieldBackground wie gehabt ... // ... resetFieldBackground wie gehabt ...
private void resetFieldBackground(int row, int col) { private void resetFieldBackground(int row, int col) {
if ((row + col) % 2 == 0) { if ((row + col) % 2 == 0) {

View File

@ -0,0 +1,27 @@
package de.hs_mannheim.informatik.chess.model;
public class PlayerDTO {
private String name;
private String color; // "WHITE" oder "BLACK"
public PlayerDTO(String name, String color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public void setName(String name) {
this.name = name;
}
public void setColor(String color) {
this.color = color;
}
}

View File

@ -8,6 +8,7 @@ import java.awt.GridBagLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Insets; import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -20,6 +21,8 @@ import de.hs_mannheim.informatik.chess.model.PieceDTO;
public class GameGui { public class GameGui {
private JLabel[][] fields = new JLabel[8][8]; private JLabel[][] fields = new JLabel[8][8];
private JButton flipBoardButton;
private boolean isFlipped = false;
public GameGui(){ public GameGui(){
mainFrame(); mainFrame();
@ -101,6 +104,24 @@ public class GameGui {
//oben, links, unten, rechts //oben, links, unten, rechts
board.fill = GridBagConstraints.BOTH; board.fill = GridBagConstraints.BOTH;
chessPanel.add(panel); chessPanel.add(panel);
// Button unten rechts
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);
GridBagConstraints btn = new GridBagConstraints();
btn.gridx = 0;
btn.gridy = 1;
btn.weightx = 0.0;
btn.weighty = 0.0;
btn.anchor = GridBagConstraints.SOUTHEAST;
btn.insets = new Insets(10, 0, 0, 0);
chessPanel.add(flipBoardButton, btn);
return chessPanel; return chessPanel;
} }
@ -114,15 +135,33 @@ public class GameGui {
return fields[row][col]; return fields[row][col];
} }
public JButton getFlipBoardButton() {
return flipBoardButton;
}
public boolean isFlipped() {
return isFlipped;
}
public void setFlipped(boolean flipped) {
this.isFlipped = flipped;
}
public void updateBoard(BoardDTO boardDTO) { public void updateBoard(BoardDTO boardDTO) {
PieceDTO[][] board = boardDTO.getBoard(); PieceDTO[][] board = boardDTO.getBoard();
if (!isFlipped) {
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++) {
PieceDTO piece = board[row][col]; PieceDTO piece = board[row][col];
if (piece != null) { fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : "");
fields[row][col].setText(piece.getUnicodeSymbol()); }
}
} else { } else {
fields[row][col].setText(""); // Invertiere Zeilen und Spalten
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
PieceDTO piece = board[7 - row][7 - col];
fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : "");
} }
} }
} }