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 {
new MainFrame(); OpeningRecognizer.loadOpenings();
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;
@ -28,6 +29,8 @@ public class Game {
private Board board; private Board board;
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();
} }
@ -319,6 +345,10 @@ public class Game {
public int getViewPointer() { public int getViewPointer() {
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,8 +33,10 @@ 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);
@ -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);

File diff suppressed because it is too large Load Diff

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");
}
}