From e043546ee41d8732a21290000054429cd75536d7 Mon Sep 17 00:00:00 2001 From: <3020511@stud.hs-mannheim.de> Date: Thu, 19 Jun 2025 23:04:51 +0200 Subject: [PATCH] Promotion mit entsprechendem Auswahlfenster implementiert --- .../de/mannheim/th/chess/domain/Game.java | 43 +++++++++++++++++++ .../de/mannheim/th/chess/ui/SpielFrame.java | 43 ++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) 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 a375d29..c716dc6 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.stream.Collectors; import com.github.bhlangonijr.chesslib.Board; +import com.github.bhlangonijr.chesslib.Piece; +import com.github.bhlangonijr.chesslib.Rank; import com.github.bhlangonijr.chesslib.Side; import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.move.Move; @@ -125,6 +127,11 @@ public class Game { public void stopClock() { clock.endGame(); } + + public boolean isPromotionMove(Move move) { + return ((move.getTo().getRank().equals(Rank.RANK_8) || move.getTo().getRank().equals(Rank.RANK_1)) && + (board.getPiece(move.getFrom()) == Piece.BLACK_PAWN || board.getPiece(move.getFrom()) == Piece.WHITE_PAWN)); + } /** * Retrieves a list of all legal moveable squares from the current board state. @@ -151,6 +158,42 @@ public class Game { .map(move -> move.getTo()) .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); + board.doMove(promotionMove); + movelist.add(promotionMove); + } public String toFEN() { board.toString(); 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 a8bd089..6d11ac5 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -3,6 +3,7 @@ package de.mannheim.th.chess.ui; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import com.github.bhlangonijr.chesslib.Piece; import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.game.Player; import com.github.bhlangonijr.chesslib.move.Move; @@ -16,6 +17,7 @@ import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; +import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @@ -293,10 +295,18 @@ public class SpielFrame extends JFrame { final Move move = new Move(selectedSquare, square); b.setEnabled(true); b.setBackground(new Color(230, 100, 100)); + for (ActionListener al : b.getActionListeners()) { + b.removeActionListener(al); + } b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - game.playMove(move); + if(game.isPromotionMove(move)) { + game.doPromotionMove(showPromotion(), selectedSquare, square); + + } else { + game.playMove(move); + } if (game.isDraw()) { game.stopClock(); mode = BoardMode.finished; @@ -354,5 +364,36 @@ public class SpielFrame extends JFrame { frame.add(jl); frame.setVisible(true); } + + private int showPromotion() { + final int[] result = {-1}; + + + JDialog dialog = new JDialog(this, "Wähle eine Figur", true); + dialog.setLayout(new GridLayout(2, 2)); + dialog.setSize(300, 200); + + int[] pictures = {81, 82, 66, 78, 113, 114, 98, 110}; + + + for (int i = 0; i < 4; i++) { + int index = (game.getActivePlayer() - 1) * 4 + i; + JButton jb = new JButton(); + 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(); + }); + dialog.add(jb); + } + + dialog.setLocationRelativeTo(null); + dialog.setVisible(true); + + return result[0]; + } + }