Merge pull request 'openingRecognition' (#28) from openingRecognition into main

Reviewed-on: #28
main^2
Marius Gündel 2025-06-30 14:51:37 +02:00
commit b43a29ff1c
7 changed files with 12257 additions and 5 deletions

View File

@ -1,6 +1,9 @@
package de.mannheim.th.chess;
import java.io.IOException;
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.Logger;
@ -20,8 +23,10 @@ public class App {
* Main-Methode.
*
* @param args
* @throws IOException
*/
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
OpeningRecognizer.loadOpenings();
new MainFrame();
}
}

View File

@ -109,7 +109,11 @@ public class Game {
public void playMove(Move move) {
Piece removedPiece = board.getPiece(move.getTo());
if (removedPiece != Piece.NONE) {
int removedPiecesCount = removedPieces.size();
removedPieces.add(removedPiece);
if (removedPiecesCount + 1 < removedPieces.size()) {
removedPieces.removeLast();
}
}
this.board.doMove(move);
this.movelist.add(move);
@ -173,10 +177,15 @@ public class Game {
Move move = new Move(origin, desination);
Piece removedPiece = board.getPiece(desination);
if (removedPiece != Piece.NONE) {
int removedPiecesCount = removedPieces.size();
removedPieces.add(removedPiece);
if (removedPieces.size() > removedPiecesCount + 1) {
removedPieces.removeLast();
}
}
this.board.doMove(move);
this.movelist.add(move);
clock.pressClock();
}

View File

@ -14,6 +14,7 @@ import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Box;
import javax.swing.BoxLayout;
@ -32,9 +33,11 @@ public class MainFrame extends JFrame {
/**
* Create the frame.
* @throws IOException
*/
public MainFrame() {
setBackground(Color.LIGHT_GRAY);
setResizable(true);
setAlwaysOnTop(true);
@ -117,6 +120,7 @@ public class MainFrame extends JFrame {
/**
* Starts the spielframe and game in playmode
* @throws IOException
*/
public void startGame() {
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.utl.Clock;
import de.mannheim.th.chess.utl.OpeningRecognizer;
import de.mannheim.th.chess.controller.ButtonAufgebenListener;
import de.mannheim.th.chess.controller.ButtonFileSaverListener;
import de.mannheim.th.chess.controller.ButtonMovePieceListener;
@ -62,6 +63,7 @@ public class SpielFrame extends JFrame {
private JTextArea ausgabe, blackRemovedPieces, whiteRemovedPieces;
private Game game;
private Clock clock;
private String opening;
private ArrayList<String> anzeigeMoves = new ArrayList<String>();
private boolean wechsel = false;
@ -76,7 +78,7 @@ public class SpielFrame extends JFrame {
* Create the frame.
*/
public SpielFrame(Game game) {
opening = "unbekannte Eröffnung";
this.game = game;
this.clock = game.getClock();
this.clock.start();
@ -271,6 +273,7 @@ public class SpielFrame extends JFrame {
}
}
@Deprecated
public void showWin(int player) {
JFrame frame = new JFrame("Result");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
@ -566,7 +569,8 @@ public class SpielFrame extends JFrame {
public void aktualisiereAusgabe() {
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();
anzeigeMoves.add(" " + game.getUnicodeFromMove(l.getLast()) + ": " + l.getLast().toString() + "\n");
@ -603,6 +607,7 @@ public class SpielFrame extends JFrame {
}
ausgabe.setText(sb.toString());
anzeigeMoves.removeLast();
}
private JPanel getUiPlayerOne() {

View File

@ -0,0 +1,64 @@
package de.mannheim.th.chess.utl;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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

View File

@ -0,0 +1,32 @@
package de.mannheim.th.chess.utl;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList;
class OpeningRecognizerTest {
@BeforeAll
static void prepareOpenings() throws IOException {
OpeningRecognizer.loadOpenings();
}
@Test
void test() {
Move m = new Move(Square.E2, Square.E4);
MoveList moves = new MoveList();
moves.add(m);
assertEquals(OpeningRecognizer.compareOpening(moves, "Unknown"), "King's pawn Opening");
}
}