SebLague / Chess-Challenge

Create your own tiny chess bot!
https://www.youtube.com/watch?v=Ne40a5LkK6A
MIT License
1.78k stars 1.07k forks source link

Copying boards/overriding fen strings #476

Closed pressgoal closed 10 months ago

pressgoal commented 10 months ago

For how I want my AI to work, it requires me to save different board layouts. What I'm wondering is how I create a new board with the same values as a different, or in other words, how do I copy one board to another. I considered just setting the new board's fen string equal to the old board's fen string, but there doesn't seem to be a way to manually set the fen string. Any suggestions would be greatly appreciated.

Edit: I found that the function for creating a new board does take in a source board, but it doesn't work if I put in the board that I want it to copy. The error reads "Argument 1: cannot convert from ChessChallenge.API.Board to ChessChallenge.Chess.Board.

ryanheath commented 10 months ago

There is this function CreateBoardFromFEN, but the comment says it all, you should try to use an algorithm that uses the board states through MakeMove and UndoMove calls repeatedly.

/// <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);
}
pressgoal commented 10 months ago

Ok, thank you. I'll try that, but if it does turn out to be too slow then I'll try to adjust my code to use Make/UndoMove.