remove game signals

This commit is contained in:
2025-04-04 19:42:23 +02:00
parent 55ef180f57
commit 48a215eae5
4 changed files with 68 additions and 77 deletions

View File

@@ -2,7 +2,6 @@ package chess.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import chess.model.visitor.KingIdentifier;
import chess.model.visitor.PiecePathChecker;
@@ -26,8 +25,8 @@ public class ChessBoard {
}
private final Cell[][] cells;
private final Stack<Move> moves;
private final Stack<Piece> ejectedPieces;
private Move lastMove;
private Piece lastEjectedPiece;
public ChessBoard() {
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
@@ -36,43 +35,41 @@ public class ChessBoard {
this.cells[i][j] = new Cell();
}
}
this.moves = new Stack<>();
this.ejectedPieces = new Stack<>();
this.lastMove = null;
this.lastEjectedPiece = null;
}
public void applyMove(Move move) {
assert (move.isValid());
assert move.isValid() : "Invalid move !";
Piece deadPiece = pieceAt(move.getFinish());
if (deadPiece != null) {
deadPiece.eject(this.moves.size());
this.ejectedPieces.add(deadPiece);
this.lastEjectedPiece = deadPiece;
} else {
this.lastEjectedPiece = null;
}
Piece movingPiece = pieceAt(move.getStart());
pieceComes(movingPiece, move.getFinish());
pieceLeaves(move.getStart());
movingPiece.move();
this.moves.add(move);
this.lastMove = move;
}
public void undoLastMove() {
assert !this.moves.empty() : "Can't undo at the beginning!";
assert this.lastMove != null: "Can't undo at the beginning!";
Move move = this.moves.pop();
Move move = this.lastMove;
Piece movingPiece = pieceAt(move.getFinish());
pieceComes(movingPiece, move.getStart());
pieceLeaves(move.getFinish());
movingPiece.unMove();
if (this.ejectedPieces.empty())
if (this.lastEjectedPiece == null)
return;
Piece piece = this.ejectedPieces.lastElement();
if (piece.getEjectedMoveNumber() != this.moves.size())
return;
Piece piece = this.lastEjectedPiece;
pieceComes(piece, move.getFinish());
piece.eject(-1);
}
public boolean isCellEmpty(Coordinate coordinate) {

View File

@@ -1,20 +1,14 @@
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 enum GameStatus {
Check, CheckMate, OnGoing, Pat;
}
public Game(ChessBoard board) {
this.board = board;
@@ -30,12 +24,10 @@ public class Game {
public void resetPlayerTurn() {
this.playerTurn = Color.White;
this.OnPlayerTurn.emit(playerTurn);
}
public void switchPlayerTurn() {
playerTurn = Color.getEnemy(playerTurn);
this.OnPlayerTurn.emit(playerTurn);
}
public boolean checkPromotion(Coordinate pieceCoords) {
@@ -52,31 +44,22 @@ public class Game {
if (destY != enemyLine)
return false;
this.OnPromote.emit(pieceCoords);
return true;
}
/**
*
* @return true if game should end
*/
public boolean checkGameStatus() {
public GameStatus checkGameStatus() {
final Color enemy = Color.getEnemy(getPlayerTurn());
if (this.board.isKingInCheck(enemy)) {
if (this.board.hasAllowedMoves(enemy)) {
this.OnCheck.emit();
} else {
this.OnMat.emit();
this.OnWin.emit(getPlayerTurn());
return true;
}
} else if (!board.hasAllowedMoves(enemy)) {
this.OnPat.emit();
return true;
}
if (this.board.isKingInCheck(enemy))
if (this.board.hasAllowedMoves(enemy))
return GameStatus.Check;
else
return GameStatus.CheckMate;
return false;
if (!board.hasAllowedMoves(enemy))
return GameStatus.Pat;
return GameStatus.OnGoing;
}
public boolean pawnShouldBePromoted() {