Reworked methods and added a logger to ChessEngine
parent
3bad46f9ea
commit
e0d14e0811
|
@ -1,6 +1,7 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
|
@ -10,11 +11,13 @@ import com.github.bhlangonijr.chesslib.move.Move;
|
|||
public class ChessEngine {
|
||||
private Board board;
|
||||
private List<Move> moves = new ArrayList<>();
|
||||
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
|
||||
|
||||
private int currentMoveIndex = 0;
|
||||
|
||||
public ChessEngine() {
|
||||
board = new Board();
|
||||
logger.info("Neues ChessEngine-Objekt erstellt.");
|
||||
}
|
||||
|
||||
public boolean move(MoveDTO move) {
|
||||
|
@ -26,31 +29,41 @@ public class ChessEngine {
|
|||
|
||||
//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;
|
||||
}
|
||||
logger.warning("Ungültiger Zug: " + libMove);
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<MoveDTO> getLegalDestinations(String from) {
|
||||
logger.info("Hole legale Züge von: " + from);
|
||||
List<MoveDTO> destinations = new ArrayList<>();
|
||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||
for (Move move : board.legalMoves()) {
|
||||
if (move.getFrom().equals(fromSq)) {
|
||||
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
||||
int fromCol = fromSq.getFile().ordinal();
|
||||
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
||||
int toCol = move.getTo().getFile().ordinal();;
|
||||
destinations.add(new MoveDTO(fromRow, fromCol, toRow, toCol));
|
||||
try {
|
||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||
for (Move move : board.legalMoves()) {
|
||||
if (move.getFrom().equals(fromSq)) {
|
||||
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
||||
int fromCol = fromSq.getFile().ordinal();
|
||||
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
||||
int toCol = move.getTo().getFile().ordinal();
|
||||
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;
|
||||
}
|
||||
|
||||
public List<String> getMoveListStringsGrouped() {
|
||||
logger.info("Gruppiere Züge für Anzeige.");
|
||||
List<String> result = new ArrayList<>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < moves.size(); i++) {
|
||||
|
@ -65,11 +78,13 @@ public class ChessEngine {
|
|||
}
|
||||
|
||||
public PieceDTO getPieceAt(String square) {
|
||||
logger.info("Hole Figur an Feld: " + square);
|
||||
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
|
||||
return convertPieceToDTO(piece);
|
||||
}
|
||||
|
||||
public BoardDTO getBoardAsDTO() {
|
||||
logger.info("Erstelle DTO-Abbild des Boards");
|
||||
PieceDTO[][] dtoBoard = new PieceDTO[8][8];
|
||||
for (int rank = 8; rank >= 1; rank--) {
|
||||
for (int file = 0; file < 8; file++) {
|
||||
|
@ -101,6 +116,7 @@ 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));
|
||||
|
@ -108,9 +124,15 @@ public class ChessEngine {
|
|||
currentMoveIndex = idx;
|
||||
}
|
||||
|
||||
public int getCurrentMoveIndex() { return currentMoveIndex; }
|
||||
public int getCurrentMoveIndex() {
|
||||
logger.info("Hole aktuellen Zug-Index: " + currentMoveIndex);
|
||||
return currentMoveIndex;
|
||||
}
|
||||
|
||||
public int getMoveListSize() { return moves.size(); }
|
||||
public int getMoveListSize() {
|
||||
logger.info("Hole Anzahl gespielter Züge: " + moves.size());
|
||||
return moves.size();
|
||||
}
|
||||
|
||||
private PieceDTO convertPieceToDTO(Piece piece) {
|
||||
if (piece == null || piece.equals(Piece.NONE)) return null;
|
||||
|
@ -121,19 +143,27 @@ public class ChessEngine {
|
|||
}
|
||||
|
||||
public boolean isMated() {
|
||||
return board.isMated();
|
||||
boolean mated = board.isMated();
|
||||
logger.info("isMated() = " + mated);
|
||||
return mated;
|
||||
}
|
||||
|
||||
public boolean isStalemate() {
|
||||
return board.isStaleMate();
|
||||
boolean stale = board.isStaleMate();
|
||||
logger.info("isStalemate() = " + stale);
|
||||
return stale;
|
||||
}
|
||||
|
||||
public boolean isDraw() {
|
||||
return board.isDraw();
|
||||
boolean draw = board.isDraw();
|
||||
logger.info("isDraw() = " + draw);
|
||||
return draw;
|
||||
}
|
||||
|
||||
public String getCurrentPlayer() {
|
||||
return board.getSideToMove().toString(); // "WHITE" oder "BLACK"
|
||||
String player = board.getSideToMove().toString();
|
||||
logger.info("Am Zug: " + player);
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue