Hinzufügen Eröffnungserkennung

openingRecognition
Marius Gündel 2025-06-29 16:42:01 +02:00
parent 1cac190b8d
commit ec6f564d72
6 changed files with 12215 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package de.mannheim.th.chess; package de.mannheim.th.chess;
import java.io.IOException;
import de.mannheim.th.chess.ui.MainFrame; import de.mannheim.th.chess.ui.MainFrame;
import de.mannheim.th.chess.utl.OpeningRecognizer;
// import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.LogManager;
// import org.apache.logging.log4j.Logger; // import org.apache.logging.log4j.Logger;
@ -20,8 +23,10 @@ public class App {
* Main-Methode. * Main-Methode.
* *
* @param args * @param args
* @throws IOException
*/ */
public static void main(String[] args) { public static void main(String[] args) throws IOException {
new MainFrame(); new MainFrame();
OpeningRecognizer.loadOpenings();
} }
} }

View File

@ -177,6 +177,7 @@ public class Game {
} }
this.board.doMove(move); this.board.doMove(move);
this.movelist.add(move); this.movelist.add(move);
clock.pressClock();
} }

View File

@ -9,11 +9,13 @@ import javax.swing.border.EmptyBorder;
// import de.mannheim.th.chess.App; // import de.mannheim.th.chess.App;
import de.mannheim.th.chess.domain.Game; import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.utl.OpeningRecognizer;
import javax.swing.JLabel; import javax.swing.JLabel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
@ -32,9 +34,11 @@ public class MainFrame extends JFrame {
/** /**
* Create the frame. * Create the frame.
* @throws IOException
*/ */
public MainFrame() { public MainFrame() {
setBackground(Color.LIGHT_GRAY); setBackground(Color.LIGHT_GRAY);
setResizable(true); setResizable(true);
setAlwaysOnTop(true); setAlwaysOnTop(true);
@ -117,6 +121,7 @@ public class MainFrame extends JFrame {
/** /**
* Starts the spielframe and game in playmode * Starts the spielframe and game in playmode
* @throws IOException
*/ */
public void startGame() { public void startGame() {
if (this.game != null) { if (this.game != null) {

View File

@ -11,6 +11,7 @@ import com.github.bhlangonijr.chesslib.Side;
import de.mannheim.th.chess.domain.Game; import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.utl.Clock; import de.mannheim.th.chess.utl.Clock;
import de.mannheim.th.chess.utl.OpeningRecognizer;
import de.mannheim.th.chess.controller.ButtonAufgebenListener; import de.mannheim.th.chess.controller.ButtonAufgebenListener;
import de.mannheim.th.chess.controller.ButtonFileSaverListener; import de.mannheim.th.chess.controller.ButtonFileSaverListener;
import de.mannheim.th.chess.controller.ButtonMovePieceListener; import de.mannheim.th.chess.controller.ButtonMovePieceListener;
@ -62,6 +63,7 @@ public class SpielFrame extends JFrame {
private JTextArea ausgabe, blackRemovedPieces, whiteRemovedPieces; private JTextArea ausgabe, blackRemovedPieces, whiteRemovedPieces;
private Game game; private Game game;
private Clock clock; private Clock clock;
private String opening;
private ArrayList<String> anzeigeMoves = new ArrayList<String>(); private ArrayList<String> anzeigeMoves = new ArrayList<String>();
private boolean wechsel = false; private boolean wechsel = false;
@ -76,7 +78,7 @@ public class SpielFrame extends JFrame {
* Create the frame. * Create the frame.
*/ */
public SpielFrame(Game game) { public SpielFrame(Game game) {
opening = "unbekannte Eröffnung";
this.game = game; this.game = game;
this.clock = game.getClock(); this.clock = game.getClock();
this.clock.start(); this.clock.start();
@ -566,7 +568,8 @@ public class SpielFrame extends JFrame {
public void aktualisiereAusgabe() { public void aktualisiereAusgabe() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("\n Bisherige Züge:\n"); opening = OpeningRecognizer.compareOpening(game.getMoveList(), opening);
sb.append("\n Bisherige Züge: " + opening + "\n");
MoveList l = game.getMoveList(); MoveList l = game.getMoveList();
anzeigeMoves.add(" " + game.getUnicodeFromMove(l.getLast()) + ": " + l.getLast().toString() + "\n"); anzeigeMoves.add(" " + game.getUnicodeFromMove(l.getLast()) + ": " + l.getLast().toString() + "\n");

View File

@ -0,0 +1,65 @@
package de.mannheim.th.chess.utl;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.List;
import com.github.bhlangonijr.chesslib.move.MoveList;
public class OpeningRecognizer {
private static class Opening {
String name;
String moves;
Opening(String name, String moves) {
this.name = name;
this.moves = moves;
}
}
private static List<Opening> openingList = new ArrayList<>();
public static void loadOpenings() throws IOException {
BufferedReader openingReader = new BufferedReader(new FileReader("src/main/resources/openings.pgn"));
StringBuilder openingName = new StringBuilder();
String moves = null;
String line;
while ((line = openingReader.readLine()) != null) {
if ((line.startsWith("[Site") && openingName.toString().equals("")) || line.equals("") ) {
continue;
}
if (line.startsWith("[Site")) {
openingList.add(new Opening(openingName.toString(), moves));
moves = null;
openingName.delete(0, openingName.length());
continue;
}
if (line.startsWith("[White")) {
openingName.append(line.split("\"")[1].trim());
continue;
}
if (line.startsWith("[Black")) {
openingName.append(":").append(line.split("\"")[1].trim());
continue;
}
moves = line;
}
openingReader.close();
}
public static String compareOpening(MoveList moves, String openingBefore) {
for (Opening o: openingList) {
if (o.moves.equals(moves.toSanWithMoveNumbers().trim())) {
return o.name;
}
}
return openingBefore;
}
}

File diff suppressed because it is too large Load Diff