Compare commits
No commits in common. "5ae9aef60550bf74f857f7cd9edcd1a2b00b5dbe" and "08fc96187b5c19468b82487bbfc6b7fd7a5a7fbd" have entirely different histories.
5ae9aef605
...
08fc96187b
|
|
@ -10,7 +10,6 @@ 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;
|
||||||
|
|
||||||
|
|
@ -27,13 +26,6 @@ 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++) {
|
||||||
|
|
@ -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) {
|
private void handleClick(int row, int col) {
|
||||||
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"
|
|
||||||
if (selectedRow == -1 && selectedCol == -1) {
|
if (selectedRow == -1 && selectedCol == -1) {
|
||||||
if (piece == null || !piece.getColor().equals(amZug)) {
|
// Erstes Feld gewählt
|
||||||
// Falsche Farbe oder leeres Feld -> abbrechen, keine Highlights!
|
selectedRow = row;
|
||||||
return;
|
selectedCol = col;
|
||||||
}
|
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||||
|
|
||||||
selectedRow = modelRow;
|
// Mögliche Ziele highlighten (DTOs!)
|
||||||
selectedCol = modelCol;
|
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||||
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 guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
|
int toRow = move.getToRow();
|
||||||
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
|
int toCol = move.getToCol();
|
||||||
gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
|
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||||
highlightedFields.add(new int[]{guiToRow, guiToCol});
|
highlightedFields.add(new int[]{toRow, toCol});
|
||||||
}
|
}
|
||||||
} 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);
|
|
||||||
|
|
||||||
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);
|
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();
|
||||||
|
|
||||||
//Spielstatus prüfen
|
// Jetzt: 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)!");
|
||||||
|
|
@ -134,17 +92,16 @@ public class Controller {
|
||||||
|
|
||||||
public void updateGuiBoard() {
|
public void updateGuiBoard() {
|
||||||
BoardDTO board = engine.getBoardAsDTO();
|
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
|
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
||||||
private String coordToChessNotation(int modelRow, int modelCol) {
|
private String coordToChessNotation(int x, int y) {
|
||||||
char file = (char)('A' + modelCol);
|
char file = (char)('A' + y);
|
||||||
int rank = 8 - modelRow;
|
int rank = 8 - x;
|
||||||
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) {
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,6 @@ 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;
|
||||||
|
|
@ -21,8 +20,6 @@ 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();
|
||||||
|
|
@ -104,24 +101,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,33 +114,15 @@ 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];
|
||||||
fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : "");
|
if (piece != null) {
|
||||||
}
|
fields[row][col].setText(piece.getUnicodeSymbol());
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Invertiere Zeilen und Spalten
|
fields[row][col].setText("");
|
||||||
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() : "");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue