84 lines
1.6 KiB
Java
84 lines
1.6 KiB
Java
package chess.model;
|
|
|
|
import chess.model.pieces.Pawn;
|
|
|
|
public class Game {
|
|
private final ChessBoard board;
|
|
private Color playerTurn;
|
|
|
|
public enum GameStatus {
|
|
Check, CheckMate, OnGoing, Pat;
|
|
}
|
|
|
|
public Game(ChessBoard board) {
|
|
this.board = board;
|
|
}
|
|
|
|
public ChessBoard getBoard() {
|
|
return board;
|
|
}
|
|
|
|
public Color getPlayerTurn() {
|
|
return playerTurn;
|
|
}
|
|
|
|
public void resetPlayerTurn() {
|
|
this.playerTurn = Color.White;
|
|
}
|
|
|
|
public void switchPlayerTurn() {
|
|
playerTurn = Color.getEnemy(playerTurn);
|
|
}
|
|
|
|
public GameStatus checkGameStatus() {
|
|
final Color enemy = Color.getEnemy(getPlayerTurn());
|
|
|
|
if (this.board.isKingInCheck(enemy))
|
|
if (this.board.hasAllowedMoves(enemy))
|
|
return GameStatus.Check;
|
|
else
|
|
return GameStatus.CheckMate;
|
|
|
|
if (!board.hasAllowedMoves(enemy))
|
|
return GameStatus.Pat;
|
|
|
|
return GameStatus.OnGoing;
|
|
}
|
|
|
|
public boolean pawnShouldBePromoted() {
|
|
return pawnPromotePosition() != null;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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++) {
|
|
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
|
Piece piece = getBoard().pieceAt(pieceCoords);
|
|
|
|
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
|
continue;
|
|
|
|
return pieceCoords;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
}
|