Geschlagene Figuren werden an der Seite angezeigt

main
Marius Gündel 2025-06-29 13:23:18 +02:00
parent 201497b4a8
commit 116e2c5e55
2 changed files with 56 additions and 5 deletions

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,10 @@ 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) {
removedPieces.add(removedPiece);
}
this.board.doMove(move); this.board.doMove(move);
this.movelist.add(move); this.movelist.add(move);
clock.pressClock(); clock.pressClock();
@ -116,7 +125,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,11 +166,15 @@ 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) {
removedPieces.add(removedPiece);
}
this.board.doMove(move); this.board.doMove(move);
this.movelist.add(move); this.movelist.add(move);
@ -320,6 +337,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

@ -6,6 +6,8 @@ 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;
@ -57,7 +59,7 @@ 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 ArrayList<String> anzeigeMoves = new ArrayList<String>(); private ArrayList<String> anzeigeMoves = new ArrayList<String>();
@ -305,7 +307,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();
}); });
@ -526,6 +527,7 @@ 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));
@ -534,10 +536,26 @@ public class SpielFrame extends JFrame {
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.setEditable(false);
whiteRemovedPieces.setBackground(new Color(75, 75, 75));
whiteRemovedPieces.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
whiteRemovedPieces.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 40));
whiteRemovedPieces.setForeground(Color.WHITE);
blackRemovedPieces = new JTextArea();
blackRemovedPieces.setEditable(false);
blackRemovedPieces.setBackground(new Color(75, 75, 75));
blackRemovedPieces.setBorder(BorderFactory.createLineBorder(Color.BLACK, 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.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
statistik.add(blackRemovedPieces);
statistik.add(scrollPane); statistik.add(scrollPane);
statistik.add(whiteRemovedPieces);
return statistik; return statistik;
} }
@ -554,6 +572,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());
} }