stuckd 2025-06-24 10:17:48 +02:00
parent e8bb9e1eee
commit 724cde3fd0
2 changed files with 120 additions and 21 deletions

View File

@ -7,6 +7,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
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;
@ -157,6 +159,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.
* *
@ -170,16 +177,50 @@ public class Game {
* Retrieves a list of legal moveable squares for a given square. * Retrieves a list of legal moveable squares for a given square.
* *
* @param square the Square from which to retrieve legal moveable squares * @param square the Square from which to retrieve legal moveable squares
* @return a List of Square objects representing the legal moveable squares from * @return a List of Square objects representing the legal moveable squares
* the specified square. * from the specified square.
*/ */
public List<Square> getLegalMoveableSquares(Square square) { public List<Square> getLegalMoveableSquares(Square square) {
return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).map(move -> move.getTo()) return this.board.legalMoves().stream()
.filter(move -> move.getFrom() == square)
.map(move -> move.getTo())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public String getUnicodeFromMove(Move move) { public void doPromotionMove(int piece, Square origin, Square destination) {
return board.getPiece(move.getTo()).getFanSymbol().toUpperCase(); 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() {

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.move.Move; import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList; import com.github.bhlangonijr.chesslib.move.MoveList;
@ -24,6 +25,7 @@ import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
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;
@ -243,7 +245,32 @@ 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));
b.addActionListener(new ButtonMovePieceListener(this, this.game, move)); 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;
showDraw();
} else if (game.isMate()) {
game.stopClock();
mode = BoardMode.finished;
showWin(game.getActivePlayer());
}
mode = BoardMode.normal;
setCursor(null);
erstelleBrett();
}
});
} }
break; break;
@ -288,6 +315,37 @@ 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];
}
private JPanel getUiPlayerTwo() { private JPanel getUiPlayerTwo() {
JPanel playerTwo = new JPanel(); JPanel playerTwo = new JPanel();