Skip to main content

Design the data model

In this section, we'll walk you through designing a simple database model for your Tic Tac Toe game.

In our game, the database model consists of the game entity. We'll create a table to manage the state of the game board, including whose turn it is and the current status of the game.

Implement the model in Rell

In Chromia, you define your dapp's data model using a language called Rell. Let's break down the Rell code that corresponds to our model:

src/tic_tac_toe/entities.rell
enum game_status {
created,
active,
winner,
draw
}

enum slot_states {
free,
zero,
one
}

entity game {
created_by: ft4_account;
created_at: timestamp = get_last_block_time();
mutable player2_account_rowid: rowid = rowid(0);
mutable last_step_by: rowid;
mutable last_step_at: timestamp = get_last_block_time();
mutable winner: rowid = rowid(0);
mutable status: game_status = game_status.created;
mutable slot_1: slot_states = slot_states.free;
mutable slot_2: slot_states = slot_states.free;
mutable slot_3: slot_states = slot_states.free;
mutable slot_4: slot_states = slot_states.free;
mutable slot_5: slot_states = slot_states.free;
mutable slot_6: slot_states = slot_states.free;
mutable slot_7: slot_states = slot_states.free;
mutable slot_8: slot_states = slot_states.free;
mutable slot_9: slot_states = slot_states.free;
}

The game entity manages the game board's state and the game's flow. It includes:

  • created_by: This field stores the identifier (ft4_account) of the account that created the game.
  • created_at: Automatically sets the timestamp to the block time when the game is created, tracking the game’s start time.
  • mutable player2_account_rowid: Initially set to zero (rowid(0)), this field is reserved to store the row ID of the second player once they join.
  • mutable last_step_by: Holds the row ID of the last player who made a move. This field is updated after each turn to identify which player made the latest move.
  • mutable last_step_at: Similar to created_at, this timestamp field is updated to track the exact time of the last move in the game.
  • mutable winner: Initially set to zero (rowid(0)), this field is updated with the row ID of the winning player once the game concludes. If there is no winner, it remains at zero.
  • mutable status: Keeps track of the game’s current state, starting with game_status.created. This could be updated to values representing other states, like "active" or "completed."
  • mutable slot_1 to mutable slot_9: Represent the nine positions on the Tic Tac Toe board. Each slot is initialized as slot_states.free and will change based on player moves, tracking whether a position is occupied by either player or remains free.

For a deeper understanding of managing relationships between entities, check out our Understand relationships in Rell course.

Great! Using Rell, you've created a basic database model for the Tic Tac Toe game on the Chromia blockchain. This model allows you to efficiently manage and maintain the state of the game board. Now, you're ready to build your Tic Tac Toe game on Chromia!