This blog, is a continuation of part1 where I explained the basic flow. Now, Let us dive into the main code :)
Declaring Constants & Structs
module tictactoe::game {
use std::error;
use std::signer;
use std::string::{String, utf8};
use std::vector;
use aptos_framework::event;
use aptos_framework::timestamp;
const EINVALID_MOVE: u64 = 1;
const EINVALID_PLAYER: u64 = 2;
const EINVALID_INDEX: u64 = 3;
const EGAME_OVER: u64 = 4;
const PLAYER_X: u8 = 1;
const PLAYER_O: u8 = 2;
const EMPTY: u8 = 0;
struct Game has key {
board: vector<u8>,
current_turn: u8,
winner: u8,
}
Code Explanation
Constants: Error codes and player identifiers are defined. EINVALID_MOVE, EINVALID_PLAYER, EINVALID_INDEX, and EGAME_OVER are error codes for different invalid states. PLAYER_X and PLAYER_O represent the two players, and EMPTY represents an empty cell on the board.
Game Struct: The Game struct holds the game's state, including the board (a vector of 9 cells), the current turn, and the winner.
Creating Event Structs
#[event]
struct MoveEvent has drop, store {
player: u8,
position: u8,
}
#[event]
struct WinEvent has drop, store {
winner: u8,
}
#[event]
struct DrawEvent has drop, store {
}
Code Explanation
MoveEvent: Emitted when a player makes a move, containing the player and the position.
WinEvent: Emitted when a player wins, containing the winner.
DrawEvent: Emitted when the game is a draw.
Initializing a Game
public entry fun init_game(account: &signer) {
move_to(account, Game {
board: vector::from_elem(EMPTY, 9),
current_turn: PLAYER_X,
winner: EMPTY,
});
}
Making a Move
public entry fun make_move(account: &signer, position: u8) acquires Game {
let game = borrow_global_mut<Game>(signer::address_of(account));
assert!(game.winner == EMPTY, EGAME_OVER);
assert!(position < 9, EINVALID_INDEX);
if (game.board[position] != EMPTY) {
abort EINVALID_MOVE;
}
game.board[position] = game.current_turn;
event::emit(MoveEvent { player: game.current_turn, position });
if (check_winner(&game.board, game.current_turn)) {
game.winner = game.current_turn;
event::emit(WinEvent { winner: game.current_turn });
} else if (is_draw(&game.board)) {
event::emit(DrawEvent {});
} else {
game.current_turn = if (game.current_turn == PLAYER_X) { PLAYER_O } else { PLAYER_X };
}
}
Code Explanation
It first checks if the game is over and if the position is valid.
If the cell at the given position is not empty, it aborts with EINVALID_MOVE.
It updates the board with the current player's move and emits a MoveEvent.
It checks if the current player has won using the check_winner function. If so, it sets the winner and emits a WinEvent.
If the board is full and there is no winner, it emits a DrawEvent.
If the game is still ongoing, it switches the turn to the other player.
Resetting the Game
public entry fun reset_game(account: &signer) acquires Game {
let game = borrow_global_mut<Game>(signer::address_of(account));
game.board = vector::from_elem(EMPTY, 9);
game.current_turn = PLAYER_X;
game.winner = EMPTY;
}
Creating View Functions to Access Information related to the Game Copy
#[view]
public fun get_board(): vector<u8> acquires Game {
let game = borrow_global<Game>(signer::address_of(account));
game.board
}
#[view]
public fun get_current_turn(): u8 acquires Game {
let game = borrow_global<Game>(signer::address_of(account));
game.current_turn
}
#[view]
public fun get_winner(): u8 acquires Game {
let game = borrow_global<Game>(signer::address_of(account));
game.winner
}
Checking the Winner of the Game
inline fun check_winner(board: &vector<u8>, player: u8): bool {
let win_positions = vector::vector([
vector::vector([0, 1, 2]),
vector::vector([3, 4, 5]),
vector::vector([6, 7, 8]),
vector::vector([0, 3, 6]),
vector::vector([1, 4, 7]),
vector::vector([2, 5, 8]),
vector::vector([0, 4, 8]),
vector::vector([2, 4, 6])
]);
vector::any(&win_positions, move |positions| {
vector::all(positions, move |&pos| board[pos] == player)
})
}
Code Explanation
win_positions: Defines all possible winning positions on the board.
vector::any: Checks if any of the winning positions are fully occupied by the current player.
vector::all: Checks if all positions in a winning line are occupied by the current player.
This function returns true if the current player has a winning combination, and false otherwise.
Checking for the Draw
inline fun is_draw(board: &vector<u8>): bool {
!vector::any(board, move |&cell| cell == EMPTY)
}
Conclusion
In this blog, we've explored a comprehensive implementation of a Tic Tac Toe game using the Move language on the Aptos blockchain platform. We covered the entire code, explaining each part in detail, from initializing the game, making moves, checking for winners, emitting events, resetting the game, and viewing the game state.
Building decentralized applications with Move and Aptos offers a secure and efficient way to develop robust blockchain-based games and other applications. By understanding the code structure and logic presented here, you can extend and customize this Tic Tac Toe game or create your own dApps on the Aptos platform.
Top comments (0)