Mini-update 3
For this update, I wanted to ensure that the game is end-to-end playable alongside having the ability to play against some level of competent AI. One of the things that I did right with the logic implementation of the game is making the piece moves modular in terms of it just being the row and column that the player selects and the entire logic when it comes to whether a piece has been selected and whether the next place is someplace that it can move to gets handled automatically in the backend.
[Snippet of code used to handle the logic]
This made it so that implementing the next part of AI logic was simply having the AI player figure out what the next move would be as a result and giving the row and column as an output. This was a pretty cool opportunity to try and experiment with different AI backends to see what would fit this situation the best. I wanted to first try out Reinforcement Learning. On that note, I found the open_spiel repository of open-source game implementation alongside RL algorithms generalized to be applicable across all games created within the formatting of the repository. I quickly found out, however, that an implementation in python would cause a large bottleneck in performance and basically be unusable within the chess context since the already-existing normal chess implementation was within their c++ side for that reason. Furthermore, the actual game is only for one version of chess requiring each different version of chess to have its own open_spiel c++ implementation. This led me down the development of a python generator that would be able to create these open_spiel versions in the backend from the frontend that I'd already made since the main thing was defining the action space and I could do that within the simpler general version of chess that I was targeting.
However, with that being a larger endeavor in general, I wanted to first make some sort of barebones implementation that could show this technology working and allow humans to actually play against something other then themself since that makes the gameloop super boring and that's when I decided to hop on to the emerging LLM trend.
Using OpenAI's GPT3.5turbo in the backend, I figured it wouldn't hurt to try prompt engineering to find a solution and that started the development of the chatgpt general chess ai.
The first thing to do in this scenario was first do the higher-level logic implementation of what an AI move would look like and that boiled down to the following code
With the overhead logic figured out, the next important thing was figuring out how I was going to store the history of the game, and for that, I decided that the important information is which piece gets chosen and which square it moves from, and where it moves to alongside who wins and to denote that in a concise manner to minimize token usage from the LLM (cost saving + efficiency), I came up with the following standard
The reason I used the hashed values and not the names was to stay consistent with the naming convention that I'd used within the npz storage for each board.
Finally, one important thing was giving the AI some training data and this was accomplished by simply appending the game history into a separate text file for each type of Board's history where with each game, it would add on to the history thus giving more training data over time. Having all the different parts for the prompt generation figured out, I was able to create the following prompt helper function
that encompassed all the information that I'd need for the AI.
As a result of all of this, I now have an AI within the game that you can play against which can be turned on within the new game scene where you can switch to AI mode as any color and the response to your move will be done by the AI. This can be seen in the demo below
This was really cool to see since the moves being done were no longer just fully random and it was getting better over time. One thing to note is that with bigger boards, the combinatoric expansion of the number of possible moves basically makes the token limit explode and so this is only going to work within smaller board sizes.
Get Generalized Chess
Generalized Chess
AI for any version of chess
Status | In development |
Author | Ultraman287 |
Genre | Puzzle |
More posts
- Mini-update 2Oct 01, 2023
- Mini-update 1Oct 01, 2023
Leave a comment
Log in with itch.io to leave a comment.