Compare commits
No commits in common. "1232345b000161cff1bf64ecda5dac2450e03f4f" and "e6e1fa31873377a15342e47a253f38995d0b8cad" have entirely different histories.
1232345b00
...
e6e1fa3187
|
|
@ -46,30 +46,6 @@ public class Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erster Zug
|
|
||||||
gui.getBtnFirst().addActionListener(e -> {
|
|
||||||
engine.setPositionToMoveIndex(0);
|
|
||||||
updateGuiBoard();
|
|
||||||
});
|
|
||||||
// Ein Zug zurück
|
|
||||||
gui.getBtnPrev().addActionListener(e -> {
|
|
||||||
int idx = Math.max(0, engine.getCurrentMoveIndex() - 1);
|
|
||||||
engine.setPositionToMoveIndex(idx);
|
|
||||||
updateGuiBoard();
|
|
||||||
});
|
|
||||||
// Ein Zug vor
|
|
||||||
gui.getBtnNext().addActionListener(e -> {
|
|
||||||
int idx = Math.min(engine.getMoveListSize(), engine.getCurrentMoveIndex() + 1);
|
|
||||||
engine.setPositionToMoveIndex(idx);
|
|
||||||
updateGuiBoard();
|
|
||||||
});
|
|
||||||
// Letzter Zug
|
|
||||||
gui.getBtnLast().addActionListener(e -> {
|
|
||||||
engine.setPositionToMoveIndex(engine.getMoveListSize());
|
|
||||||
updateGuiBoard();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
gui.getFlipBoardButton().addActionListener(e -> {
|
gui.getFlipBoardButton().addActionListener(e -> {
|
||||||
//ALLE Highlights und Borders zurücksetzen
|
//ALLE Highlights und Borders zurücksetzen
|
||||||
for (int row = 0; row < 8; row++) {
|
for (int row = 0; row < 8; row++) {
|
||||||
|
|
@ -158,7 +134,7 @@ public class Controller {
|
||||||
|
|
||||||
public void updateGuiBoard() {
|
public void updateGuiBoard() {
|
||||||
BoardDTO board = engine.getBoardAsDTO();
|
BoardDTO board = engine.getBoardAsDTO();
|
||||||
gui.updateBoard(board);
|
gui.updateBoard(board); // Passe die GUI an
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
||||||
|
|
@ -169,6 +145,7 @@ public class Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ... 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) {
|
||||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
package de.hs_mannheim.informatik.chess.model;
|
package de.hs_mannheim.informatik.chess.model;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.ConsoleHandler;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.LogRecord;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.logging.SimpleFormatter;
|
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
import com.github.bhlangonijr.chesslib.Piece;
|
import com.github.bhlangonijr.chesslib.Piece;
|
||||||
|
|
@ -15,12 +10,8 @@ import com.github.bhlangonijr.chesslib.move.Move;
|
||||||
public class ChessEngine {
|
public class ChessEngine {
|
||||||
private Board board;
|
private Board board;
|
||||||
private List<Move> moves = new ArrayList<>();
|
private List<Move> moves = new ArrayList<>();
|
||||||
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
|
|
||||||
|
|
||||||
private int currentMoveIndex = 0;
|
|
||||||
|
|
||||||
public ChessEngine() {
|
public ChessEngine() {
|
||||||
logging();
|
|
||||||
board = new Board();
|
board = new Board();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,44 +21,28 @@ public class ChessEngine {
|
||||||
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
||||||
if (board.legalMoves().contains(libMove)) {
|
if (board.legalMoves().contains(libMove)) {
|
||||||
board.doMove(libMove);
|
board.doMove(libMove);
|
||||||
|
moves.add(libMove); // <-- hier merken!
|
||||||
//Replay? Dann abschneiden
|
|
||||||
if (currentMoveIndex < moves.size()) {
|
|
||||||
logger.info("Replay-Modus: Züge nach " + currentMoveIndex + " werden entfernt.");
|
|
||||||
moves = new ArrayList<>(moves.subList(0, currentMoveIndex));
|
|
||||||
}
|
|
||||||
moves.add(libMove);
|
|
||||||
currentMoveIndex++;
|
|
||||||
logger.info("Zug erfolgreich durchgeführt: " + libMove);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
logger.warning("Ungültiger Zug: " + libMove);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<MoveDTO> getLegalDestinations(String from) {
|
public List<MoveDTO> getLegalDestinations(String from) {
|
||||||
logger.info("Hole legale Züge von: " + from);
|
|
||||||
List<MoveDTO> destinations = new ArrayList<>();
|
List<MoveDTO> destinations = new ArrayList<>();
|
||||||
try {
|
|
||||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||||
for (Move move : board.legalMoves()) {
|
for (Move move : board.legalMoves()) {
|
||||||
if (move.getFrom().equals(fromSq)) {
|
if (move.getFrom().equals(fromSq)) {
|
||||||
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
||||||
int fromCol = fromSq.getFile().ordinal();
|
int fromCol = fromSq.getFile().ordinal();
|
||||||
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
||||||
int toCol = move.getTo().getFile().ordinal();
|
int toCol = move.getTo().getFile().ordinal();;
|
||||||
destinations.add(new MoveDTO(fromRow, fromCol, toRow, toCol));
|
destinations.add(new MoveDTO(fromRow, fromCol, toRow, toCol));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info("Es wurden " + destinations.size() + " Ziele gefunden.");
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.severe("Fehler beim Holen der legalen Ziele: " + e.getMessage());
|
|
||||||
}
|
|
||||||
return destinations;
|
return destinations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getMoveListStringsGrouped() {
|
public List<String> getMoveListStringsGrouped() {
|
||||||
logger.info("Gruppiere Züge für Anzeige.");
|
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int i = 0; i < moves.size(); i++) {
|
for (int i = 0; i < moves.size(); i++) {
|
||||||
|
|
@ -82,13 +57,11 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public PieceDTO getPieceAt(String square) {
|
public PieceDTO getPieceAt(String square) {
|
||||||
logger.info("Hole Figur an Feld: " + square);
|
|
||||||
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
|
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
|
||||||
return convertPieceToDTO(piece);
|
return convertPieceToDTO(piece);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BoardDTO getBoardAsDTO() {
|
public BoardDTO getBoardAsDTO() {
|
||||||
logger.info("Erstelle DTO-Abbild des Boards");
|
|
||||||
PieceDTO[][] dtoBoard = new PieceDTO[8][8];
|
PieceDTO[][] dtoBoard = new PieceDTO[8][8];
|
||||||
for (int rank = 8; rank >= 1; rank--) {
|
for (int rank = 8; rank >= 1; rank--) {
|
||||||
for (int file = 0; file < 8; file++) {
|
for (int file = 0; file < 8; file++) {
|
||||||
|
|
@ -118,26 +91,6 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPositionToMoveIndex(int idx) {
|
|
||||||
// Neues Board erzeugen
|
|
||||||
logger.info("Setze Board auf Zug-Index: " + idx);
|
|
||||||
board = new Board();
|
|
||||||
for (int i = 0; i < idx; i++) {
|
|
||||||
board.doMove(moves.get(i));
|
|
||||||
}
|
|
||||||
currentMoveIndex = idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCurrentMoveIndex() {
|
|
||||||
logger.info("Hole aktuellen Zug-Index: " + currentMoveIndex);
|
|
||||||
return currentMoveIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMoveListSize() {
|
|
||||||
logger.info("Hole Anzahl gespielter Züge: " + moves.size());
|
|
||||||
return moves.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
private PieceDTO convertPieceToDTO(Piece piece) {
|
private PieceDTO convertPieceToDTO(Piece piece) {
|
||||||
if (piece == null || piece.equals(Piece.NONE)) return null;
|
if (piece == null || piece.equals(Piece.NONE)) return null;
|
||||||
String color = piece.name().startsWith("WHITE") ? "WHITE" : "BLACK";
|
String color = piece.name().startsWith("WHITE") ? "WHITE" : "BLACK";
|
||||||
|
|
@ -147,44 +100,19 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMated() {
|
public boolean isMated() {
|
||||||
boolean mated = board.isMated();
|
return board.isMated();
|
||||||
logger.info("isMated() = " + mated);
|
|
||||||
return mated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isStalemate() {
|
public boolean isStalemate() {
|
||||||
boolean stale = board.isStaleMate();
|
return board.isStaleMate();
|
||||||
logger.info("isStalemate() = " + stale);
|
|
||||||
return stale;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDraw() {
|
public boolean isDraw() {
|
||||||
boolean draw = board.isDraw();
|
return board.isDraw();
|
||||||
logger.info("isDraw() = " + draw);
|
|
||||||
return draw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentPlayer() {
|
public String getCurrentPlayer() {
|
||||||
String player = board.getSideToMove().toString();
|
return board.getSideToMove().toString(); // "WHITE" oder "BLACK"
|
||||||
logger.info("Am Zug: " + player);
|
|
||||||
return player;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void logging() {
|
|
||||||
|
|
||||||
// Eigener Handler nur für diese Klasse
|
|
||||||
ConsoleHandler handler = new ConsoleHandler();
|
|
||||||
handler.setLevel(Level.ALL);
|
|
||||||
handler.setFormatter(new SimpleFormatter() {
|
|
||||||
@Override
|
|
||||||
public synchronized String format(LogRecord lr) {
|
|
||||||
return String.format("[%s] %s%n%n", lr.getLevel().getLocalizedName(), lr.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
logger.setUseParentHandlers(false);
|
|
||||||
logger.addHandler(handler);
|
|
||||||
|
|
||||||
logger.info("ChessEngine wurde initialisiert.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,11 +28,6 @@ public class GameGui {
|
||||||
private JButton flipBoardButton;
|
private JButton flipBoardButton;
|
||||||
private boolean isFlipped = false;
|
private boolean isFlipped = false;
|
||||||
|
|
||||||
JButton btnFirst = new JButton("|<");
|
|
||||||
JButton btnPrev = new JButton("<");
|
|
||||||
JButton btnNext = new JButton(">");
|
|
||||||
JButton btnLast = new JButton(">|");
|
|
||||||
|
|
||||||
private JPanel moveListPanel;
|
private JPanel moveListPanel;
|
||||||
private JScrollPane moveListScroll;
|
private JScrollPane moveListScroll;
|
||||||
|
|
||||||
|
|
@ -171,7 +166,10 @@ public class GameGui {
|
||||||
buttonPanel.setBackground(new Color(0x0d1b2a));
|
buttonPanel.setBackground(new Color(0x0d1b2a));
|
||||||
// Grid oder Flow – je nach Geschmack
|
// Grid oder Flow – je nach Geschmack
|
||||||
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
|
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
|
||||||
|
JButton btnFirst = new JButton("|<");
|
||||||
|
JButton btnPrev = new JButton("<");
|
||||||
|
JButton btnNext = new JButton(">");
|
||||||
|
JButton btnLast = new JButton(">|");
|
||||||
// Style (optional)
|
// Style (optional)
|
||||||
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
|
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
|
||||||
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
|
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
|
||||||
|
|
@ -219,11 +217,6 @@ public class GameGui {
|
||||||
this.isFlipped = flipped;
|
this.isFlipped = flipped;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JButton getBtnFirst() { return btnFirst; }
|
|
||||||
public JButton getBtnPrev() { return btnPrev; }
|
|
||||||
public JButton getBtnNext() { return btnNext; }
|
|
||||||
public JButton getBtnLast() { return btnLast; }
|
|
||||||
|
|
||||||
public void updateBoard(BoardDTO boardDTO) {
|
public void updateBoard(BoardDTO boardDTO) {
|
||||||
PieceDTO[][] board = boardDTO.getBoard();
|
PieceDTO[][] board = boardDTO.getBoard();
|
||||||
if (!isFlipped) {
|
if (!isFlipped) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue