Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HasCastled in BoardAPI #449

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Chess-Challenge/src/API/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public bool IsDraw()
/// </summary>
public bool IsInsufficientMaterial() => Arbiter.InsufficentMaterial(board);

/// <summary>
/// Does the given player has castled?
/// (this will only detect castled made during the game moves)
/// </summary>
public bool HasCastled(bool white) => white ? board.currentGameState.hasWhiteCastled : board.currentGameState.hasBlackCastled;

/// <summary>
/// Does the given player still have the right to castle kingside?
/// Note that having the right to castle doesn't necessarily mean castling is legal right now
Expand Down
11 changes: 7 additions & 4 deletions Chess-Challenge/src/Framework/Chess/Board/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public void MakeMove(Move move, bool inSearch = true)
int newCastlingRights = currentGameState.castlingRights;
int newEnPassantFile = 0;

bool hasWhiteCastled = currentGameState.hasWhiteCastled || IsWhiteToMove && move.MoveFlag == Move.CastleFlag;
bool hasBlackCastled = currentGameState.hasBlackCastled || !IsWhiteToMove && move.MoveFlag == Move.CastleFlag;

// Update bitboard of moved piece (pawn promotion is a special case and is corrected later)
MovePiece(movedPiece, startSquare, targetSquare);

Expand Down Expand Up @@ -275,7 +278,7 @@ public void MakeMove(Move move, bool inSearch = true)
newFiftyMoveCounter = 0;
}

GameState newState = new(capturedPieceType, newEnPassantFile, newCastlingRights, newFiftyMoveCounter, newZobristKey);
GameState newState = new(capturedPieceType, newEnPassantFile, newCastlingRights, hasWhiteCastled, hasBlackCastled, newFiftyMoveCounter, newZobristKey);
gameStateHistory.Push(newState);
currentGameState = newState;
hasCachedInCheckValue = false;
Expand Down Expand Up @@ -400,7 +403,7 @@ public void MakeNullMove()
newZobristKey ^= Zobrist.sideToMove;
newZobristKey ^= Zobrist.enPassantFile[currentGameState.enPassantFile];

GameState newState = new(PieceHelper.None, 0, currentGameState.castlingRights, currentGameState.fiftyMoveCounter + 1, newZobristKey);
GameState newState = new (PieceHelper.None, 0, currentGameState.castlingRights, currentGameState.hasWhiteCastled, currentGameState.hasBlackCastled, currentGameState.fiftyMoveCounter + 1, newZobristKey);
currentGameState = newState;
gameStateHistory.Push(currentGameState);
UpdateSliderBitboards();
Expand Down Expand Up @@ -519,9 +522,9 @@ public void LoadPosition(FenUtility.PositionInfo posInfo)
plyCount = (posInfo.moveCount - 1) * 2 + (IsWhiteToMove ? 0 : 1);

// Set game state (note: calculating zobrist key relies on current game state)
currentGameState = new GameState(PieceHelper.None, posInfo.epFile, castlingRights, posInfo.fiftyMovePlyCount, 0);
currentGameState = new GameState(PieceHelper.None, posInfo.epFile, castlingRights, false, false, posInfo.fiftyMovePlyCount, 0);
ulong zobristKey = Zobrist.CalculateZobristKey(this);
currentGameState = new GameState(PieceHelper.None, posInfo.epFile, castlingRights, posInfo.fiftyMovePlyCount, zobristKey);
currentGameState = new GameState(PieceHelper.None, posInfo.epFile, castlingRights, false, false, posInfo.fiftyMovePlyCount, zobristKey);

RepetitionPositionHistory.Push(zobristKey);

Expand Down
6 changes: 5 additions & 1 deletion Chess-Challenge/src/Framework/Chess/Board/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public readonly struct GameState
public readonly int capturedPieceType;
public readonly int enPassantFile;
public readonly int castlingRights;
public readonly bool hasWhiteCastled;
public readonly bool hasBlackCastled;
public readonly int fiftyMoveCounter;
public readonly ulong zobristKey;

Expand All @@ -13,11 +15,13 @@ public readonly struct GameState
public const int ClearBlackKingsideMask = 0b1011;
public const int ClearBlackQueensideMask = 0b0111;

public GameState(int capturedPieceType, int enPassantFile, int castlingRights, int fiftyMoveCounter, ulong zobristKey)
public GameState(int capturedPieceType, int enPassantFile, int castlingRights, bool hasWhiteCastled, bool hasBlackCastled, int fiftyMoveCounter, ulong zobristKey)
{
this.capturedPieceType = capturedPieceType;
this.enPassantFile = enPassantFile;
this.castlingRights = castlingRights;
this.hasWhiteCastled = hasWhiteCastled;
this.hasBlackCastled = hasBlackCastled;
this.fiftyMoveCounter = fiftyMoveCounter;
this.zobristKey = zobristKey;
}
Expand Down