Compare commits

..

No commits in common. "5ae9aef60550bf74f857f7cd9edcd1a2b00b5dbe" and "08fc96187b5c19468b82487bbfc6b7fd7a5a7fbd" have entirely different histories.

3 changed files with 35 additions and 144 deletions

View File

@ -10,7 +10,6 @@ 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;
@ -27,13 +26,6 @@ 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++) {
@ -45,82 +37,48 @@ 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 guiRow, int guiCol) {
int modelRow = flipRow(guiRow);
int modelCol = flipCol(guiCol);
//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"
private void handleClick(int row, int col) {
if (selectedRow == -1 && selectedCol == -1) {
if (piece == null || !piece.getColor().equals(amZug)) {
// Falsche Farbe oder leeres Feld -> abbrechen, keine Highlights!
return;
}
// Erstes Feld gewählt
selectedRow = row;
selectedCol = col;
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
selectedRow = modelRow;
selectedCol = modelCol;
gui.getField(guiRow, guiCol).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
String fromSquare = coordToChessNotation(modelRow, modelCol);
// Mögliche Ziele highlighten (DTOs!)
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
for (MoveDTO move : moves) {
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});
int toRow = move.getToRow();
int toCol = move.getToCol();
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
highlightedFields.add(new int[]{toRow, toCol});
}
} 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);
MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
gui.getField(selectedRow, selectedCol).setBorder(null);
// **Neues MoveDTO statt String-Kram!**
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
handleMove(move);
selectedRow = -1;
selectedCol = -1;
}
}
public void handleMove(MoveDTO move) {
if (engine.move(move)) {
updateGuiBoard();
//Spielstatus prüfen
// Jetzt: Spielstatus prüfen!
if (engine.isMated()) {
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
@ -134,17 +92,16 @@ public class Controller {
public void updateGuiBoard() {
BoardDTO board = engine.getBoardAsDTO();
gui.updateBoard(board); // Passe die GUI an
gui.updateBoard(board); // Passe die GUI an, damit sie ein BoardDTO nimmt!
}
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
private String coordToChessNotation(int modelRow, int modelCol) {
char file = (char)('A' + modelCol);
int rank = 8 - modelRow;
private String coordToChessNotation(int x, int y) {
char file = (char)('A' + y);
int rank = 8 - x;
return "" + file + rank;
}
// ... resetFieldBackground wie gehabt ...
private void resetFieldBackground(int row, int col) {
if ((row + col) % 2 == 0) {

View File

@ -1,27 +0,0 @@
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,7 +8,6 @@ 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;
@ -21,8 +20,6 @@ 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();
@ -104,24 +101,6 @@ 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;
}
@ -135,33 +114,15 @@ 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();
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() : "");
}
}
if (piece != null) {
fields[row][col].setText(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() : "");
fields[row][col].setText("");
}
}
}