Compare commits

..

No commits in common. "main" and "cleanup" have entirely different histories.

10 changed files with 634 additions and 12971 deletions

View File

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

View File

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

View File

@ -38,22 +38,22 @@ public class ButtonMovePieceListener implements ActionListener {
this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished);
this.sf.enableControlPanelButtons();
this.sf.showResult("Spieler " + game.getActivePlayer() + " hat gewonnen!");
this.sf.showResult("Spieler "+game.getActivePlayer()+" hat gewonnen!");
} else {
this.sf.setBoardMode(BoardMode.normal);
if (game.getLastMove() != null) {
sf.aktualisiereAusgabe();
}
}
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();
if (game.getLastMove() != null) {
sf.aktualisiereAusgabe();
}
}
}

View File

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

View File

@ -14,7 +14,6 @@ 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;
@ -33,11 +32,9 @@ public class MainFrame extends JFrame {
/**
* Create the frame.
* @throws IOException
*/
public MainFrame() {
setBackground(Color.LIGHT_GRAY);
setResizable(true);
setAlwaysOnTop(true);
@ -90,10 +87,6 @@ public class MainFrame extends JFrame {
contentPane.add(Box.createVerticalStrut(15));
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());
contentPane.add(pgnLoaderButton);
@ -120,11 +113,10 @@ public class MainFrame extends JFrame {
/**
* Starts the spielframe and game in playmode
* @throws IOException
*/
public void startGame() {
if (this.game != null) {
//this.game.stopClock();
this.game.stopClock();
new SpielFrame(this.game);
}
}

View File

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

View File

@ -6,12 +6,9 @@ package de.mannheim.th.chess.ui;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
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.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;
@ -50,7 +47,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.GridLayout;
import java.awt.Toolkit;
public class SpielFrame extends JFrame {
@ -61,10 +57,9 @@ public class SpielFrame extends JFrame {
private HashMap<JButton, String> belegungen = new HashMap<>();
private JPanel panelLinks, panelRechts, contentPane, controlPanel;
private JButton undo, undo2, aufgeben, aufgeben2;
private JTextArea ausgabe, blackRemovedPieces, whiteRemovedPieces;
private JTextArea ausgabe;
private Game game;
private Clock clock;
private String opening;
private ArrayList<String> anzeigeMoves = new ArrayList<String>();
private boolean wechsel = false;
@ -72,14 +67,14 @@ public class SpielFrame extends JFrame {
private Square selectedSquare;
public enum BoardMode {
normal, pieceSelected, finished
normal, pieceSelected, finished, gameEnd
}
/**
* Create the frame.
*/
public SpielFrame(Game game) {
opening = "unbekannte Eröffnung";
this.game = game;
this.clock = game.getClock();
this.clock.start();
@ -87,11 +82,7 @@ public class SpielFrame extends JFrame {
mode = BoardMode.normal;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//setBounds(100, 100, 1920, 1080);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0, d.width, d.height);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setBounds(100, 100, 1920, 1080);
setTitle("Schach");
setAlwaysOnTop(true);
@ -253,6 +244,9 @@ public class SpielFrame extends JFrame {
case finished:
break;
case gameEnd:
break;
default:
break;
}
@ -278,7 +272,6 @@ public class SpielFrame extends JFrame {
}
}
@Deprecated
public void showWin(int player) {
JFrame frame = new JFrame("Result");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
@ -315,6 +308,7 @@ public class SpielFrame extends JFrame {
jb.setIcon(new ImageIcon("src/main/resources/" + pictures[index] + ".png"));
int selectedPiece = index;
jb.addActionListener(e -> {
System.out.println("Test");
result[0] = selectedPiece;
dialog.dispose();
});
@ -399,60 +393,36 @@ public class SpielFrame extends JFrame {
// ----- ViewQuicksaveButton -----
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.addActionListener(new ButtonQuicksaveListener(this.game));
this.controlPanel.add(quicksave);
// ----- ViewFirstButton -----
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);
JButton viewFirstButton = new JButton("<<-");
viewFirstButton.setEnabled(false);
viewFirstButton.addActionListener(new ButtonViewFirstListener(this.game, this));
this.controlPanel.add(viewFirstButton);
// ----- ViewBackButton -----
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);
JButton viewBackButton = new JButton("<-");
viewBackButton.setEnabled(false);
viewBackButton.addActionListener(new ButtonViewBackListener(this.game, this));
this.controlPanel.add(viewBackButton);
// ----- ViewForwardButton -----
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);
JButton viewForwardButton = new JButton("->");
viewForwardButton.setEnabled(false);
viewForwardButton.addActionListener(new ButtonViewForwardListener(this.game, this));
this.controlPanel.add(viewForwardButton);
// ----- ViewLastButton -----
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);
JButton viewLastButton = new JButton("->>");
viewLastButton.setEnabled(false);
viewLastButton.addActionListener(new ButtonViewLastListener(this.game, this));
this.controlPanel.add(viewLastButton);
// ----- ViewQuickloadButton -----
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.addActionListener(new ButtonQuickloadListener(this.game, this));
this.controlPanel.add(quickload);
@ -535,38 +505,18 @@ public class SpielFrame extends JFrame {
statistik.setBackground(new Color(90, 90, 90));
statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS));
ausgabe = new JTextArea();
ausgabe.setEditable(false);
ausgabe.setBackground(new Color(75, 75, 75));
ausgabe.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
ausgabe.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
ausgabe.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
ausgabe.setForeground(Color.BLACK);
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);
scrollPane.setPreferredSize(new Dimension(500, 1000));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
statistik.add(whiteRemovedPieces);
statistik.add(scrollPane);
statistik.add(blackRemovedPieces);
return statistik;
}
@ -574,8 +524,7 @@ public class SpielFrame extends JFrame {
public void aktualisiereAusgabe() {
StringBuilder sb = new StringBuilder();
opening = OpeningRecognizer.compareOpening(game.getMoveList(), opening);
sb.append("\n Bisherige Züge: " + opening + "\n");
sb.append("\n Bisherige Züge:\n");
MoveList l = game.getMoveList();
anzeigeMoves.add(" " + game.getUnicodeFromMove(l.getLast()) + ": " + l.getLast().toString() + "\n");
@ -584,18 +533,6 @@ public class SpielFrame extends JFrame {
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());
}
@ -612,7 +549,6 @@ public class SpielFrame extends JFrame {
}
ausgabe.setText(sb.toString());
anzeigeMoves.removeLast();
}
private JPanel getUiPlayerOne() {

View File

@ -1,64 +0,0 @@
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

@ -1,32 +0,0 @@
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");
}
}