better undo

This commit is contained in:
2025-04-04 20:06:32 +02:00
parent 48a215eae5
commit 810934aea1
8 changed files with 48 additions and 56 deletions

View File

@@ -30,23 +30,6 @@ public class Game {
playerTurn = Color.getEnemy(playerTurn);
}
public boolean checkPromotion(Coordinate pieceCoords) {
Piece piece = getBoard().pieceAt(pieceCoords);
if (piece == null || !(piece instanceof Pawn))
return false;
int destY = pieceCoords.getY();
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
if (destY != enemyLine)
return false;
return true;
}
public GameStatus checkGameStatus() {
final Color enemy = Color.getEnemy(getPlayerTurn());
@@ -63,20 +46,38 @@ public class Game {
}
public boolean pawnShouldBePromoted() {
if (pawnShouldBePromoted(Color.White))
return true;
return pawnShouldBePromoted(Color.Black);
return pawnPromotePosition() != null;
}
private boolean pawnShouldBePromoted(Color color) {
/**
*
* @return Null if there is no pawn to promote
*/
public Coordinate pawnPromotePosition() {
Coordinate piecePos = pawnPromotePosition(Color.White);
if (piecePos != null)
return piecePos;
return pawnPromotePosition(Color.Black);
}
/**
*
* @return Null if there is no pawn to promote
*/
private Coordinate pawnPromotePosition(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;
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
Piece piece = getBoard().pieceAt(pieceCoords);
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
continue;
return pieceCoords;
}
return false;
return null;
}
}