Promotion mit entsprechendem Auswahlfenster implementiert

buttonActions
Marius Gündel 2025-06-19 23:04:51 +02:00
parent 392d704729
commit e043546ee4
2 changed files with 85 additions and 1 deletions

View File

@ -4,6 +4,8 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.github.bhlangonijr.chesslib.Board; 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.Side;
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;
@ -126,6 +128,11 @@ public class Game {
clock.endGame(); 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. * Retrieves a list of all legal moveable squares from the current board state.
* *
@ -152,6 +159,42 @@ public class Game {
.collect(Collectors.toList()); .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() { public String toFEN() {
board.toString(); board.toString();
return board.getFen(); return board.getFen();

View File

@ -3,6 +3,7 @@ package de.mannheim.th.chess.ui;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Piece;
import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.game.Player; import com.github.bhlangonijr.chesslib.game.Player;
import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.Move;
@ -16,6 +17,7 @@ import java.awt.Font;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -293,10 +295,18 @@ public class SpielFrame extends JFrame {
final Move move = new Move(selectedSquare, square); final Move move = new Move(selectedSquare, square);
b.setEnabled(true); b.setEnabled(true);
b.setBackground(new Color(230, 100, 100)); b.setBackground(new Color(230, 100, 100));
for (ActionListener al : b.getActionListeners()) {
b.removeActionListener(al);
}
b.addActionListener(new ActionListener() { b.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(game.isPromotionMove(move)) {
game.doPromotionMove(showPromotion(), selectedSquare, square);
} else {
game.playMove(move); game.playMove(move);
}
if (game.isDraw()) { if (game.isDraw()) {
game.stopClock(); game.stopClock();
mode = BoardMode.finished; mode = BoardMode.finished;
@ -355,4 +365,35 @@ public class SpielFrame extends JFrame {
frame.setVisible(true); 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];
}
} }