promote gui

This commit is contained in:
2025-04-03 22:06:34 +02:00
parent 927ba129f6
commit 0d72e015f1
5 changed files with 91 additions and 15 deletions

View File

@@ -16,7 +16,6 @@ public class Game {
public final Signal0 OnMat = new Signal0();
public final Signal0 OnPat = new Signal0();
public Game(ChessBoard board) {
this.board = board;
}
@@ -39,23 +38,30 @@ public class Game {
this.OnPlayerTurn.emit(playerTurn);
}
public void checkPromotion(Coordinate pieceCoords) {
public boolean checkPromotion(Coordinate pieceCoords) {
Piece piece = getBoard().pieceAt(pieceCoords);
if (piece == null || !(piece instanceof Pawn))
return;
return false;
int destY = pieceCoords.getY();
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
if (destY == enemyLine) {
OnPromote.emit(pieceCoords);
}
if (destY != enemyLine)
return false;
OnPromote.emit(pieceCoords);
return true;
}
public void checkGameStatus() {
/**
*
* @return true if game should end
*/
public boolean checkGameStatus() {
final ChessBoard board = getBoard();
final Color enemy = Color.getEnemy(getPlayerTurn());
@@ -66,10 +72,30 @@ public class Game {
} else {
OnMat.emit();
OnWin.emit(getPlayerTurn());
return true;
}
} else if (!board.hasAllowedMoves(enemy)) {
OnPat.emit();
return true;
}
return false;
}
public boolean pawnShouldBePromoted() {
if (pawnShouldBePromoted(Color.White))
return true;
return pawnShouldBePromoted(Color.Black);
}
private boolean pawnShouldBePromoted(Color color) {
int enemyLineY = color == Color.White ? 0 : 7;
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
if(checkPromotion(new Coordinate(x, enemyLineY)))
return true;
}
return false;
}
}