kida fixed promotion

buttonActions
dstuck 2025-06-21 16:09:55 +02:00
parent 8cb621d804
commit 64cf321511
2 changed files with 58 additions and 51 deletions

View File

@ -22,7 +22,11 @@ public class ButtonMovePieceListener implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
this.game.playMove(this.mv); if (game.isPromotionMove(this.mv))
game.doPromotionMove(this.sf.showPromotion(), mv.getFrom(), mv.getTo());
else
this.game.playMove(this.mv);
if (this.game.isDraw()) { if (this.game.isDraw()) {
this.game.stopClock(); this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished); this.sf.setBoardMode(BoardMode.finished);

View File

@ -96,18 +96,18 @@ public class Game {
} }
public boolean isMate() { public boolean isMate() {
return board.isMated(); return board.isMated();
} }
public boolean isDraw() { public boolean isDraw() {
return board.isDraw(); return board.isDraw();
} }
public int getActivePlayer() { public int getActivePlayer() {
if (board.getSideToMove() == Side.WHITE) { if (board.getSideToMove() == Side.WHITE) {
return 1; return 1;
} }
return 2; return 2;
} }
/** /**
@ -125,13 +125,13 @@ public class Game {
} }
public void stopClock() { public void stopClock() {
clock.endGame(); clock.endGame();
} }
public boolean isPromotionMove(Move move) { public boolean isPromotionMove(Move move) {
return ((move.getTo().getRank().equals(Rank.RANK_8) || move.getTo().getRank().equals(Rank.RANK_1)) && 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)); (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.
@ -160,43 +160,46 @@ public class Game {
} }
public void doPromotionMove(int piece, Square origin, Square destination) { public void doPromotionMove(int piece, Square origin, Square destination) {
System.out.println(piece); System.out.println(piece);
Piece promotedTo; Piece promotedTo;
switch(piece) { switch (piece) {
case 7: case 7:
promotedTo = Piece.BLACK_KNIGHT; promotedTo = Piece.BLACK_KNIGHT;
break; break;
case 4: case 4:
promotedTo = Piece.BLACK_QUEEN; promotedTo = Piece.BLACK_QUEEN;
break; break;
case 5: case 5:
promotedTo = Piece.BLACK_ROOK; promotedTo = Piece.BLACK_ROOK;
break; break;
case 6: case 6:
promotedTo = Piece.BLACK_BISHOP; promotedTo = Piece.BLACK_BISHOP;
break; break;
case 3: case 3:
promotedTo = Piece.WHITE_KNIGHT; promotedTo = Piece.WHITE_KNIGHT;
break; break;
case 0: case 0:
promotedTo = Piece.WHITE_QUEEN; promotedTo = Piece.WHITE_QUEEN;
break; break;
case 1: case 1:
promotedTo = Piece.WHITE_ROOK; promotedTo = Piece.WHITE_ROOK;
break; break;
case 2: case 2:
promotedTo = Piece.WHITE_BISHOP; promotedTo = Piece.WHITE_BISHOP;
break; break;
default: default:
promotedTo = Piece.WHITE_QUEEN; promotedTo = Piece.WHITE_QUEEN;
} }
Move promotionMove = new Move(origin, destination, promotedTo); Move promotionMove = new Move(origin, destination, promotedTo);
board.doMove(promotionMove); playMove(promotionMove);
movelist.add(promotionMove);
} }
public String toFEN() { public String toFEN() {
board.toString(); board.toString();
return board.getFen(); return board.getFen();
} }
public Square getSelectedSquare() {
return this.getSelectedSquare();
}
} }