From 116e2c5e55ba2167a420f862c1d6c7bd8b8c61c1 Mon Sep 17 00:00:00 2001 From: <3020511@stud.hs-mannheim.de> Date: Sun, 29 Jun 2025 13:23:18 +0200 Subject: [PATCH] Geschlagene Figuren werden an der Seite angezeigt --- .../de/mannheim/th/chess/domain/Game.java | 25 +++++++++++-- .../de/mannheim/th/chess/ui/SpielFrame.java | 36 +++++++++++++++++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/mannheim/th/chess/domain/Game.java b/src/main/java/de/mannheim/th/chess/domain/Game.java index d06cff5..f6581ac 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -1,5 +1,6 @@ package de.mannheim.th.chess.domain; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -28,6 +29,8 @@ public class Game { private Board board; private Clock clock; private boolean rotieren, zuruecknahme; + + ArrayList removedPieces; private MoveList movelist; private int viewPointer; @@ -43,6 +46,7 @@ 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(); @@ -63,6 +67,7 @@ public class Game { this.movelist = new MoveList(); clock = new Clock(modus); + removedPieces = new ArrayList<>(); } /** @@ -102,6 +107,10 @@ 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) { + removedPieces.add(removedPiece); + } this.board.doMove(move); this.movelist.add(move); clock.pressClock(); @@ -116,7 +125,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); + } } /** @@ -153,11 +166,15 @@ public class Game { /** * 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) { + removedPieces.add(removedPiece); + } this.board.doMove(move); this.movelist.add(move); @@ -319,6 +336,10 @@ public class Game { public int getViewPointer() { return this.viewPointer; } + + public ArrayList getRemovedPieces() { + return removedPieces; + } public boolean isRotieren() { return rotieren; diff --git a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java index 6fc5b4c..beb97e6 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -6,6 +6,8 @@ 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; @@ -57,7 +59,7 @@ public class SpielFrame extends JFrame { private HashMap belegungen = new HashMap<>(); private JPanel panelLinks, panelRechts, contentPane, controlPanel; private JButton undo, undo2, aufgeben, aufgeben2; - private JTextArea ausgabe; + private JTextArea ausgabe, blackRemovedPieces, whiteRemovedPieces; private Game game; private Clock clock; private ArrayList anzeigeMoves = new ArrayList(); @@ -305,7 +307,6 @@ 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(); }); @@ -525,7 +526,8 @@ public class SpielFrame extends JFrame { JPanel statistik = new JPanel(); 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)); @@ -533,11 +535,27 @@ public class SpielFrame extends JFrame { ausgabe.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20)); ausgabe.setForeground(Color.BLACK); 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); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + statistik.add(blackRemovedPieces); statistik.add(scrollPane); + statistik.add(whiteRemovedPieces); return statistik; } @@ -553,6 +571,18 @@ public class SpielFrame extends JFrame { for (String line : anzeigeMoves) { 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()); }