Promotion mit entsprechendem Auswahlfenster implementiert
parent
392d704729
commit
e043546ee4
|
|
@ -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;
|
||||
|
|
@ -126,6 +128,11 @@ public class Game {
|
|||
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.
|
||||
*
|
||||
|
|
@ -152,6 +159,42 @@ public class Game {
|
|||
.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();
|
||||
return board.getFen();
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
if(game.isPromotionMove(move)) {
|
||||
game.doPromotionMove(showPromotion(), selectedSquare, square);
|
||||
|
||||
} else {
|
||||
game.playMove(move);
|
||||
}
|
||||
if (game.isDraw()) {
|
||||
game.stopClock();
|
||||
mode = BoardMode.finished;
|
||||
|
|
@ -355,4 +365,35 @@ public class SpielFrame extends JFrame {
|
|||
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];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue