SUI Move Official Example Contract Practice - Game Category: Shared Object Tic-Tac-Toe (shared_tic_tac_toe)

·

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

Contract Code Highlights

👉 View Full Contract on GitHub

Core Functions

  1. 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);
    }
  2. place_mark
    Validates player turns and updates the board. Emits GameEndEvent upon completion.

    public entry fun place_mark(game: &mut TicTacToe, row: u8, col: u8, ctx: &mut TxContext) {
        // Game logic and winner checks
    }
  3. delete_game
    Destroys the game object post-completion.

    public entry fun delete_game(game: TicTacToe) {
        object::delete(id);
    }

Prerequisites

Account Setup

AliasAddress (Sample)Role
Jason0x5c588...569aGame Creator
Alice0x2d178...2d19Player X
Bob0xf2e6f...09f0Player O
export ALICE=0x2d178b9704706393d2630fe6cf9415c2c50b181e9e3c7a977237bb2929f82d19
export BOB=0xf2e6ffef7d0543e258d4c47a53d6fa9872de4630cc186950accbd83415b009f0

Deployment Steps

  1. Publish Contract

    sui client publish --gas-budget 100000000
    • Record PACKAGE_ID from output.
  2. Initialize Game

    sui client call --function create_game --package $PACKAGE_ID --args $ALICE $BOB
    • Note the shared GAME object ID.

Gameplay Walkthrough

  1. Player X (Alice) Moves

    sui client call --function place_mark --args $GAME 0 0
  2. Player O (Bob) Responds

    sui client call --function place_mark --args $GAME 1 2
  3. Continue Alternating Turns

    • Repeat until a winner emerges or the board fills.
  4. 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!