refactor game
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.pieces.Pawn;
|
||||
import common.Signal0;
|
||||
import common.Signal1;
|
||||
|
||||
public class Game {
|
||||
private final ChessBoard board;
|
||||
private Color playerTurn;
|
||||
|
||||
public final Signal1<Coordinate> OnPromote = new Signal1<>();
|
||||
public final Signal1<Color> OnWin = new Signal1<>();
|
||||
public final Signal1<Color> OnPlayerTurn = new Signal1<>();
|
||||
|
||||
public final Signal0 OnCheck = new Signal0();
|
||||
public final Signal0 OnMat = new Signal0();
|
||||
public final Signal0 OnPat = new Signal0();
|
||||
|
||||
|
||||
public Game(ChessBoard board) {
|
||||
this.board = board;
|
||||
}
|
||||
@@ -22,6 +35,40 @@ public class Game {
|
||||
|
||||
public void switchPlayerTurn() {
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
this.OnPlayerTurn.emit(playerTurn);
|
||||
}
|
||||
|
||||
public void checkPromotion(Coordinate pieceCoords) {
|
||||
|
||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
||||
|
||||
if (piece == null || !(piece instanceof Pawn))
|
||||
return;
|
||||
|
||||
int destY = pieceCoords.getY();
|
||||
|
||||
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
|
||||
|
||||
if (destY == enemyLine) {
|
||||
OnPromote.emit(pieceCoords);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkGameStatus() {
|
||||
final ChessBoard board = getBoard();
|
||||
|
||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||
|
||||
if (board.isKingInCheck(enemy)) {
|
||||
if (board.hasAllowedMoves(enemy)) {
|
||||
OnCheck.emit();
|
||||
} else {
|
||||
OnMat.emit();
|
||||
OnWin.emit(getPlayerTurn());
|
||||
}
|
||||
} else if (!board.hasAllowedMoves(enemy)) {
|
||||
OnPat.emit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user