Fully implemented CreativeController class
parent
5d9f4e2b8e
commit
a36fb3b69d
|
@ -1,5 +1,158 @@
|
||||||
package de.hs_mannheim.informatik.chess.controller;
|
package de.hs_mannheim.informatik.chess.controller;
|
||||||
|
|
||||||
public class CreativeController {
|
import de.hs_mannheim.informatik.chess.view.CreativeGui;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CreativeController {
|
||||||
|
private CreativeGui gui;
|
||||||
|
private ChessEngine engine;
|
||||||
|
|
||||||
|
// Hilfsmap für Unicode zu Chesslib Piece FEN-Zeichen
|
||||||
|
private static final HashMap<String, Character> PIECE_TO_FEN = createPieceToFenMap();
|
||||||
|
|
||||||
|
private static HashMap<String, Character> createPieceToFenMap() {
|
||||||
|
HashMap<String, Character> map = new HashMap<String, Character>();
|
||||||
|
map.put("BLACK_KING", 'k');
|
||||||
|
map.put("BLACK_QUEEN", 'q');
|
||||||
|
map.put("BLACK_ROOK", 'r');
|
||||||
|
map.put("BLACK_BISHOP", 'b');
|
||||||
|
map.put("BLACK_KNIGHT", 'n');
|
||||||
|
map.put("BLACK_PAWN", 'p');
|
||||||
|
map.put("WHITE_KING", 'K');
|
||||||
|
map.put("WHITE_QUEEN", 'Q');
|
||||||
|
map.put("WHITE_ROOK", 'R');
|
||||||
|
map.put("WHITE_BISHOP", 'B');
|
||||||
|
map.put("WHITE_KNIGHT", 'N');
|
||||||
|
map.put("WHITE_PAWN", 'P');
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CreativeController(CreativeGui gui, ChessEngine engine) {
|
||||||
|
this.gui = gui;
|
||||||
|
this.engine = engine;
|
||||||
|
|
||||||
|
setupFieldListeners();
|
||||||
|
setupFenUpdateListener();
|
||||||
|
gui.setFenText(boardToFen());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupFieldListeners() {
|
||||||
|
JLabel[][] fields = gui.getFields();
|
||||||
|
for (int row = 0; row < 8; row++) {
|
||||||
|
for (int col = 0; col < 8; col++) {
|
||||||
|
final int r = row, c = col;
|
||||||
|
for (MouseListener l : fields[r][c].getMouseListeners()) {
|
||||||
|
fields[r][c].removeMouseListener(l);
|
||||||
|
}
|
||||||
|
fields[r][c].addMouseListener(new java.awt.event.MouseAdapter() {
|
||||||
|
public void mousePressed(java.awt.event.MouseEvent evt) {
|
||||||
|
fieldClicked(r, c);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fieldClicked(int row, int col) {
|
||||||
|
// Nach jedem Klick: FEN aktualisieren
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
String fen = boardToFen();
|
||||||
|
gui.setFenText(fen);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupFenUpdateListener() {
|
||||||
|
//Getter für das Feld in deiner Gui:
|
||||||
|
JTextField fenField = gui.getFenField();
|
||||||
|
if (fenField != null) {
|
||||||
|
fenField.addActionListener(new java.awt.event.ActionListener() {
|
||||||
|
public void actionPerformed(java.awt.event.ActionEvent e) {
|
||||||
|
applyFenToBoard();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
JButton updateBtn = gui.getUpdateButton();
|
||||||
|
if (updateBtn != null) {
|
||||||
|
updateBtn.addActionListener(new java.awt.event.ActionListener() {
|
||||||
|
public void actionPerformed(java.awt.event.ActionEvent e) {
|
||||||
|
applyFenToBoard();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void applyFenToBoard() {
|
||||||
|
String fen = gui.getFenText();
|
||||||
|
try {
|
||||||
|
engine.setPositionFromFEN(fen);
|
||||||
|
updateGuiFromEngine();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Ungültiges FEN: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Board aus der GUI zu FEN String
|
||||||
|
private String boardToFen() {
|
||||||
|
JLabel[][] fields = gui.getFields();
|
||||||
|
StringBuilder fen = new StringBuilder();
|
||||||
|
for (int row = 0; row < 8; row++) {
|
||||||
|
int empty = 0;
|
||||||
|
for (int col = 0; col < 8; col++) {
|
||||||
|
String symbol = fields[row][col].getText();
|
||||||
|
String pieceKey = null;
|
||||||
|
for (Map.Entry<String, String> entry : CreativeGui.UNICODE_MAP.entrySet()) {
|
||||||
|
if (entry.getValue().equals(symbol)) {
|
||||||
|
pieceKey = entry.getKey();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pieceKey == null || "ERASER".equals(pieceKey) || symbol.trim().isEmpty()) {
|
||||||
|
empty++;
|
||||||
|
} else {
|
||||||
|
if (empty > 0) {
|
||||||
|
fen.append(empty);
|
||||||
|
empty = 0;
|
||||||
|
}
|
||||||
|
Character fenChar = PIECE_TO_FEN.get(pieceKey);
|
||||||
|
if (fenChar != null) fen.append(fenChar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (empty > 0) fen.append(empty);
|
||||||
|
if (row < 7) fen.append('/');
|
||||||
|
}
|
||||||
|
fen.append(" w - - 0 1");
|
||||||
|
return fen.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateGuiFromEngine() {
|
||||||
|
// Lies das BoardDTO und update die Felder
|
||||||
|
PieceDTO[][] board = engine.getBoardAsDTO().getBoard();
|
||||||
|
JLabel[][] fields = gui.getFields();
|
||||||
|
|
||||||
|
for (int row = 0; row < 8; row++) {
|
||||||
|
for (int col = 0; col < 8; col++) {
|
||||||
|
PieceDTO p = board[row][col];
|
||||||
|
if (p == null) {
|
||||||
|
fields[row][col].setText("");
|
||||||
|
} else {
|
||||||
|
// Suche Symbol-String in UNICODE_MAP
|
||||||
|
for (Map.Entry<String, String> entry : CreativeGui.UNICODE_MAP.entrySet()) {
|
||||||
|
if (entry.getKey().startsWith(p.getColor()) && entry.getKey().endsWith(p.getType())) {
|
||||||
|
fields[row][col].setText(entry.getValue());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue