openingRecognition #28
|
|
@ -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();
|
OpeningRecognizer.loadOpenings();
|
||||||
|
new MainFrame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,11 @@ public class Game {
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
Piece removedPiece = board.getPiece(move.getTo());
|
Piece removedPiece = board.getPiece(move.getTo());
|
||||||
if (removedPiece != Piece.NONE) {
|
if (removedPiece != Piece.NONE) {
|
||||||
|
int removedPiecesCount = removedPieces.size();
|
||||||
removedPieces.add(removedPiece);
|
removedPieces.add(removedPiece);
|
||||||
|
if (removedPiecesCount + 1 < removedPieces.size()) {
|
||||||
|
removedPieces.removeLast();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.board.doMove(move);
|
this.board.doMove(move);
|
||||||
this.movelist.add(move);
|
this.movelist.add(move);
|
||||||
|
|
@ -173,10 +177,15 @@ public class Game {
|
||||||
Move move = new Move(origin, desination);
|
Move move = new Move(origin, desination);
|
||||||
Piece removedPiece = board.getPiece(desination);
|
Piece removedPiece = board.getPiece(desination);
|
||||||
if (removedPiece != Piece.NONE) {
|
if (removedPiece != Piece.NONE) {
|
||||||
|
int removedPiecesCount = removedPieces.size();
|
||||||
removedPieces.add(removedPiece);
|
removedPieces.add(removedPiece);
|
||||||
|
if (removedPieces.size() > removedPiecesCount + 1) {
|
||||||
|
removedPieces.removeLast();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.board.doMove(move);
|
this.board.doMove(move);
|
||||||
this.movelist.add(move);
|
this.movelist.add(move);
|
||||||
|
clock.pressClock();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ 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 +33,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 +120,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) {
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
@ -271,6 +273,7 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public void showWin(int player) {
|
public void showWin(int player) {
|
||||||
JFrame frame = new JFrame("Result");
|
JFrame frame = new JFrame("Result");
|
||||||
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
|
@ -566,7 +569,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");
|
||||||
|
|
@ -603,6 +607,7 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
ausgabe.setText(sb.toString());
|
ausgabe.setText(sb.toString());
|
||||||
|
anzeigeMoves.removeLast();
|
||||||
}
|
}
|
||||||
|
|
||||||
private JPanel getUiPlayerOne() {
|
private JPanel getUiPlayerOne() {
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
@ -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");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue