Compare commits
34 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
86cb40439f | |
|
|
f3aed91025 | |
|
|
b43a29ff1c | |
|
|
3a9084da0b | |
|
|
9995299331 | |
|
|
ec6f564d72 | |
|
|
1cac190b8d | |
|
|
116e2c5e55 | |
|
|
201497b4a8 | |
|
|
9588847c72 | |
|
|
bc909c3ba5 | |
|
|
b6a74b957e | |
|
|
700213a465 | |
|
|
dc72fe5f0a | |
|
|
101b5e7d19 | |
|
|
043e8b5090 | |
|
|
516ecb1b10 | |
|
|
73768473e1 | |
|
|
100cf4712c | |
|
|
518ff09d45 | |
|
|
dc6e6d311b | |
|
|
cdf993d998 | |
|
|
2afe8d406c | |
|
|
dc288ee95e | |
|
|
b44d2e636f | |
|
|
f3cd880c9e | |
|
|
039a5756ee | |
|
|
cf0d56847b | |
|
|
cf1513c162 | |
|
|
2a7d7dd16b | |
|
|
82a10a46fc | |
|
|
6d1aa274ac | |
|
|
bee5b22ac1 | |
|
|
cd89c3b131 |
Binary file not shown.
Binary file not shown.
5
quellen
5
quellen
|
|
@ -1,2 +1,5 @@
|
|||
JFileChoser in UI: (mit GPT)
|
||||
Prompt: "Wie kann ich in swing in java Files aus einem Explorer auswählen und speichern?".
|
||||
Prompt: "Wie kann ich in swing in java Files aus einem Explorer auswählen und speichern?".
|
||||
|
||||
JButton mit Text und Icon gleichzeitig belegen:
|
||||
Pompt: "Kann ich einem button einen text und ein bild gleichzeigtig geben?".
|
||||
|
|
@ -1,25 +1,32 @@
|
|||
package de.mannheim.th.chess;
|
||||
|
||||
import de.mannheim.th.chess.ui.MainFrame;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Eine einfache Schach App mithilfe von {@linkplain https://github.com/bhlangonijr/chesslib} entwickelt.
|
||||
* Eine einfache Schach App mithilfe von
|
||||
* {@linkplain https://github.com/bhlangonijr/chesslib} entwickelt.
|
||||
*
|
||||
* @author Matias Mas Viehl, Dominik Stuck und Marius Guendel
|
||||
* @version 0.0.1
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static MainFrame userinterface;
|
||||
/**
|
||||
* Main-Methode.
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
userinterface = new MainFrame();
|
||||
}
|
||||
|
||||
// private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Main-Methode.
|
||||
*
|
||||
* @param args
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void main(String[] args) throws IOException {
|
||||
OpeningRecognizer.loadOpenings();
|
||||
new MainFrame();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,17 +5,50 @@ import java.awt.event.ActionListener;
|
|||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class ButtonAufgebenListener extends JFrame implements ActionListener{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public ButtonAufgebenListener() {
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||
|
||||
public class ButtonAufgebenListener extends JFrame implements ActionListener {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private SpielFrame sf;
|
||||
private Game g;
|
||||
|
||||
public ButtonAufgebenListener(SpielFrame sf, Game g) {
|
||||
this.sf = sf;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Object source = e.getSource();
|
||||
|
||||
g.stopClock();
|
||||
|
||||
// weil nur der aktive Spieler button drücken darf
|
||||
if (source == sf.getAufgeben()) {
|
||||
logger.info("Spieler 1 will aufgeben.");
|
||||
sf.getAufgeben2().setEnabled(false);
|
||||
sf.showResult("Spieler 2 hat gewonnen!");
|
||||
} else if (source == sf.getAufgeben2()) {
|
||||
logger.info("Spieler 2 will aufgeben.");
|
||||
sf.getAufgeben().setEnabled(false);
|
||||
sf.showResult("Spieler 1 hat gewonnen!");
|
||||
}
|
||||
|
||||
this.sf.setBoardMode(BoardMode.finished);
|
||||
this.sf.enableControlPanelButtons();
|
||||
|
||||
sf.setButtonsActions();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,41 +14,43 @@ import javax.swing.JOptionPane;
|
|||
|
||||
import de.mannheim.th.chess.ui.ModeSelectionFrame;
|
||||
|
||||
public class ButtonFileLoaderListener implements ActionListener{
|
||||
|
||||
private ModeSelectionFrame msf;
|
||||
|
||||
public ButtonFileLoaderListener(ModeSelectionFrame msf) {
|
||||
this.msf = msf;
|
||||
|
||||
}
|
||||
public class ButtonFileLoaderListener implements ActionListener {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
JFileChooser dateiWaehler = new JFileChooser();
|
||||
JFrame jfFile = new JFrame();
|
||||
int auswahl = dateiWaehler.showOpenDialog(jfFile);
|
||||
private ModeSelectionFrame msf;
|
||||
|
||||
if (auswahl == JFileChooser.APPROVE_OPTION) {
|
||||
File ausgewaehlteDatei = dateiWaehler.getSelectedFile();
|
||||
JOptionPane.showMessageDialog(jfFile, "Gewählte Datei:\n" + ausgewaehlteDatei.getAbsolutePath());
|
||||
public ButtonFileLoaderListener(ModeSelectionFrame msf) {
|
||||
this.msf = msf;
|
||||
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(ausgewaehlteDatei));
|
||||
|
||||
msf.setFen(br.readLine());
|
||||
|
||||
} catch (FileNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
JFileChooser dateiWaehler = new JFileChooser();
|
||||
JFrame jfFile = new JFrame();
|
||||
int auswahl = dateiWaehler.showOpenDialog(jfFile);
|
||||
|
||||
if (auswahl == JFileChooser.APPROVE_OPTION) {
|
||||
File ausgewaehlteDatei = dateiWaehler.getSelectedFile();
|
||||
JOptionPane.showMessageDialog(jfFile, "Gewählte Datei:\n" + ausgewaehlteDatei.getAbsolutePath());
|
||||
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(ausgewaehlteDatei));
|
||||
|
||||
msf.setFen(br.readLine());
|
||||
|
||||
br.close();
|
||||
|
||||
} catch (FileNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import java.io.BufferedWriter;
|
|||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JFrame;
|
||||
|
|
@ -14,48 +13,46 @@ import javax.swing.JFrame;
|
|||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
|
||||
public class ButtonFileSaverListener implements ActionListener{
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private Game g;
|
||||
private JFrame sf;
|
||||
|
||||
public ButtonFileSaverListener(JFrame sf, Game g) {
|
||||
this.sf = sf;
|
||||
this.g = g;
|
||||
}
|
||||
public class ButtonFileSaverListener implements ActionListener {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
logger.info("Spiel wird gespeichert.");
|
||||
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setCurrentDirectory(new File(System.getProperty("user.home") + "/Documents"));
|
||||
|
||||
chooser.setDialogTitle("Datei speichern");
|
||||
int userSelection = chooser.showSaveDialog(sf);
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
if (userSelection == JFileChooser.APPROVE_OPTION) {
|
||||
File fileToSave = chooser.getSelectedFile();
|
||||
private Game g;
|
||||
private JFrame sf;
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileToSave))) {
|
||||
|
||||
writer.write(g.getFen());
|
||||
|
||||
logger.info(g.getFen());
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
logger.info("Speichern fehlgeschlagen.");
|
||||
}
|
||||
}
|
||||
public ButtonFileSaverListener(JFrame sf, Game g) {
|
||||
this.sf = sf;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
logger.info("Spiel wird gespeichert.");
|
||||
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setCurrentDirectory(new File(System.getProperty("user.home") + "/Documents"));
|
||||
|
||||
chooser.setDialogTitle("Datei speichern");
|
||||
int userSelection = chooser.showSaveDialog(sf);
|
||||
|
||||
if (userSelection == JFileChooser.APPROVE_OPTION) {
|
||||
File fileToSave = chooser.getSelectedFile();
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileToSave))) {
|
||||
|
||||
writer.write(g.getFen());
|
||||
|
||||
logger.info(g.getFen());
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
logger.info("Speichern fehlgeschlagen.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,22 +27,33 @@ public class ButtonMovePieceListener implements ActionListener {
|
|||
else
|
||||
this.game.playMove(this.mv);
|
||||
|
||||
this.game.setViewPointer(this.game.getMoveList().size() - 1);
|
||||
|
||||
if (this.game.isDraw()) {
|
||||
this.game.stopClock();
|
||||
this.sf.setBoardMode(BoardMode.finished);
|
||||
this.sf.showDraw();
|
||||
this.sf.enableControlPanelButtons();
|
||||
this.sf.showResult("Unentschieden!");
|
||||
} else if (this.game.isMate()) {
|
||||
this.game.stopClock();
|
||||
this.sf.setBoardMode(BoardMode.finished);
|
||||
this.sf.showWin(game.getActivePlayer());
|
||||
}
|
||||
this.sf.setBoardMode(BoardMode.normal);
|
||||
this.sf.setCursor(null);
|
||||
this.sf.erstelleBrett();
|
||||
|
||||
if (game.getLastMove() != null) {
|
||||
|
||||
this.sf.enableControlPanelButtons();
|
||||
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
|
||||
|
||||
if (game.isRotieren())
|
||||
sf.setWechsel(!sf.isWechsel());
|
||||
|
||||
this.sf.erstelleBrett();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package de.mannheim.th.chess.controller;
|
|||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||
|
||||
|
|
@ -20,7 +19,7 @@ public class ButtonToNormalListener implements ActionListener {
|
|||
this.sf.setSelectedSquare(null);
|
||||
this.sf.setCursor(null);
|
||||
this.sf.erstelleBrett();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
|
||||
public abstract class BaseButtonViewListener {
|
||||
protected SpielFrame sf;
|
||||
protected Game game;
|
||||
|
||||
public BaseButtonViewListener(Game game, SpielFrame sf) {
|
||||
this.sf = sf;
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the gamestate and renders the board
|
||||
*/
|
||||
protected void updateView() {
|
||||
this.game.loadView();
|
||||
this.sf.setDefaultButtons();
|
||||
this.sf.applyBoardButtons();
|
||||
this.sf.ladeBrett();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
package de.mannheim.th.chess.controller;
|
||||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.mannheim.th.chess.controller;
|
||||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class ButtonViewBackListener extends BaseButtonViewListener implements ActionListener {
|
||||
|
||||
public ButtonViewBackListener(Game game, SpielFrame sf) {
|
||||
super(game, sf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (this.game.getViewPointer() > 0) {
|
||||
this.game.setViewPointer(this.game.getViewPointer() - 1);
|
||||
updateView();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class ButtonViewFirstListener extends BaseButtonViewListener implements ActionListener {
|
||||
|
||||
public ButtonViewFirstListener(Game game, SpielFrame sf) {
|
||||
super(game, sf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
this.game.setViewPointer(0);
|
||||
updateView();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class ButtonViewForwardListener extends BaseButtonViewListener implements ActionListener {
|
||||
|
||||
public ButtonViewForwardListener(Game game, SpielFrame sf) {
|
||||
super(game, sf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (this.game.getMoveList().size() > this.game.getViewPointer()) {
|
||||
this.game.setViewPointer(this.game.getViewPointer() + 1);
|
||||
updateView();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
package de.mannheim.th.chess.controller.controlPanel;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class ButtonViewLastListener extends BaseButtonViewListener implements ActionListener {
|
||||
|
||||
public ButtonViewLastListener(Game game, SpielFrame sf) {
|
||||
super(game, sf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
this.game.setViewPointer(this.game.getMoveList().size());
|
||||
updateView();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package de.mannheim.th.chess.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -13,10 +14,8 @@ import com.github.bhlangonijr.chesslib.Side;
|
|||
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.pgn.PgnHolder;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.utl.Clock;
|
||||
|
||||
/**
|
||||
|
|
@ -29,11 +28,12 @@ public class Game {
|
|||
|
||||
private Board board;
|
||||
private Clock clock;
|
||||
private SpielFrame sp;
|
||||
private String modus;
|
||||
private boolean rotieren, zuruecknahme;
|
||||
|
||||
ArrayList<Piece> removedPieces;
|
||||
|
||||
private MoveList movelist;
|
||||
private int viewPointer;
|
||||
|
||||
private MoveList savestate;
|
||||
private String startPosFen;
|
||||
|
|
@ -46,13 +46,13 @@ 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();
|
||||
}
|
||||
|
||||
public Game(String modus, boolean rotieren, boolean zuruecknahme, String fen) {
|
||||
this.modus = modus;
|
||||
this.rotieren = rotieren;
|
||||
this.zuruecknahme = zuruecknahme;
|
||||
|
||||
|
|
@ -67,9 +67,7 @@ public class Game {
|
|||
this.movelist = new MoveList();
|
||||
|
||||
clock = new Clock(modus);
|
||||
|
||||
sp = new SpielFrame(this);
|
||||
|
||||
removedPieces = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,15 +79,13 @@ public class Game {
|
|||
this.board = new Board();
|
||||
|
||||
this.startPosFen = this.board.getFen();
|
||||
|
||||
this.movelist = movelist;
|
||||
|
||||
for (Move move : movelist) {
|
||||
this.board.doMove(move);
|
||||
}
|
||||
|
||||
// this.clockPlayer1 = new Clock();
|
||||
// this.clockPlayer2 = new Clock();
|
||||
this.clock = new Clock("blitz");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -103,10 +99,6 @@ public class Game {
|
|||
|
||||
this.movelist = new MoveList();
|
||||
this.startPosFen = this.board.getFen();
|
||||
// this.sp = new SpielFrame();
|
||||
|
||||
// this.clockPlayer1 = new Clock();
|
||||
// this.clockPlayer2 = new Clock();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -115,6 +107,14 @@ 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,7 +129,11 @@ public class Game {
|
|||
|
||||
public void undo() {
|
||||
this.board.undoMove();
|
||||
this.movelist.removeLast();
|
||||
Move lastMove = this.movelist.removeLast();
|
||||
Piece removedPiece = board.getPiece(lastMove.getTo());
|
||||
if (removedPiece != Piece.NONE) {
|
||||
removedPieces.remove(removedPiece);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -138,7 +142,8 @@ public class Game {
|
|||
public void quicksave() {
|
||||
// TODO: save the current clocktime
|
||||
this.savestate = new MoveList(this.movelist);
|
||||
logger.info("Quicksaved");
|
||||
|
||||
logger.info("Quicksaved...");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -158,23 +163,81 @@ public class Game {
|
|||
this.playMove(move);
|
||||
}
|
||||
|
||||
logger.info("Quickloaded");
|
||||
logger.info("Quickloaded...");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
public void doPromotionMove(int piece, Square origin, Square destination) {
|
||||
System.out.println(piece);
|
||||
Piece promotedTo;
|
||||
switch (piece) {
|
||||
case 7:
|
||||
promotedTo = Piece.BLACK_KNIGHT;
|
||||
break;
|
||||
case 4:
|
||||
promotedTo = Piece.BLACK_QUEEN;
|
||||
break;
|
||||
case 5:
|
||||
promotedTo = Piece.BLACK_ROOK;
|
||||
break;
|
||||
case 6:
|
||||
promotedTo = Piece.BLACK_BISHOP;
|
||||
break;
|
||||
case 3:
|
||||
promotedTo = Piece.WHITE_KNIGHT;
|
||||
break;
|
||||
case 0:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
break;
|
||||
case 1:
|
||||
promotedTo = Piece.WHITE_ROOK;
|
||||
break;
|
||||
case 2:
|
||||
promotedTo = Piece.WHITE_BISHOP;
|
||||
break;
|
||||
default:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
}
|
||||
Move promotionMove = new Move(origin, destination, promotedTo);
|
||||
playMove(promotionMove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the current view
|
||||
*
|
||||
* @brief Creates a new gameboard from the start pos and playes moves until it
|
||||
* reaches the viewPointer
|
||||
*/
|
||||
public void loadView() {
|
||||
this.board = new Board();
|
||||
this.board.loadFromFen(this.startPosFen);
|
||||
for (int i = 0; i < this.viewPointer; i++) {
|
||||
this.board.doMove(this.movelist.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMate() {
|
||||
return board.isMated();
|
||||
}
|
||||
|
|
@ -234,45 +297,6 @@ public class Game {
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void doPromotionMove(int piece, Square origin, Square destination) {
|
||||
System.out.println(piece);
|
||||
Piece promotedTo;
|
||||
switch (piece) {
|
||||
case 7:
|
||||
promotedTo = Piece.BLACK_KNIGHT;
|
||||
break;
|
||||
case 4:
|
||||
promotedTo = Piece.BLACK_QUEEN;
|
||||
break;
|
||||
case 5:
|
||||
promotedTo = Piece.BLACK_ROOK;
|
||||
break;
|
||||
case 6:
|
||||
promotedTo = Piece.BLACK_BISHOP;
|
||||
break;
|
||||
case 3:
|
||||
promotedTo = Piece.WHITE_KNIGHT;
|
||||
break;
|
||||
case 0:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
break;
|
||||
case 1:
|
||||
promotedTo = Piece.WHITE_ROOK;
|
||||
break;
|
||||
case 2:
|
||||
promotedTo = Piece.WHITE_BISHOP;
|
||||
break;
|
||||
default:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
}
|
||||
Move promotionMove = new Move(origin, destination, promotedTo);
|
||||
playMove(promotionMove);
|
||||
}
|
||||
|
||||
public void setModus(String modus) {
|
||||
this.modus = modus;
|
||||
}
|
||||
|
||||
public Clock getClock() {
|
||||
return this.clock;
|
||||
}
|
||||
|
|
@ -302,7 +326,6 @@ public class Game {
|
|||
}
|
||||
|
||||
public Board getBoard() {
|
||||
// TODO Auto-generated method stub
|
||||
return this.board;
|
||||
}
|
||||
|
||||
|
|
@ -311,11 +334,24 @@ public class Game {
|
|||
return board.getFen();
|
||||
}
|
||||
|
||||
// public Square getSelectedSquare() {
|
||||
// return this.getSelectedSquare();
|
||||
// }
|
||||
|
||||
public String getUnicodeFromMove(Move move) {
|
||||
return board.getPiece(move.getTo()).getFanSymbol().toUpperCase();
|
||||
}
|
||||
|
||||
public void setViewPointer(int i) {
|
||||
this.viewPointer = i;
|
||||
}
|
||||
|
||||
public int getViewPointer() {
|
||||
return this.viewPointer;
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getRemovedPieces() {
|
||||
return removedPieces;
|
||||
}
|
||||
|
||||
public boolean isRotieren() {
|
||||
return rotieren;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
package de.mannheim.th.chess.domain;
|
||||
|
||||
/**
|
||||
* Ueberprueft, ob ein Zug gueltig ist.
|
||||
*/
|
||||
public class MoveChecker{
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
package de.mannheim.th.chess.domain;
|
||||
|
||||
/**
|
||||
* Liest einen Zug ein.
|
||||
*/
|
||||
public class MoveReader{
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package de.mannheim.th.chess.model;
|
||||
|
||||
/**
|
||||
* Speichert Spielstaende, bisher gespielte Spiele und weiteres in einem File.
|
||||
*/
|
||||
public class Database{
|
||||
|
||||
public Database() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package de.mannheim.th.chess.ui;
|
||||
|
||||
import de.mannheim.th.chess.model.Database;
|
||||
|
||||
/**
|
||||
* ???
|
||||
*/
|
||||
public class Creator{
|
||||
|
||||
private Database database = new Database();
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package de.mannheim.th.chess.ui;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
|
||||
/**
|
||||
* Zeigt das Spielbrett mit den Stats rechts daneben an.
|
||||
*/
|
||||
public class GameWindow{
|
||||
|
||||
//private Game gamelogic = new Game();
|
||||
|
||||
public GameWindow() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,105 +4,158 @@ import javax.swing.JFrame;
|
|||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
// import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
|
||||
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;
|
||||
import javax.swing.JButton;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.awt.Color;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JPanel contentPane;
|
||||
// private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public MainFrame() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JPanel contentPane;
|
||||
private Game game;
|
||||
|
||||
setBackground(Color.LIGHT_GRAY);
|
||||
setResizable(true);
|
||||
setAlwaysOnTop(true);
|
||||
setTitle("Schach");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 500, 500);
|
||||
/**
|
||||
* Create the frame.
|
||||
* @throws IOException
|
||||
*/
|
||||
public MainFrame() {
|
||||
|
||||
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBackground(new Color(90, 90, 90));
|
||||
contentPane.setForeground(Color.BLACK);
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setBackground(Color.LIGHT_GRAY);
|
||||
setResizable(true);
|
||||
setAlwaysOnTop(true);
|
||||
setTitle("Schach");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 500, 500);
|
||||
|
||||
setContentPane(contentPane);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBackground(new Color(90, 90, 90));
|
||||
contentPane.setForeground(Color.BLACK);
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||
setContentPane(contentPane);
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(10));
|
||||
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||
|
||||
JLabel lblNewLabel = new JLabel("Schach");
|
||||
lblNewLabel.setForeground(Color.BLACK);
|
||||
lblNewLabel.setFont(new Font("Serif", Font.BOLD, 60));
|
||||
lblNewLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(lblNewLabel);
|
||||
contentPane.add(Box.createVerticalStrut(10));
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(10));
|
||||
JLabel lblNewLabel = new JLabel("Schach");
|
||||
lblNewLabel.setForeground(Color.BLACK);
|
||||
lblNewLabel.setFont(new Font("Serif", Font.BOLD, 60));
|
||||
lblNewLabel.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(lblNewLabel);
|
||||
|
||||
JLabel lblNewLabel_1 = new JLabel("by Dominik, Marius und Matias");
|
||||
lblNewLabel_1.setFont(new Font("Calibri", Font.ITALIC, 24));
|
||||
lblNewLabel_1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(lblNewLabel_1);
|
||||
contentPane.add(Box.createVerticalStrut(10));
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(75));
|
||||
JLabel lblNewLabel_1 = new JLabel("by Dominik, Marius und Matias");
|
||||
lblNewLabel_1.setFont(new Font("Calibri", Font.ITALIC, 24));
|
||||
lblNewLabel_1.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(lblNewLabel_1);
|
||||
|
||||
JButton btnNewButton = new JButton("Neues Spiel starten");
|
||||
|
||||
btnNewButton.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton.setForeground(Color.BLACK);
|
||||
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
btnNewButton.addActionListener(new ActionListener() {
|
||||
contentPane.add(Box.createVerticalStrut(75));
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JButton btnNewButton = new JButton("Neues Spiel starten");
|
||||
|
||||
ModeSelectionFrame ms = new ModeSelectionFrame();
|
||||
btnNewButton.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton.setForeground(Color.BLACK);
|
||||
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton.setAlignmentX(CENTER_ALIGNMENT);
|
||||
btnNewButton.addActionListener(new ActionListener() {
|
||||
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
openSelectModeFrame();
|
||||
}
|
||||
|
||||
});
|
||||
contentPane.add(btnNewButton);
|
||||
});
|
||||
contentPane.add(btnNewButton);
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
JButton btnNewButton_2 = new JButton("App beenden");
|
||||
|
||||
btnNewButton_2.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton_2.setForeground(Color.BLACK);
|
||||
btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
btnNewButton_2.addActionListener(new ActionListener() {
|
||||
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);
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
});
|
||||
JButton btnNewButton_2 = new JButton("App beenden");
|
||||
|
||||
contentPane.add(btnNewButton_2);
|
||||
setVisible(true);
|
||||
}
|
||||
btnNewButton_2.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton_2.setForeground(Color.BLACK);
|
||||
btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton_2.setAlignmentX(CENTER_ALIGNMENT);
|
||||
btnNewButton_2.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
contentPane.add(btnNewButton_2);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the spielframe and game in playmode
|
||||
* @throws IOException
|
||||
*/
|
||||
public void startGame() {
|
||||
if (this.game != null) {
|
||||
//this.game.stopClock();
|
||||
new SpielFrame(this.game);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the spielframe and game in view mode
|
||||
*/
|
||||
public void startView() {
|
||||
if (this.game != null) {
|
||||
this.game.stopClock();
|
||||
SpielFrame sf = new SpielFrame(this.game);
|
||||
sf.setMode(SpielFrame.BoardMode.finished);
|
||||
sf.enableControlPanelButtons();
|
||||
}
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* opens the selectmodeframe
|
||||
*/
|
||||
private void openSelectModeFrame() {
|
||||
new ModeSelectionFrame(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the pgnselectorframe
|
||||
*/
|
||||
private void openPgnSelectFrame() {
|
||||
new PGNLoaderFrame(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,135 +3,130 @@ package de.mannheim.th.chess.ui;
|
|||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
// import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.controller.ButtonFileLoaderListener;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
|
||||
public class ModeSelectionFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final JPanel contentPane;
|
||||
private final ArrayList<Game> spiele = new ArrayList<>();
|
||||
private String fen;
|
||||
// private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
public ModeSelectionFrame() {
|
||||
// Frame-Eigenschaften
|
||||
setTitle("Modusauswahl");
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setBounds(100, 100, 500, 500);
|
||||
setResizable(true);
|
||||
setAlwaysOnTop(true);
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final JPanel contentPane;
|
||||
private String fen;
|
||||
|
||||
// Panel konfigurieren
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBackground(new Color(90, 90, 90));
|
||||
contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
|
||||
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||
setContentPane(contentPane);
|
||||
public ModeSelectionFrame(MainFrame mf) {
|
||||
// Frame-Eigenschaften
|
||||
setTitle("Modusauswahl");
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setBounds(100, 100, 600, 600);
|
||||
setResizable(true);
|
||||
setAlwaysOnTop(true);
|
||||
|
||||
// Überschrift
|
||||
JLabel jl = new JLabel("Welchen Modus wollen Sie spielen?");
|
||||
jl.setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl.setForeground(Color.BLACK);
|
||||
jl.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(jl);
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
// Panel konfigurieren
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBackground(new Color(90, 90, 90));
|
||||
contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
|
||||
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||
setContentPane(contentPane);
|
||||
|
||||
// Modusauswahl
|
||||
String[] modi = {"Blitz", "Schnellschach", "Klassisch"};
|
||||
JComboBox<String> jcb1 = new JComboBox<>(modi);
|
||||
jcb1 .setMaximumSize(new Dimension(150, 30));
|
||||
jcb1 .setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(jcb1 );
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
// Überschrift
|
||||
JLabel jl = new JLabel("Welchen Modus wollen Sie spielen?");
|
||||
jl.setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl.setForeground(Color.BLACK);
|
||||
jl.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(jl);
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
// Spielbrettdrehen
|
||||
JLabel jl2 = new JLabel("Soll das Spielbrett nach jedem Zug gedreht werden?");
|
||||
jl2 .setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl2 .setForeground(Color.BLACK);
|
||||
jl2 .setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(jl2 );
|
||||
// Modusauswahl
|
||||
String[] modi = { "Blitz", "Schnellschach", "Klassisch" };
|
||||
JComboBox<String> jcb1 = new JComboBox<>(modi);
|
||||
jcb1.setMaximumSize(new Dimension(150, 30));
|
||||
jcb1.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(jcb1);
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
JCheckBox jb1 = new JCheckBox();
|
||||
jb1.setOpaque(false);
|
||||
jb1.setFocusPainted(false);
|
||||
jb1.setForeground(Color.BLACK);
|
||||
jb1 .setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
jb1 .setMaximumSize(new Dimension(30, 30));
|
||||
contentPane.add(jb1 );
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
// Spielbrettdrehen
|
||||
JLabel jl2 = new JLabel("Soll das Spielbrett nach jedem Zug gedreht werden?");
|
||||
jl2.setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl2.setForeground(Color.BLACK);
|
||||
jl2.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(jl2);
|
||||
|
||||
// Zurücknahmeoption
|
||||
JLabel jl3 = new JLabel("Sollen Zurücknahmen erlaubt sein?");
|
||||
jl3.setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl3.setForeground(Color.BLACK);
|
||||
jl3.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(jl3);
|
||||
JCheckBox jb1 = new JCheckBox();
|
||||
jb1.setOpaque(false);
|
||||
jb1.setFocusPainted(false);
|
||||
jb1.setForeground(Color.BLACK);
|
||||
jb1.setAlignmentX(CENTER_ALIGNMENT);
|
||||
jb1.setMaximumSize(new Dimension(30, 30));
|
||||
contentPane.add(jb1);
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
JCheckBox jb2 = new JCheckBox();
|
||||
jb2.setOpaque(false);
|
||||
jb2.setFocusPainted(false);
|
||||
jb2.setForeground(Color.BLACK);
|
||||
jb2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
jb2.setMaximumSize(new Dimension(30, 30));
|
||||
contentPane.add(jb2);
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
// Zurücknahmeoption
|
||||
JLabel jl3 = new JLabel("Sollen Zurücknahmen erlaubt sein?");
|
||||
jl3.setFont(new Font("Calibri", Font.BOLD, 20));
|
||||
jl3.setForeground(Color.BLACK);
|
||||
jl3.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(jl3);
|
||||
|
||||
JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden");
|
||||
|
||||
btnNewButton_1.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton_1.setForeground(Color.BLACK);
|
||||
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
btnNewButton_1.addActionListener(new ButtonFileLoaderListener(this));
|
||||
JCheckBox jb2 = new JCheckBox();
|
||||
jb2.setOpaque(false);
|
||||
jb2.setFocusPainted(false);
|
||||
jb2.setForeground(Color.BLACK);
|
||||
jb2.setAlignmentX(CENTER_ALIGNMENT);
|
||||
jb2.setMaximumSize(new Dimension(30, 30));
|
||||
contentPane.add(jb2);
|
||||
|
||||
contentPane.add(btnNewButton_1);
|
||||
|
||||
contentPane.add(Box.createVerticalStrut(25));
|
||||
contentPane.add(Box.createVerticalStrut(15));
|
||||
|
||||
// Spiel starten Button
|
||||
JButton btnNewButton = new JButton("Spiel starten");
|
||||
btnNewButton .setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton .setForeground(Color.BLACK);
|
||||
btnNewButton .setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton .setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
contentPane.add(btnNewButton );
|
||||
JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden");
|
||||
|
||||
// Button-Listener
|
||||
btnNewButton .addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String modus = (String) jcb1.getSelectedItem();
|
||||
boolean rotieren = jb1.isSelected();
|
||||
boolean zuruecknahme = jb2.isSelected();
|
||||
btnNewButton_1.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton_1.setForeground(Color.BLACK);
|
||||
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton_1.setAlignmentX(CENTER_ALIGNMENT);
|
||||
btnNewButton_1.addActionListener(new ButtonFileLoaderListener(this));
|
||||
|
||||
Game game = new Game(modus, rotieren, zuruecknahme, fen);
|
||||
|
||||
spiele.add(game);
|
||||
contentPane.add(btnNewButton_1);
|
||||
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
contentPane.add(Box.createVerticalStrut(25));
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void setFen(String fen) {
|
||||
this.fen = fen;
|
||||
}
|
||||
// Spiel starten Button
|
||||
JButton btnNewButton = new JButton("Spiel starten");
|
||||
btnNewButton.setBackground(Color.LIGHT_GRAY);
|
||||
btnNewButton.setForeground(Color.BLACK);
|
||||
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||
btnNewButton.setAlignmentX(CENTER_ALIGNMENT);
|
||||
contentPane.add(btnNewButton);
|
||||
|
||||
// Button-Listener
|
||||
btnNewButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String modus = (String) jcb1.getSelectedItem();
|
||||
boolean rotieren = jb1.isSelected();
|
||||
boolean zuruecknahme = jb2.isSelected();
|
||||
|
||||
Game game = new Game(modus, rotieren, zuruecknahme, fen);
|
||||
mf.setGame(game);
|
||||
mf.startGame();
|
||||
|
||||
// spiele.add(game);
|
||||
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void setFen(String fen) {
|
||||
this.fen = fen;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,154 @@
|
|||
package de.mannheim.th.chess.ui;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JProgressBar;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.game.Game;
|
||||
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||
import com.github.bhlangonijr.chesslib.pgn.PgnLoadListener;
|
||||
|
||||
public class PGNLoaderFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(PGNLoaderFrame.class);
|
||||
|
||||
private PgnHolder pgn;
|
||||
private File selectedFile;
|
||||
private List<Game> games;
|
||||
private DefaultListModel<String> gameListModel;
|
||||
private JPanel contentPane;
|
||||
private JList<String> gameList;
|
||||
private JProgressBar progressBar;
|
||||
|
||||
/**
|
||||
* Opens the PNGLoaderFrame
|
||||
*/
|
||||
public PGNLoaderFrame(MainFrame mf) {
|
||||
|
||||
// ----- Style -----
|
||||
setResizable(true);
|
||||
setAlwaysOnTop(true);
|
||||
setTitle("PGNLoader");
|
||||
setBounds(100, 100, 500, 500);
|
||||
|
||||
// ----- contentPane -----
|
||||
contentPane = new JPanel();
|
||||
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||
|
||||
setContentPane(contentPane);
|
||||
|
||||
// ----- FileSelector -----
|
||||
JButton fileSelectButton = new JButton("Select File");
|
||||
fileSelectButton.addActionListener(e -> {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
int returnValue = fileChooser.showOpenDialog(null);
|
||||
if (returnValue == JFileChooser.APPROVE_OPTION) {
|
||||
selectedFile = fileChooser.getSelectedFile();
|
||||
}
|
||||
});
|
||||
contentPane.add(fileSelectButton);
|
||||
|
||||
// ----- LoadButton -----
|
||||
JButton loadPgnButton = new JButton("Load file");
|
||||
loadPgnButton.addActionListener(e -> loadFile());
|
||||
contentPane.add(loadPgnButton);
|
||||
|
||||
// ----- SelectionList -----
|
||||
gameListModel = new DefaultListModel<>();
|
||||
gameList = new JList<>(gameListModel);
|
||||
gameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
gameList.setVisibleRowCount(5);
|
||||
JScrollPane scrollPane = new JScrollPane(gameList);
|
||||
contentPane.add(scrollPane);
|
||||
|
||||
// ----- ProgressBar -----
|
||||
progressBar = new JProgressBar(0, 100);
|
||||
progressBar.setValue(0);
|
||||
progressBar.setStringPainted(true);
|
||||
contentPane.add(progressBar);
|
||||
|
||||
// ----- StartButton -----
|
||||
JButton startGameButton = new JButton("Starte Spiel");
|
||||
startGameButton.addActionListener(e -> {
|
||||
int index = gameList.getSelectedIndex();
|
||||
de.mannheim.th.chess.domain.Game game = new de.mannheim.th.chess.domain.Game(games.get(index).getHalfMoves());
|
||||
|
||||
mf.setGame(game);
|
||||
mf.startView();
|
||||
});
|
||||
contentPane.add(startGameButton);
|
||||
|
||||
this.setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
private void loadFile() {
|
||||
if (this.selectedFile != null) {
|
||||
pgn = new PgnHolder(this.selectedFile.getAbsolutePath());
|
||||
|
||||
LoadPGNWorker loadPGNWorker = new LoadPGNWorker();
|
||||
loadPGNWorker.addPropertyChangeListener(e -> {
|
||||
progressBar.setIndeterminate(true);
|
||||
});
|
||||
|
||||
pgn.getListener().add(loadPGNWorker);
|
||||
loadPGNWorker.execute();
|
||||
|
||||
gameList.revalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private class LoadPGNWorker extends SwingWorker<Integer, Integer> implements PgnLoadListener {
|
||||
|
||||
@Override
|
||||
protected Integer doInBackground() throws Exception {
|
||||
try {
|
||||
pgn.loadPgn();
|
||||
games = pgn.getGames();
|
||||
int totalGames = games.size();
|
||||
for (int i = 0; i < totalGames; i++) {
|
||||
publish(i);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("Could not load pgn file!");
|
||||
}
|
||||
return pgn.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<Integer> chunks) {
|
||||
for (Integer index : chunks) {
|
||||
gameListModel.addElement("Game: " + index);
|
||||
setProgress(Math.min(90, index * 100 / games.size()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
setProgress(100);
|
||||
progressBar.setValue(100);
|
||||
progressBar.setIndeterminate(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyProgress(int games) {
|
||||
setProgress(Math.min(90, games));
|
||||
progressBar.setValue(Math.min(90, games));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -3,157 +3,152 @@ package de.mannheim.th.chess.utl;
|
|||
/**
|
||||
* Zeigt die Zeitangabe während eines Spiels eines Spielers an.
|
||||
*/
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class Clock extends Thread implements Runnable {
|
||||
private volatile boolean whiteToMove = true;
|
||||
private volatile boolean gameHasFinished = false;
|
||||
private static final Logger clockLogger = LogManager.getLogger(Clock.class);
|
||||
private int minutes;
|
||||
private StringBuilder clockShower;
|
||||
private JLabel clock1, clock2;
|
||||
private volatile boolean whiteToMove = true;
|
||||
private volatile boolean gameHasFinished = false;
|
||||
private static final Logger clockLogger = LogManager.getLogger(Clock.class);
|
||||
private int minutes;
|
||||
private StringBuilder clockShower;
|
||||
private JLabel clock1, clock2;
|
||||
|
||||
public Clock(String mode) {
|
||||
|
||||
setMode(mode);
|
||||
}
|
||||
public Clock(String mode) {
|
||||
|
||||
public void pressClock() {
|
||||
whiteToMove = !whiteToMove;
|
||||
if (whiteToMove) {
|
||||
clockLogger.info("Weiß ist am Zug");
|
||||
} else {
|
||||
clockLogger.info("Schwarz ist am Zug");
|
||||
}
|
||||
}
|
||||
setMode(mode);
|
||||
}
|
||||
|
||||
public void endGame() {
|
||||
gameHasFinished = true;
|
||||
}
|
||||
public void pressClock() {
|
||||
whiteToMove = !whiteToMove;
|
||||
if (whiteToMove) {
|
||||
clockLogger.info("Weiß ist am Zug");
|
||||
} else {
|
||||
clockLogger.info("Schwarz ist am Zug");
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
// JFrame clockFrame = new JFrame("Clock");
|
||||
//
|
||||
// JPanel player1Panel = new JPanel();
|
||||
// player1Panel.setBackground(Color.BLACK);
|
||||
// JPanel player2Panel = new JPanel();
|
||||
// player2Panel.setBackground(Color.BLACK);
|
||||
// clockFrame.setBounds(1000, 500, 10000, 10000);
|
||||
// clockFrame.setLayout(new BorderLayout());
|
||||
clock1 = new JLabel("" + minutes + ":00 ");
|
||||
clock1.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||
clock1.setForeground(Color.BLACK);
|
||||
clock1.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||
clock1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
clock2 = new JLabel("" + minutes + ":00 ");
|
||||
clock2.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||
clock2.setForeground(Color.BLACK);
|
||||
clock2.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||
clock2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
// player1Panel.add(clock1);
|
||||
// player2Panel.add(clock2);
|
||||
// JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, player1Panel, player2Panel);
|
||||
// split.setFont(new Font("Arial", Font.BOLD, 50));
|
||||
// clockFrame.add(split);
|
||||
public void endGame() {
|
||||
gameHasFinished = true;
|
||||
}
|
||||
|
||||
var min1 = new AtomicInteger(minutes);
|
||||
var sec1 = new AtomicInteger(0);
|
||||
var min2 = new AtomicInteger(minutes);
|
||||
var sec2 = new AtomicInteger(0);
|
||||
public void run() {
|
||||
// JFrame clockFrame = new JFrame("Clock");
|
||||
//
|
||||
// JPanel player1Panel = new JPanel();
|
||||
// player1Panel.setBackground(Color.BLACK);
|
||||
// JPanel player2Panel = new JPanel();
|
||||
// player2Panel.setBackground(Color.BLACK);
|
||||
// clockFrame.setBounds(1000, 500, 10000, 10000);
|
||||
// clockFrame.setLayout(new BorderLayout());
|
||||
clock1 = new JLabel("" + minutes + ":00 ");
|
||||
clock1.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||
clock1.setForeground(Color.BLACK);
|
||||
clock1.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||
clock1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
var t = new Timer(1000, (ae) -> {
|
||||
clock2 = new JLabel("" + minutes + ":00 ");
|
||||
clock2.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||
clock2.setForeground(Color.BLACK);
|
||||
clock2.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||
clock2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
// player1Panel.add(clock1);
|
||||
// player2Panel.add(clock2);
|
||||
// JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, player1Panel,
|
||||
// player2Panel);
|
||||
// split.setFont(new Font("Arial", Font.BOLD, 50));
|
||||
// clockFrame.add(split);
|
||||
|
||||
if (!gameHasFinished) {
|
||||
var min1 = new AtomicInteger(minutes);
|
||||
var sec1 = new AtomicInteger(0);
|
||||
var min2 = new AtomicInteger(minutes);
|
||||
var sec2 = new AtomicInteger(0);
|
||||
|
||||
clockShower = new StringBuilder();
|
||||
if (whiteToMove) {
|
||||
if (sec1.intValue() == 00) {
|
||||
sec1.set(60);
|
||||
min1.decrementAndGet();
|
||||
}
|
||||
if (min1.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(min1.get());
|
||||
clockShower.append(":");
|
||||
if (sec1.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(sec1.decrementAndGet());
|
||||
clock1.setText(clockShower.toString());
|
||||
var t = new Timer(1000, (ae) -> {
|
||||
|
||||
} else {
|
||||
if (sec2.intValue() == 00) {
|
||||
sec2.set(60);
|
||||
min2.decrementAndGet();
|
||||
}
|
||||
if (min2.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(min2.get());
|
||||
clockShower.append(":");
|
||||
if (sec2.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(sec2.decrementAndGet());
|
||||
clock2.setText(clockShower.toString());
|
||||
}
|
||||
//sp.repaint();
|
||||
if ((sec1.intValue() == 0 && min1.intValue() == 0) || (sec2.intValue() == 0 && min2.intValue() == 0)) {
|
||||
endGame();
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!gameHasFinished) {
|
||||
|
||||
t.start();
|
||||
}
|
||||
clockShower = new StringBuilder();
|
||||
if (whiteToMove) {
|
||||
if (sec1.intValue() == 00) {
|
||||
sec1.set(60);
|
||||
min1.decrementAndGet();
|
||||
}
|
||||
if (min1.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(min1.get());
|
||||
clockShower.append(":");
|
||||
if (sec1.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(sec1.decrementAndGet());
|
||||
clock1.setText(clockShower.toString());
|
||||
|
||||
private void setMode(String mode) {
|
||||
switch (mode.toLowerCase()) {
|
||||
case "blitz":
|
||||
minutes = 5;
|
||||
clockLogger.info("Neue Blitz-Uhr wurde erstellt");
|
||||
break;
|
||||
case "schnellschach":
|
||||
minutes = 10;
|
||||
clockLogger.info("Neue Schnellschach-Uhr wurde erstellt");
|
||||
break;
|
||||
case "klassisch":
|
||||
minutes = 120;
|
||||
clockLogger.info("Neue klassische Schachuhr wurde erstellt");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void switchClock() {
|
||||
whiteToMove = !whiteToMove;
|
||||
}
|
||||
} else {
|
||||
if (sec2.intValue() == 00) {
|
||||
sec2.set(60);
|
||||
min2.decrementAndGet();
|
||||
}
|
||||
if (min2.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(min2.get());
|
||||
clockShower.append(":");
|
||||
if (sec2.intValue() < 10) {
|
||||
clockShower.append("0");
|
||||
}
|
||||
clockShower.append(sec2.decrementAndGet());
|
||||
clock2.setText(clockShower.toString());
|
||||
}
|
||||
// sp.repaint();
|
||||
if ((sec1.intValue() == 0 && min1.intValue() == 0) || (sec2.intValue() == 0 && min2.intValue() == 0)) {
|
||||
endGame();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public JLabel getClock1() {
|
||||
t.start();
|
||||
}
|
||||
|
||||
return clock1;
|
||||
}
|
||||
private void setMode(String mode) {
|
||||
switch (mode.toLowerCase()) {
|
||||
case "blitz":
|
||||
minutes = 5;
|
||||
clockLogger.info("Neue Blitz-Uhr wurde erstellt");
|
||||
break;
|
||||
case "schnellschach":
|
||||
minutes = 10;
|
||||
clockLogger.info("Neue Schnellschach-Uhr wurde erstellt");
|
||||
break;
|
||||
case "klassisch":
|
||||
minutes = 120;
|
||||
clockLogger.info("Neue klassische Schachuhr wurde erstellt");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public JLabel getClock2() {
|
||||
|
||||
return clock2;
|
||||
}
|
||||
public void switchClock() {
|
||||
whiteToMove = !whiteToMove;
|
||||
}
|
||||
|
||||
}
|
||||
public JLabel getClock1() {
|
||||
|
||||
return clock1;
|
||||
}
|
||||
|
||||
public JLabel getClock2() {
|
||||
|
||||
return clock2;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
package de.mannheim.th.chess.utl;
|
||||
|
||||
import de.mannheim.th.chess.model.Database;
|
||||
|
||||
/**
|
||||
* Liest ein schon vordefinierten Spielstand ein.
|
||||
*/
|
||||
public class GameReader{
|
||||
|
||||
private Database database = new Database();
|
||||
|
||||
}
|
||||
|
|
@ -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