Tic-tac-toe on Bitcoin SV
Bitcoin: A Peer-to-Peer Electronic Gaming System
A purely peer-to-peer version of games would allow online players to play with one another without going through a gaming company. Digital signatures provide part of the solution, but the main benefits are lost if a trusted third party is still required to prevent cheating. We propose a solution to the cheating problem using the Bitcoin network. Specifically, we put a game fully on chain: not only the data, but also the logic. We demonstrate the idea by implementing the first ever p2p version of tic-tac-toe.
To initiate a game, Alice and Bob each locks up X amount of bitcoins into a contract UTXO. They take turns to play the game by sending signed transactions interacting with the stateful contract. If one of them wins, the winner claims the 2X bitcoins. If there is a draw, they take back their bitcoins. The core contract code is listed below with self-explanatory comments inline.
We only show the most essential part of the game contract, for ease of exposition. They are several ways to polish it into a fully practical game, including:
- Timeout: add a clause (i.e., function) to handle when a player refuses to make his move (maybe because he finds out he will lose). For example, set nLockTime to 1 hour from now when Alice plays. If Bob does not move in time, Alice wins and can take all bitcoins. Vice versa.
- Transaction fee: add another UTXO input and change output to cover transaction fee.
We only showed how to implement p2p games using the easiest tic-tac-toe example. The underlying idea can be trivially generalized to arbitrary games (chess, Go, poker, …) to make them fully on chain and thus peer-to-peer. It can also be extended to multiple players. Let us start playing p2p games on Bitcoin!