-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.cs
65 lines (57 loc) · 2.35 KB
/
GameState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace SurpriseChess;
// Manages the the current player, castling & en passant rights
public class GameState
{
public PieceColor CurrentPlayerColor { get; private set; }
public Position? EnPassantPosition { get; private set; }
public Dictionary<PieceColor, Dictionary<CastleDirection, bool>> CanCastle { get; private set; }
private readonly Board board;
public GameState(Board board)
{
CurrentPlayerColor = PieceColor.White;
CanCastle = ChessUtils.InitialCastlingRights;
this.board = board;
}
public void UpdateStateAfterMove(Position source, Position destination)
{
Piece? pieceAtSource = board.GetPieceAt(source);
if (pieceAtSource == null) throw new InvalidOperationException("No piece at source position");
UpdateEnPassantRights(pieceAtSource, source, destination);
UpdateCastlingRights(pieceAtSource, source);
SwitchPlayer();
}
private void UpdateCastlingRights(Piece piece, Position source)
{
if (piece.Type == PieceType.King) // King moved, disable castling for player
{
CanCastle[piece.Color][CastleDirection.KingSide] = false;
CanCastle[piece.Color][CastleDirection.QueenSide] = false;
}
else if (piece.Type == PieceType.Rook) // Rook moved, disable castling for that side
{
if (source == board.RookStartingPositions[piece.Color][CastleDirection.KingSide])
{
CanCastle[piece.Color][CastleDirection.KingSide] = false;
}
else if (source == board.RookStartingPositions[piece.Color][CastleDirection.QueenSide])
{
CanCastle[piece.Color][CastleDirection.QueenSide] = false;
}
}
}
private void UpdateEnPassantRights(Piece piece, Position source, Position destination)
{
bool isPawnDoubleMove = (
piece.Type == PieceType.Pawn
&& Math.Abs(source.Row - destination.Row) == 2
);
// En passant becomes available if a pawn moves two squares forward
EnPassantPosition = isPawnDoubleMove
? new Position((source.Row + destination.Row) / 2, source.Col)
: null;
}
private void SwitchPlayer()
{
CurrentPlayerColor = CurrentPlayerColor == PieceColor.White ? PieceColor.Black : PieceColor.White;
}
}