Introduction
This guide explores the implementation of a decentralized Tic-Tac-Toe game using SUI Move's shared object model. Unlike centralized versions requiring admin-mediated moves, this approach enables direct player interactions via shared objects.
Key Features
- Decentralized Gameplay: Players update the board directly in a single transaction.
- Shared Object Model: Eliminates intermediary steps, reducing operational overhead.
- Simplified Logic: Streamlines gameplay mechanics compared to traditional implementations.
Contract Code Highlights
👉 View Full Contract on GitHub
Core Functions
create_game
Initializes the board as a shared object with two player addresses.public entry fun create_game(x_address: address, o_address: address, ctx: &mut TxContext) { // Initialization logic here transfer::share_object(game); }place_mark
Validates player turns and updates the board. EmitsGameEndEventupon completion.public entry fun place_mark(game: &mut TicTacToe, row: u8, col: u8, ctx: &mut TxContext) { // Game logic and winner checks }delete_game
Destroys the game object post-completion.public entry fun delete_game(game: TicTacToe) { object::delete(id); }
Prerequisites
Account Setup
| Alias | Address (Sample) | Role |
|---|---|---|
| Jason | 0x5c588...569a | Game Creator |
| Alice | 0x2d178...2d19 | Player X |
| Bob | 0xf2e6f...09f0 | Player O |
export ALICE=0x2d178b9704706393d2630fe6cf9415c2c50b181e9e3c7a977237bb2929f82d19
export BOB=0xf2e6ffef7d0543e258d4c47a53d6fa9872de4630cc186950accbd83415b009f0Deployment Steps
Publish Contract
sui client publish --gas-budget 100000000- Record
PACKAGE_IDfrom output.
- Record
Initialize Game
sui client call --function create_game --package $PACKAGE_ID --args $ALICE $BOB- Note the shared
GAMEobject ID.
- Note the shared
Gameplay Walkthrough
Player X (Alice) Moves
sui client call --function place_mark --args $GAME 0 0Player O (Bob) Responds
sui client call --function place_mark --args $GAME 1 2Continue Alternating Turns
- Repeat until a winner emerges or the board fills.
Game Termination
- Trophy awarded to the winner.
Delete the game object:
sui client call --function delete_game --args $GAME
FAQ
Q1: What distinguishes shared object Tic-Tac-Toe from centralized versions?
A1: Shared objects enable direct player interactions, removing the need for admin-mediated moves and reducing transaction steps.
Q2: How are winners determined?
A2: The contract checks for three consecutive marks (X or O) horizontally, vertically, or diagonally.
Q3: Can anyone delete a game?
A3: Currently, yes—though permissions can be added to restrict deletion to players or admins.
Further Reading
👉 Join the Sui Startrek Program to explore more SUI Move examples!