Onchain AI Composability - AI Powered Gaming With Chess
In this page, we will demonstrate how to build a simple AI-powered chess game utilizing the LLAMA model deployed on the Base blockchain.
Smart Contract Design Overview
The contract enables text-based chess games where users play as White against the contract's AI playing as Black. The game follows standard chess rules and uses a request-response mechanism with EternalAI's decentralized AI inference.
Key Contract Features:
Game Initialization: Creates a game session for the user.
Game Moves: Processes user moves and responds with AI-generated moves.
Game State Management: Keeps track of game states and contexts.
Key Functions
Creating a Game
This function initializes a new game for the player, creating a game session and requesting AI inference based on the initial game prompt.
Note: replace <PRIVATE_KEY> with your actual wallet private key. Ensure that the wallet has sufficient ETH on the Base network to cover transaction fees and amounts.
Playing Chess
After deploying the contract to Base network, you can start playing chess with the AI-powered chess contract by running the following command which will create transactions into the contract.
Note: replace <PRIVATE_KEY> with your actual wallet private key. Ensure that the wallet has sufficient ETH on the Base network to cover transaction fees and amounts.
Below are transactions on Base we've created when playing chess with the chess contract.
Create a new game by a player with address 0x62998e172240F4CC26EC10717d16a8D4442bf2Dd
The 1st move
The 2st move
At every step you can view the updated chess board in your terminal.
Let's play chess with Dagent...
Player address: 0x62998e172240F4CC26EC10717d16a8D4442bf2Dd
------------------------
Create game...
Tx hash: 0xe2ef210993488793c2249d4210869d3b90d634e37fc675d69787ab840e6e305e
Wait for Dagent to construct a game...
Dagent's created the game. Let's play...
a b c d e f g h
8 R N B Q K B N R
7 P P P P P P P P
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 p p p p p p p p
1 r n b q k b n r
------------------------
Your move: b2-b4
Tx hash: 0x1e5354387f36ccf2fbae1f0e71c082e0e71c6c2bd919042c2fd37aab54bf601c
a b c d e f g h
8 R N B Q K B N R
7 P P P P P P P P
6 . . . . . . . .
5 . . . . . . . .
4 . p . . . . . .
3 . . . . . . . .
2 p . p p p p p p
1 r n b q k b n r
Wait for Dagent to make a move...
Dagent made c7-c5 move
a b c d e f g h
8 R N B Q K B N R
7 P P . P P P P P
6 . . . . . . . .
5 . . P . . . . .
4 . p . . . . . .
3 . . . . . . . .
2 p . p p p p p p
1 r n b q k b n r
------------------------
Your move: Nb1-c3
Tx hash: 0xf43cee1a5527589e950b31c372366d32e6e0992398d677bfa7c62326ea748f7f
a b c d e f g h
8 R N B Q K B N R
7 P P . P P P P P
6 . . . . . . . .
5 . . P . . . . .
4 . p . . . . . .
3 . . n . . . . .
2 p . p p p p p p
1 r . b q k b n r
Wait for Dagent to make a move...
Dagent made g7-g5 move
a b c d e f g h
8 R N B Q K B N R
7 P P . P P P . P
6 . . . . . . . .
5 . . P . . . P .
4 . p . . . . . .
3 . . n . . . . .
2 p . p p p p p p
1 r . b q k b n r