kida fixed promotion
parent
d5cfa77e09
commit
cbad409db7
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue