V1.1 fix GetPiece function and add CreateBoardFromFen

This commit is contained in:
Sebastian Lague 2023-07-21 21:08:41 +02:00
parent 951ac24ae9
commit 24fa340cc9
4 changed files with 33 additions and 25 deletions

View file

@ -13,7 +13,6 @@ namespace ChessChallenge.API
readonly HashSet<ulong> repetitionHistory; readonly HashSet<ulong> repetitionHistory;
readonly PieceList[] allPieceLists; readonly PieceList[] allPieceLists;
readonly PieceList[] validPieceLists; readonly PieceList[] validPieceLists;
readonly Piece[] pieces;
Move[] cachedLegalMoves; Move[] cachedLegalMoves;
bool hasCachedMoves; bool hasCachedMoves;
@ -47,14 +46,6 @@ namespace ChessChallenge.API
// Init rep history // Init rep history
repetitionHistory = new HashSet<ulong>(board.RepetitionPositionHistory); repetitionHistory = new HashSet<ulong>(board.RepetitionPositionHistory);
repetitionHistory.Remove(board.ZobristKey); repetitionHistory.Remove(board.ZobristKey);
// Create piece array
pieces = new Piece[64];
for (int i = 0; i < 64; i++)
{
int p = board.Square[i];
pieces[i] = new Piece((PieceType)PieceHelper.PieceType(p), PieceHelper.IsWhite(p), new Square(i));
}
} }
/// <summary> /// <summary>
@ -191,18 +182,20 @@ namespace ChessChallenge.API
return new Square(board.KingSquare[colIndex]); return new Square(board.KingSquare[colIndex]);
} }
/// <summary> /// <summary>
/// Gets the piece on the given square. If the square is empty, the piece will have a PieceType of None. /// Gets the piece on the given square. If the square is empty, the piece will have a PieceType of None.
/// </summary> /// </summary>
public Piece GetPiece(Square square) public Piece GetPiece(Square square)
{ {
return pieces[square.Index]; int p = board.Square[square.Index];
} bool white = PieceHelper.IsWhite(p);
return new Piece((PieceType)PieceHelper.PieceType(p), white, square);
}
/// <summary> /// <summary>
/// Gets a list of pieces of the given type and colour /// Gets a list of pieces of the given type and colour
/// </summary> /// </summary>
public PieceList GetPieceList(PieceType pieceType, bool white) public PieceList GetPieceList(PieceType pieceType, bool white)
{ {
return allPieceLists[PieceHelper.MakePiece((int)pieceType, white)]; return allPieceLists[PieceHelper.MakePiece((int)pieceType, white)];
} }
@ -271,5 +264,16 @@ namespace ChessChallenge.API
/// </summary> /// </summary>
public ulong ZobristKey => board.ZobristKey; public ulong ZobristKey => board.ZobristKey;
} /// <summary>
/// Creates a board from the given fen string. Please note that this is quite slow, and so it is advised
/// to use the board given in the Think function, and update it using MakeMove and UndoMove instead.
/// </summary>
public static Board CreateBoardFromFEN(string fen)
{
Chess.Board boardCore = new Chess.Board();
boardCore.LoadPosition(fen);
return new Board(boardCore);
}
}
} }

View file

@ -55,6 +55,7 @@ namespace ChessChallenge.Application
public ChallengeController() public ChallengeController()
{ {
Log($"Launching Chess-Challenge version {Settings.Version}");
tokenCount = GetTokenCount(); tokenCount = GetTokenCount();
Warmer.Warm(); Warmer.Warm();
@ -228,8 +229,7 @@ namespace ChessChallenge.Application
{ {
moveToPlay = chosenMove; moveToPlay = chosenMove;
isWaitingToPlayMove = true; isWaitingToPlayMove = true;
const float minDelay = 0.1f; playMoveTime = lastMoveMadeTime + MinMoveDelay;
playMoveTime = lastMoveMadeTime + minDelay;
} }
else else
{ {

View file

@ -4,10 +4,14 @@ namespace ChessChallenge.Application
{ {
public static class Settings public static class Settings
{ {
public const string Version = "1.1";
public const int GameDurationMilliseconds = 60 * 1000; public const int GameDurationMilliseconds = 60 * 1000;
public const int MaxTokenCount = 1024; public const float MinMoveDelay = 0;
public static readonly bool RunBotsOnSeparateThread = true; public static readonly bool RunBotsOnSeparateThread = true;
public const int MaxTokenCount = 1024;
public const LogType MessagesToLog = LogType.All; public const LogType MessagesToLog = LogType.All;
public static readonly Vector2 ScreenSizeSmall = new(1280, 720); public static readonly Vector2 ScreenSizeSmall = new(1280, 720);

View file

@ -7,7 +7,7 @@ namespace ChessChallenge.Application
public static class TokenCounter public static class TokenCounter
{ {
static HashSet<SyntaxKind> tokensToIgnore = new(new SyntaxKind[] static readonly HashSet<SyntaxKind> tokensToIgnore = new(new SyntaxKind[]
{ {
SyntaxKind.PrivateKeyword, SyntaxKind.PrivateKeyword,
SyntaxKind.PublicKeyword, SyntaxKind.PublicKeyword,