Reworked methods and added a logger to ChessEngine
parent
3bad46f9ea
commit
e0d14e0811
|
|
@ -1,6 +1,7 @@
|
||||||
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.Logger;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
import com.github.bhlangonijr.chesslib.Piece;
|
import com.github.bhlangonijr.chesslib.Piece;
|
||||||
|
|
@ -10,11 +11,13 @@ 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;
|
private int currentMoveIndex = 0;
|
||||||
|
|
||||||
public ChessEngine() {
|
public ChessEngine() {
|
||||||
board = new Board();
|
board = new Board();
|
||||||
|
logger.info("Neues ChessEngine-Objekt erstellt.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean move(MoveDTO move) {
|
public boolean move(MoveDTO move) {
|
||||||
|
|
@ -26,31 +29,41 @@ public class ChessEngine {
|
||||||
|
|
||||||
//Replay? Dann abschneiden
|
//Replay? Dann abschneiden
|
||||||
if (currentMoveIndex < moves.size()) {
|
if (currentMoveIndex < moves.size()) {
|
||||||
|
logger.info("Replay-Modus: Züge nach " + currentMoveIndex + " werden entfernt.");
|
||||||
moves = new ArrayList<>(moves.subList(0, currentMoveIndex));
|
moves = new ArrayList<>(moves.subList(0, currentMoveIndex));
|
||||||
}
|
}
|
||||||
moves.add(libMove);
|
moves.add(libMove);
|
||||||
currentMoveIndex++;
|
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<>();
|
||||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
try {
|
||||||
for (Move move : board.legalMoves()) {
|
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||||
if (move.getFrom().equals(fromSq)) {
|
for (Move move : board.legalMoves()) {
|
||||||
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
if (move.getFrom().equals(fromSq)) {
|
||||||
int fromCol = fromSq.getFile().ordinal();
|
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
||||||
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
int fromCol = fromSq.getFile().ordinal();
|
||||||
int toCol = move.getTo().getFile().ordinal();;
|
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
||||||
destinations.add(new MoveDTO(fromRow, fromCol, toRow, toCol));
|
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;
|
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++) {
|
||||||
|
|
@ -65,11 +78,13 @@ 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++) {
|
||||||
|
|
@ -101,6 +116,7 @@ public class ChessEngine {
|
||||||
|
|
||||||
public void setPositionToMoveIndex(int idx) {
|
public void setPositionToMoveIndex(int idx) {
|
||||||
// Neues Board erzeugen
|
// Neues Board erzeugen
|
||||||
|
logger.info("Setze Board auf Zug-Index: " + idx);
|
||||||
board = new Board();
|
board = new Board();
|
||||||
for (int i = 0; i < idx; i++) {
|
for (int i = 0; i < idx; i++) {
|
||||||
board.doMove(moves.get(i));
|
board.doMove(moves.get(i));
|
||||||
|
|
@ -108,9 +124,15 @@ public class ChessEngine {
|
||||||
currentMoveIndex = idx;
|
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) {
|
private PieceDTO convertPieceToDTO(Piece piece) {
|
||||||
if (piece == null || piece.equals(Piece.NONE)) return null;
|
if (piece == null || piece.equals(Piece.NONE)) return null;
|
||||||
|
|
@ -121,19 +143,27 @@ public class ChessEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMated() {
|
public boolean isMated() {
|
||||||
return board.isMated();
|
boolean mated = board.isMated();
|
||||||
|
logger.info("isMated() = " + mated);
|
||||||
|
return mated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isStalemate() {
|
public boolean isStalemate() {
|
||||||
return board.isStaleMate();
|
boolean stale = board.isStaleMate();
|
||||||
|
logger.info("isStalemate() = " + stale);
|
||||||
|
return stale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDraw() {
|
public boolean isDraw() {
|
||||||
return board.isDraw();
|
boolean draw = board.isDraw();
|
||||||
|
logger.info("isDraw() = " + draw);
|
||||||
|
return draw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentPlayer() {
|
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