Compare commits

...

12 Commits

10 changed files with 12971 additions and 634 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 {
OpeningRecognizer.loadOpenings();
new MainFrame(); new MainFrame();
} }
} }

View File

@ -46,6 +46,7 @@ public class ButtonAufgebenListener extends JFrame implements ActionListener {
} }
this.sf.setBoardMode(BoardMode.finished); this.sf.setBoardMode(BoardMode.finished);
this.sf.enableControlPanelButtons();
sf.setButtonsActions(); sf.setButtonsActions();

View File

@ -38,22 +38,22 @@ public class ButtonMovePieceListener implements ActionListener {
this.game.stopClock(); this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished); this.sf.setBoardMode(BoardMode.finished);
this.sf.enableControlPanelButtons(); this.sf.enableControlPanelButtons();
this.sf.showResult("Spieler "+game.getActivePlayer()+" hat gewonnen!"); this.sf.showResult("Spieler " + game.getActivePlayer() + " hat gewonnen!");
} else { } else {
this.sf.setBoardMode(BoardMode.normal); this.sf.setBoardMode(BoardMode.normal);
if (game.getLastMove() != null) {
sf.aktualisiereAusgabe();
}
} }
this.sf.setCursor(null); this.sf.setCursor(null);
//hier rotieren markieren // hier rotieren markieren
if(game.isRotieren())sf.setWechsel(!sf.isWechsel()); if (game.isRotieren())
sf.setWechsel(!sf.isWechsel());
this.sf.erstelleBrett(); this.sf.erstelleBrett();
if (game.getLastMove() != null) {
sf.aktualisiereAusgabe();
}
} }
} }

View File

@ -1,5 +1,6 @@
package de.mannheim.th.chess.domain; package de.mannheim.th.chess.domain;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -29,6 +30,8 @@ public class Game {
private Clock clock; private Clock clock;
private boolean rotieren, zuruecknahme; private boolean rotieren, zuruecknahme;
ArrayList<Piece> removedPieces;
private MoveList movelist; private MoveList movelist;
private int viewPointer; private int viewPointer;
@ -43,6 +46,7 @@ public class Game {
this.board = new Board(); this.board = new Board();
this.movelist = new MoveList(); this.movelist = new MoveList();
this.startPosFen = this.board.getFen(); this.startPosFen = this.board.getFen();
removedPieces = new ArrayList<>();
clock = new Clock("blitz"); clock = new Clock("blitz");
clock.start(); clock.start();
@ -63,6 +67,7 @@ public class Game {
this.movelist = new MoveList(); this.movelist = new MoveList();
clock = new Clock(modus); clock = new Clock(modus);
removedPieces = new ArrayList<>();
} }
/** /**
@ -102,6 +107,14 @@ public class Game {
* @param move the move to be played * @param move the move to be played
*/ */
public void playMove(Move move) { 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.board.doMove(move);
this.movelist.add(move); this.movelist.add(move);
clock.pressClock(); clock.pressClock();
@ -116,7 +129,11 @@ public class Game {
public void undo() { public void undo() {
this.board.undoMove(); this.board.undoMove();
this.movelist.removeLast(); Move lastMove = this.movelist.removeLast();
Piece removedPiece = board.getPiece(lastMove.getTo());
if (removedPiece != Piece.NONE) {
removedPieces.remove(removedPiece);
}
} }
/** /**
@ -153,13 +170,22 @@ public class Game {
/** /**
* Plays the move on the board and adds it to the movelist * Plays the move on the board and adds it to the movelist
* *
* @param origin The square from wich it moves from. * @param origin The square from which it moves from.
* @param desination The square where it will move to. * @param desination The square where it will move to.
*/ */
public void playMove(Square origin, Square desination) { public void playMove(Square origin, Square desination) {
Move move = new Move(origin, desination); 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.board.doMove(move);
this.movelist.add(move); this.movelist.add(move);
clock.pressClock();
} }
@ -320,6 +346,10 @@ public class Game {
return this.viewPointer; return this.viewPointer;
} }
public ArrayList<Piece> getRemovedPieces() {
return removedPieces;
}
public boolean isRotieren() { public boolean isRotieren() {
return rotieren; return rotieren;
} }

View File

@ -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);
@ -87,6 +90,10 @@ public class MainFrame extends JFrame {
contentPane.add(Box.createVerticalStrut(15)); contentPane.add(Box.createVerticalStrut(15));
JButton pgnLoaderButton = new JButton("Lade aus PGN Datei"); JButton pgnLoaderButton = new JButton("Lade aus PGN Datei");
pgnLoaderButton.setBackground(Color.LIGHT_GRAY);
pgnLoaderButton.setForeground(Color.BLACK);
pgnLoaderButton.setFont(new Font("Tahoma", Font.BOLD, 16));
pgnLoaderButton.setAlignmentX(CENTER_ALIGNMENT);
pgnLoaderButton.addActionListener(e -> openPgnSelectFrame()); pgnLoaderButton.addActionListener(e -> openPgnSelectFrame());
contentPane.add(pgnLoaderButton); contentPane.add(pgnLoaderButton);
@ -113,10 +120,11 @@ 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) {
this.game.stopClock(); //this.game.stopClock();
new SpielFrame(this.game); new SpielFrame(this.game);
} }
} }

View File

@ -25,7 +25,7 @@ public class ModeSelectionFrame extends JFrame {
// Frame-Eigenschaften // Frame-Eigenschaften
setTitle("Modusauswahl"); setTitle("Modusauswahl");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 500, 500); setBounds(100, 100, 600, 600);
setResizable(true); setResizable(true);
setAlwaysOnTop(true); setAlwaysOnTop(true);

View File

@ -6,9 +6,12 @@ package de.mannheim.th.chess.ui;
import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList; import com.github.bhlangonijr.chesslib.move.MoveList;
import com.github.bhlangonijr.chesslib.Piece;
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;
@ -47,6 +50,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Toolkit;
public class SpielFrame extends JFrame { public class SpielFrame extends JFrame {
@ -57,9 +61,10 @@ public class SpielFrame extends JFrame {
private HashMap<JButton, String> belegungen = new HashMap<>(); private HashMap<JButton, String> belegungen = new HashMap<>();
private JPanel panelLinks, panelRechts, contentPane, controlPanel; private JPanel panelLinks, panelRechts, contentPane, controlPanel;
private JButton undo, undo2, aufgeben, aufgeben2; private JButton undo, undo2, aufgeben, aufgeben2;
private JTextArea ausgabe; 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;
@ -67,14 +72,14 @@ public class SpielFrame extends JFrame {
private Square selectedSquare; private Square selectedSquare;
public enum BoardMode { public enum BoardMode {
normal, pieceSelected, finished, gameEnd normal, pieceSelected, finished
} }
/** /**
* 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();
@ -82,7 +87,11 @@ public class SpielFrame extends JFrame {
mode = BoardMode.normal; mode = BoardMode.normal;
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(100, 100, 1920, 1080);
//setBounds(100, 100, 1920, 1080);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0, d.width, d.height);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setTitle("Schach"); setTitle("Schach");
setAlwaysOnTop(true); setAlwaysOnTop(true);
@ -244,9 +253,6 @@ public class SpielFrame extends JFrame {
case finished: case finished:
break; break;
case gameEnd:
break;
default: default:
break; break;
} }
@ -272,6 +278,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);
@ -308,7 +315,6 @@ public class SpielFrame extends JFrame {
jb.setIcon(new ImageIcon("src/main/resources/" + pictures[index] + ".png")); jb.setIcon(new ImageIcon("src/main/resources/" + pictures[index] + ".png"));
int selectedPiece = index; int selectedPiece = index;
jb.addActionListener(e -> { jb.addActionListener(e -> {
System.out.println("Test");
result[0] = selectedPiece; result[0] = selectedPiece;
dialog.dispose(); dialog.dispose();
}); });
@ -393,36 +399,60 @@ public class SpielFrame extends JFrame {
// ----- ViewQuicksaveButton ----- // ----- ViewQuicksaveButton -----
JButton quicksave = new JButton("Quicksave"); JButton quicksave = new JButton("Quicksave");
quicksave.setBackground(Color.LIGHT_GRAY);
quicksave.setForeground(Color.BLACK);
quicksave.setFont(new Font("Tahoma", Font.BOLD, 16));
quicksave.setAlignmentX(CENTER_ALIGNMENT);
quicksave.setEnabled(true); quicksave.setEnabled(true);
quicksave.addActionListener(new ButtonQuicksaveListener(this.game)); quicksave.addActionListener(new ButtonQuicksaveListener(this.game));
this.controlPanel.add(quicksave); this.controlPanel.add(quicksave);
// ----- ViewFirstButton ----- // ----- ViewFirstButton -----
JButton viewFirstButton = new JButton("<<-"); JButton viewFirstButton = new JButton("<<");
viewFirstButton.setBackground(Color.LIGHT_GRAY);
viewFirstButton.setForeground(Color.BLACK);
viewFirstButton.setFont(new Font("Calibri", Font.BOLD, 16));
viewFirstButton.setAlignmentX(CENTER_ALIGNMENT);
viewFirstButton.setEnabled(false); viewFirstButton.setEnabled(false);
viewFirstButton.addActionListener(new ButtonViewFirstListener(this.game, this)); viewFirstButton.addActionListener(new ButtonViewFirstListener(this.game, this));
this.controlPanel.add(viewFirstButton); this.controlPanel.add(viewFirstButton);
// ----- ViewBackButton ----- // ----- ViewBackButton -----
JButton viewBackButton = new JButton("<-"); JButton viewBackButton = new JButton("←");
viewBackButton.setBackground(Color.LIGHT_GRAY);
viewBackButton.setForeground(Color.BLACK);
viewBackButton.setFont(new Font("Calibri", Font.BOLD, 16));
viewBackButton.setAlignmentX(CENTER_ALIGNMENT);
viewBackButton.setEnabled(false); viewBackButton.setEnabled(false);
viewBackButton.addActionListener(new ButtonViewBackListener(this.game, this)); viewBackButton.addActionListener(new ButtonViewBackListener(this.game, this));
this.controlPanel.add(viewBackButton); this.controlPanel.add(viewBackButton);
// ----- ViewForwardButton ----- // ----- ViewForwardButton -----
JButton viewForwardButton = new JButton("->"); JButton viewForwardButton = new JButton("→");
viewForwardButton.setBackground(Color.LIGHT_GRAY);
viewForwardButton.setForeground(Color.BLACK);
viewForwardButton.setFont(new Font("Calibri", Font.BOLD, 16));
viewForwardButton.setAlignmentX(CENTER_ALIGNMENT);
viewForwardButton.setEnabled(false); viewForwardButton.setEnabled(false);
viewForwardButton.addActionListener(new ButtonViewForwardListener(this.game, this)); viewForwardButton.addActionListener(new ButtonViewForwardListener(this.game, this));
this.controlPanel.add(viewForwardButton); this.controlPanel.add(viewForwardButton);
// ----- ViewLastButton ----- // ----- ViewLastButton -----
JButton viewLastButton = new JButton("->>"); JButton viewLastButton = new JButton(">>");
viewLastButton.setBackground(Color.LIGHT_GRAY);
viewLastButton.setForeground(Color.BLACK);
viewLastButton.setFont(new Font("Calibri", Font.BOLD, 16));
viewLastButton.setAlignmentX(CENTER_ALIGNMENT);
viewLastButton.setEnabled(false); viewLastButton.setEnabled(false);
viewLastButton.addActionListener(new ButtonViewLastListener(this.game, this)); viewLastButton.addActionListener(new ButtonViewLastListener(this.game, this));
this.controlPanel.add(viewLastButton); this.controlPanel.add(viewLastButton);
// ----- ViewQuickloadButton ----- // ----- ViewQuickloadButton -----
JButton quickload = new JButton("Quickload"); JButton quickload = new JButton("Quickload");
quickload.setBackground(Color.LIGHT_GRAY);
quickload.setForeground(Color.BLACK);
quickload.setFont(new Font("Tahoma", Font.BOLD, 16));
quickload.setAlignmentX(CENTER_ALIGNMENT);
quickload.setEnabled(true); quickload.setEnabled(true);
quickload.addActionListener(new ButtonQuickloadListener(this.game, this)); quickload.addActionListener(new ButtonQuickloadListener(this.game, this));
this.controlPanel.add(quickload); this.controlPanel.add(quickload);
@ -505,18 +535,38 @@ public class SpielFrame extends JFrame {
statistik.setBackground(new Color(90, 90, 90)); statistik.setBackground(new Color(90, 90, 90));
statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS)); statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS));
ausgabe = new JTextArea(); ausgabe = new JTextArea();
ausgabe.setEditable(false); ausgabe.setEditable(false);
ausgabe.setBackground(new Color(75, 75, 75)); ausgabe.setBackground(new Color(75, 75, 75));
ausgabe.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); ausgabe.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
ausgabe.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20)); ausgabe.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
ausgabe.setForeground(Color.BLACK); ausgabe.setForeground(Color.BLACK);
ausgabe.setText("\n Bisherige Züge:\n"); ausgabe.setText("\n Bisherige Züge:\n");
whiteRemovedPieces = new JTextArea();
whiteRemovedPieces.setPreferredSize(new Dimension(300, 100));
whiteRemovedPieces.setEditable(false);
whiteRemovedPieces.setBackground(new Color(75, 75, 75));
whiteRemovedPieces.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
whiteRemovedPieces.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 40));
whiteRemovedPieces.setForeground(Color.WHITE);
blackRemovedPieces = new JTextArea();
blackRemovedPieces.setPreferredSize(new Dimension(300, 100));
blackRemovedPieces.setEditable(false);
blackRemovedPieces.setBackground(new Color(75, 75, 75));
blackRemovedPieces.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
blackRemovedPieces.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 40));
blackRemovedPieces.setForeground(Color.BLACK);
JScrollPane scrollPane = new JScrollPane(ausgabe); JScrollPane scrollPane = new JScrollPane(ausgabe);
scrollPane.setPreferredSize(new Dimension(500, 1000));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
statistik.add(whiteRemovedPieces);
statistik.add(scrollPane); statistik.add(scrollPane);
statistik.add(blackRemovedPieces);
return statistik; return statistik;
} }
@ -524,7 +574,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");
@ -533,6 +584,18 @@ public class SpielFrame extends JFrame {
sb.append(line); sb.append(line);
} }
StringBuilder whitePieces = new StringBuilder();
StringBuilder blackPieces = new StringBuilder();
for (Piece piece: game.getRemovedPieces()) {
if (piece.getPieceSide() == Side.BLACK) {
blackPieces.append(piece.getFanSymbol().toUpperCase());
} else {
whitePieces.append(piece.getFanSymbol().toUpperCase());
}
}
blackRemovedPieces.setText(blackPieces.toString());
whiteRemovedPieces.setText(whitePieces.toString());
ausgabe.setText(sb.toString()); ausgabe.setText(sb.toString());
} }
@ -549,6 +612,7 @@ public class SpielFrame extends JFrame {
} }
ausgabe.setText(sb.toString()); ausgabe.setText(sb.toString());
anzeigeMoves.removeLast();
} }
private JPanel getUiPlayerOne() { 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");
}
}