Fully onchain Tic-Tac-Toe
Let's build Trustless Tic Tac Toe, a decentralized, unstoppable, and open game.
create a new match.
seek and match with another player based on Elo ranking.
play Tic Tac Toe.
update Elo ranking to use for better matching in the future matches.
You can treat this Tic Tac Toe tutorial as a template for implementing a PvP turn based game so you can build your own future games.
Write the Tic Tac Toe smart contracts
It turns out that writing the Tic Tac Toe smart contract is pretty simple.
First, we need to extend TurnBasedGame
contract that implemented the following standard functions that will need for building PvP turn based games:
turnDuration
: the duration of each turnturnTimePivot
: the starting time of the current turnplayer1TimePool
,player2TimePool
: the total game time (countdown) for the two playersfindMatch
: the game only allows a maximum of one waiting match. If there is one available then use joinMatch, if not then use createMatchcreateMatch
: create a new match, may need to override to add game setup logic, player statejoinMatch
: join an existing matchcancelMatch
: cancel the match, can only be called after createMatch but before any player joinsaffirmTimeOut
: Normally, when a timeout occurs, the losing client will call resign to trigger theendGame
function. Suppose the losing client is disconnected, the winning client can proactively callaffirmTimeOut
to end the game.resign
,offerDraw
,cancelDraw
,rejectDraw
,acceptDraw
: functions as their names suggest, no specific notesgetTurn
: returns (bool, int256, int256) which includes:the current player's turn (true for player1, false for player2)
the remaining time for the current turn (can be negative when the match times out)
the total remaining time for the current player's turn
The initial ELO of a new player is set to 1500 by default in the DEFAULT_ELO variable.
Of course, you can also override these functions to your custom logic if needed. The complete TurnBasedGame smart contract code can be found at:
Next, we need to implement an additional move function for players to submit their moves. This function should include game logic, check for timeouts, and update the turn in MatchData
. If the game reaches a win/loss result, the endGame
function should be called. When you build your own game, you will need to implement this function for your game logic.
The complete TicTacToe smart contract code can be found at:
Deploy the contracts
We've published the game’s repository to Github for you to get started.
To compile the contracts, use the built-in hardhat compile task.
Finally, to deploy the contracts, review config file hardhat.config.ts. The network configs should look like this.
Run the deploy scripts using hardhat-deploy.
Build an UI to interact with the Tic Tac Toe contracts
There are two things the game UI needs to support so players can interact with Tic Tac Toe contracts above via the UI:
Creating an internal NOS wallet. This is an EVM-compatible wallet, players need to buy from topup page and send TC token to the wallet so that they can create transactions to the Tic Tac Toe contract and pay NOS network gas fee.
Creating transactions for creating a new game, making a move to the Tic Tac Toe contract via web3 libraries and then displaying the game state on the UI.
The complete Tic Tac Toe game UI code can be found at:
Build the source code:
and view it on browser at http://localhost:6010
. The game UI should look like this:
We have deployed the full Tic Tac Toe source code on NOS mainnet, you can experience the final result at: https://newbitcoincity.com/tic-tac-toe
Happy building!
Last updated