Compare commits
11 Commits
08fc96187b
...
5ae9aef605
| Author | SHA1 | Date |
|---|---|---|
|
|
5ae9aef605 | |
|
|
11cede7678 | |
|
|
93570f2c1c | |
|
|
0e417bdca6 | |
|
|
7da371cd9b | |
|
|
994f03067c | |
|
|
d577d6843f | |
|
|
1167b54260 | |
|
|
241a532327 | |
|
|
f6cfc8fd9b | |
|
|
c0bff9e7c6 |
|
|
@ -10,6 +10,7 @@ import javax.swing.BorderFactory;
|
|||
|
||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||
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.view.GameGui;
|
||||
|
||||
|
|
@ -26,6 +27,13 @@ public class Controller {
|
|||
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() {
|
||||
for (int row = 0; row < 8; row++) {
|
||||
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) {
|
||||
if (selectedRow == -1 && selectedCol == -1) {
|
||||
// Erstes Feld gewählt
|
||||
selectedRow = row;
|
||||
selectedCol = col;
|
||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||
private void handleClick(int guiRow, int guiCol) {
|
||||
int modelRow = flipRow(guiRow);
|
||||
int modelCol = flipCol(guiCol);
|
||||
|
||||
// Mögliche Ziele highlighten (DTOs!)
|
||||
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||
//Figur am Feld
|
||||
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);
|
||||
|
||||
for (MoveDTO move : moves) {
|
||||
int toRow = move.getToRow();
|
||||
int toCol = move.getToCol();
|
||||
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(new int[]{toRow, toCol});
|
||||
int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
|
||||
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
|
||||
gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(new int[]{guiToRow, guiToCol});
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
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);
|
||||
|
||||
// **Neues MoveDTO statt String-Kram!**
|
||||
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
||||
MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
|
||||
handleMove(move);
|
||||
|
||||
selectedRow = -1;
|
||||
selectedCol = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void handleMove(MoveDTO move) {
|
||||
if (engine.move(move)) {
|
||||
updateGuiBoard();
|
||||
|
||||
// Jetzt: Spielstatus prüfen!
|
||||
//Spielstatus prüfen
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
|
|
@ -92,16 +134,17 @@ public class Controller {
|
|||
|
||||
public void updateGuiBoard() {
|
||||
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
|
||||
private String coordToChessNotation(int x, int y) {
|
||||
char file = (char)('A' + y);
|
||||
int rank = 8 - x;
|
||||
private String coordToChessNotation(int modelRow, int modelCol) {
|
||||
char file = (char)('A' + modelCol);
|
||||
int rank = 8 - modelRow;
|
||||
return "" + file + rank;
|
||||
}
|
||||
|
||||
|
||||
// ... resetFieldBackground wie gehabt ...
|
||||
private void resetFieldBackground(int row, int col) {
|
||||
if ((row + col) % 2 == 0) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import java.awt.GridBagLayout;
|
|||
import java.awt.GridLayout;
|
||||
import java.awt.Insets;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
|
|
@ -20,6 +21,8 @@ import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
|||
public class GameGui {
|
||||
|
||||
private JLabel[][] fields = new JLabel[8][8];
|
||||
private JButton flipBoardButton;
|
||||
private boolean isFlipped = false;
|
||||
|
||||
public GameGui(){
|
||||
mainFrame();
|
||||
|
|
@ -101,6 +104,24 @@ public class GameGui {
|
|||
//oben, links, unten, rechts
|
||||
board.fill = GridBagConstraints.BOTH;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -114,19 +135,37 @@ public class GameGui {
|
|||
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) {
|
||||
PieceDTO[][] board = boardDTO.getBoard();
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
PieceDTO piece = board[row][col];
|
||||
if (piece != null) {
|
||||
fields[row][col].setText(piece.getUnicodeSymbol());
|
||||
} else {
|
||||
fields[row][col].setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PieceDTO[][] board = boardDTO.getBoard();
|
||||
if (!isFlipped) {
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
PieceDTO piece = board[row][col];
|
||||
fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : "");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 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() : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void displayMessage(String msg) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue