Openings werden erkannt und im Logger ausgegeben
parent
ea2c0066fd
commit
d16aa13804
File diff suppressed because it is too large
Load Diff
|
@ -33,7 +33,6 @@ public class ChessEngine {
|
|||
logging();
|
||||
board = new Board();
|
||||
}
|
||||
|
||||
public boolean move(MoveDTO move) {
|
||||
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
|
||||
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
|
||||
|
@ -41,20 +40,38 @@ public class ChessEngine {
|
|||
if (board.legalMoves().contains(libMove)) {
|
||||
board.doMove(libMove);
|
||||
|
||||
//Replay? Dann abschneiden
|
||||
// Replay? Dann abschneiden
|
||||
if (currentMoveIndex < moves.size()) {
|
||||
logger.info("Replay-Modus: Züge nach " + currentMoveIndex + " werden entfernt.");
|
||||
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);
|
||||
|
||||
// Opening-Erkennung nach jedem erfolgreichen Zug
|
||||
String playedMovesUci = movesToUciString(moves);
|
||||
Opening detectedOpening = Opening.detect(playedMovesUci);
|
||||
|
||||
if (detectedOpening != null) {
|
||||
logger.info("Aktuelles Opening erkannt: " + detectedOpening.getEco() + " - " + detectedOpening.getName());
|
||||
// Optional: Speichere das Opening in ein Feld, falls benötigt
|
||||
}
|
||||
return true;
|
||||
}
|
||||
logger.warning("Ungültiger Zug: " + libMove);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private String movesToUciString(List<Move> moves) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Move m : moves) {
|
||||
sb.append(m.toString()).append(" ");
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public List<MoveDTO> getLegalDestinations(String from) {
|
||||
logger.info("Hole legale Züge von: " + from);
|
||||
List<MoveDTO> destinations = new ArrayList<>();
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
import java.util.*;
|
||||
import java.nio.file.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Opening {
|
||||
private final String eco;
|
||||
private final String name;
|
||||
private final String moves;
|
||||
|
||||
public Opening(String eco, String name, String moves) {
|
||||
this.eco = eco;
|
||||
this.name = name;
|
||||
this.moves = moves;
|
||||
}
|
||||
|
||||
public String getEco() { return eco; }
|
||||
public String getName() { return name; }
|
||||
public String getMoves() { return moves; }
|
||||
|
||||
// Öffnet und cached die Liste aus der gewünschten CSV
|
||||
private static List<Opening> cachedOpenings = null;
|
||||
|
||||
public static List<Opening> allOpenings() {
|
||||
if (cachedOpenings == null) {
|
||||
cachedOpenings = new ArrayList<>();
|
||||
// Passe den Pfad für deinen Benutzernamen an!
|
||||
String path = "Ressources/all_openings.csv";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(Paths.get(path))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
// CSV-Format: ECO,"Name","Moves"
|
||||
String[] parts = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
|
||||
if (parts.length == 3) {
|
||||
String eco = parts[0];
|
||||
String name = parts[1].replaceAll("^\"|\"$", "");
|
||||
String moves = parts[2].replaceAll("^\"|\"$", "");
|
||||
cachedOpenings.add(new Opening(eco, name, moves));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return cachedOpenings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sucht das längste passende Opening für eine gegebene Zugfolge (UCI).
|
||||
* @param playedMovesUci UCI-Züge als String, z.B. "e2e4 e7e5 g1f3"
|
||||
* @return bestes Opening oder null
|
||||
*/
|
||||
public static Opening detect(String playedMovesUci) {
|
||||
Opening bestMatch = null;
|
||||
int bestLength = 0;
|
||||
for (Opening opening : allOpenings()) {
|
||||
if (playedMovesUci.startsWith(opening.getMoves())) {
|
||||
if (opening.getMoves().length() > bestLength) {
|
||||
bestMatch = opening;
|
||||
bestLength = opening.getMoves().length();
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestMatch;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue