Removed unused methods in PgnController
parent
0f5e656614
commit
0d37de1cd9
|
@ -1,24 +1,15 @@
|
||||||
package de.hs_mannheim.informatik.chess.controller;
|
package de.hs_mannheim.informatik.chess.controller;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.event.MouseAdapter;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
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.PieceDTO;
|
|
||||||
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
||||||
import de.hs_mannheim.informatik.chess.view.PgnGui;
|
import de.hs_mannheim.informatik.chess.view.PgnGui;
|
||||||
|
|
||||||
public class PgnController {
|
public class PgnController {
|
||||||
PgnGui gui;
|
PgnGui gui;
|
||||||
ChessEngine engine;
|
ChessEngine engine;
|
||||||
private int selectedRow = -1, selectedCol = -1;
|
|
||||||
private List<int[]> highlightedFields = new ArrayList<>();
|
|
||||||
|
|
||||||
public PgnController(PgnGui pgngui, ChessEngine engine) {
|
public PgnController(PgnGui pgngui, ChessEngine engine) {
|
||||||
this.gui = pgngui;
|
this.gui = pgngui;
|
||||||
|
@ -27,25 +18,7 @@ public class PgnController {
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int flipRow(int row) {
|
private void initListeners() {
|
||||||
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++) {
|
|
||||||
final int r = row, c = col;
|
|
||||||
gui.getField(row, col).addMouseListener(new MouseAdapter() {
|
|
||||||
public void mousePressed(MouseEvent e) {
|
|
||||||
handleClick(r, c);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Erster Zug
|
// Erster Zug
|
||||||
gui.getBtnFirst().addActionListener(e -> {
|
gui.getBtnFirst().addActionListener(e -> {
|
||||||
engine.setPositionToMoveIndex(0);
|
engine.setPositionToMoveIndex(0);
|
||||||
|
@ -78,9 +51,6 @@ public class PgnController {
|
||||||
gui.getField(row, col).setBorder(null);
|
gui.getField(row, col).setBorder(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
highlightedFields.clear();
|
|
||||||
selectedRow = -1;
|
|
||||||
selectedCol = -1;
|
|
||||||
|
|
||||||
// 2. Flip-Zustand ändern
|
// 2. Flip-Zustand ändern
|
||||||
gui.setFlipped(!gui.isFlipped());
|
gui.setFlipped(!gui.isFlipped());
|
||||||
|
@ -91,103 +61,12 @@ public class PgnController {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
|
||||||
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(new Color(0x1b263b), 7));
|
|
||||||
|
|
||||||
String fromSquare = coordToChessNotation(modelRow, modelCol);
|
|
||||||
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(new Color( 27, 38, 59 ));
|
|
||||||
highlightedFields.add(new int[]{guiToRow, guiToCol});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
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);
|
|
||||||
handleMove(move);
|
|
||||||
selectedRow = -1;
|
|
||||||
selectedCol = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleMove(MoveDTO move) {
|
|
||||||
BoardDTO boardDTO = engine.getBoardAsDTO();
|
|
||||||
PieceDTO piece = boardDTO.getBoard()[move.getFromRow()][move.getFromCol()];
|
|
||||||
boolean isPawn = piece != null && piece.getType().equals("PAWN");
|
|
||||||
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);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
public void updateGuiBoard() {
|
||||||
BoardDTO board = engine.getBoardAsDTO();
|
BoardDTO board = engine.getBoardAsDTO();
|
||||||
gui.updateBoard(board);
|
gui.updateBoard(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
return "" + file + rank;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void resetFieldBackground(int row, int col) {
|
private void resetFieldBackground(int row, int col) {
|
||||||
Color LIGHT = new Color(0xe0e1dd);
|
Color LIGHT = new Color(0xe0e1dd);
|
||||||
Color DARK = new Color(0x778da9);
|
Color DARK = new Color(0x778da9);
|
||||||
|
|
Loading…
Reference in New Issue