From cbad409db75f6b5a7d2acc6b6431c9f308d43d15 Mon Sep 17 00:00:00 2001 From: dstuck Date: Sat, 21 Jun 2025 16:09:55 +0200 Subject: [PATCH] kida fixed promotion --- .../controller/ButtonMovePieceListener.java | 7 +- .../de/mannheim/th/chess/domain/Game.java | 114 ++++++++++++------ 2 files changed, 79 insertions(+), 42 deletions(-) diff --git a/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java b/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java index ccd1178..83de36c 100644 --- a/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java +++ b/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java @@ -22,8 +22,11 @@ public class ButtonMovePieceListener implements ActionListener { @Override 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()) { this.game.stopClock(); this.sf.setBoardMode(BoardMode.finished); 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 c75b421..963e154 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -127,6 +127,33 @@ public class Game { this.board.undoMove(); this.movelist.removeLast(); } + /** + * Plays the move on the board and adds it to the movelist + * + * @param origin The square from wich it moves from. + * @param desination The square where it will move to. + */ + public void playMove(Square origin, Square desination) { + Move move = new Move(origin, desination); + this.board.doMove(move); + this.movelist.add(move); + + } + + public boolean isMate() { + return board.isMated(); + } + + public boolean isDraw() { + return board.isDraw(); + } + + public int getActivePlayer() { + if (board.getSideToMove() == Side.WHITE) { + return 1; + } + return 2; + } public boolean isMate() { return board.isMated(); @@ -154,15 +181,15 @@ public class Game { return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).collect(Collectors.toList()); } - + public void stopClock() { - 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)); - } + 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. @@ -186,41 +213,40 @@ 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); + 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); + playMove(promotionMove); } public String toFEN() { @@ -264,4 +290,12 @@ public class Game { // TODO Auto-generated method stub return this.board; } + public String toFEN() { + board.toString(); + return board.getFen(); + } + + public Square getSelectedSquare() { + return this.getSelectedSquare(); + } }