C++ Chess engine creation issues
Moderator: Ras
-
- Posts: 2
- Joined: Thu Nov 23, 2023 5:01 am
- Full name: Dalton Platt
C++ Chess engine creation issues
I am creating a C++ chess engine, and I need to implement Kibitz and Perft and get it to work with Arena Chess GUI. The chess engine that I programmed has already generated moves based on the FEN of the board. I am unsure that is how Arena sends the position to my engine. Some of the UCI standards is already implemented, but when a move is made the engine tries to move that side again and it ends in an illegal move. I am not sure how to properly code my program to work with UCI completely. The idea is that the engine should be able to play against itself, but I am currently stuck. Any help on this would be great. Here is a Github link for my files. https://github.com/DaltonPlatt/LegalMoveGen
-
- Posts: 29
- Joined: Thu Jun 09, 2022 5:09 am
- Full name: Clayton Ramsey
Re: C++ Chess engine creation issues
From a quick scan of your code, I think I know the issue. From the specification:
Your code currently ignores the data after `moves` and just sets the current board based on the FEN. This is incorrect - you must set up the board based on the FEN and then play out all the moves listed after `moves`. In practice, I've found that you can't rely on a GUI sending `moves` as part of the position command, though - some GUIs will omit the `moves` token if they have no moves to provide.
You might also want to consider: the UCI standard allows arbitrary whitespace between tokens, so the current hardcoding based on string positions might cause bugs in the future.
In this specification, every `position` command is followed by a specification of the start position (a FEN or the normal start position) and then the sequence of moves required to reach the current position of the game. For example, the following would be a valid command:position [ fen <fenstring> | startpos ] moves <move1> ... <movei>
set up the position described in fenstring on the internal board and play the moves on the internal chess board.
if the game was played from the start position the string startpos will be sent
Note: no "new" command is needed. However, if this position is from a different game than the last position sent to the engine, the GUI should have sent a ucinewgame inbetween.
Code: Select all
position startpos moves e2e4
You might also want to consider: the UCI standard allows arbitrary whitespace between tokens, so the current hardcoding based on string positions might cause bugs in the future.
-
- Posts: 2
- Joined: Thu Nov 23, 2023 5:01 am
- Full name: Dalton Platt
Re: C++ Chess engine creation issues
How would I go about programming that? Someone I was talking to said to implement a make_move function to make the move and update the internal FEN based on the random move chosen. There are a couple of functions already implemented like to_fen and from_fen. Not sure how to start coding make_move or getting it to work.
-
- Posts: 290
- Joined: Mon Mar 13, 2006 5:23 pm
- Location: Québec
- Full name: Mathieu Pagé
Re: C++ Chess engine creation issues
You have a Position class that represents a chess game position. What you need is add a method "void make_move(Move move)" that accepts a move as input and modify the current position so that it becomes the position that resulted from the move. If the move was "e2e4", make_move() needs to remove the pawn from e2 and put it on e4. It also need to somehow mark the e4 pawn as a candidate for en passant.platt12 wrote: ↑Tue Nov 28, 2023 3:16 am How would I go about programming that? Someone I was talking to said to implement a make_move function to make the move and update the internal FEN based on the random move chosen. There are a couple of functions already implemented like to_fen and from_fen. Not sure how to start coding make_move or getting it to work.
Once you have this method you will be able to handle the position command by first parsing the FEN string and setting the position accordingly and then calling my_position.MakeMove(move) for each moves provided by the position command.
For performance purposes you will later also need an "void unmake_move(move)" method. Getting the make_move, unmake_move method and the (pseudo legal) move generator right is not a trivial task. There is a lot of corner cases to think about. Making it efficient is even more complex, but really rewarding.
Mathieu Pagé
mathieu@mathieupage.com
mathieu@mathieupage.com
-
- Posts: 1476
- Joined: Mon Jan 28, 2013 2:51 pm
Re: C++ Chess engine creation issues
Thank you very much for helping my student implement a UCI engine that plays against itself or against a human player by making random legal moves! I allowed him to ask for help here. The engine can also play with white pieces against another engine. If the other engine makes the first move as white, the student engine fails to respond as black, due to some bug.
-
- Posts: 28348
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: C++ Chess engine creation issues
Ummmm... If the student has no idea how to implement a MakeMove() routine I would be started to worry how much he understood of the entire project.