Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3841

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:27:05+00:00 2024-11-26T06:27:05+00:00

Building a Tic Tac Toe Game on Aptos with Move Language-Part2

  • 61k

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,     }  
Enter fullscreen mode Exit fullscreen mode

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 { }  
Enter fullscreen mode Exit fullscreen mode

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,         });     }  
Enter fullscreen mode Exit fullscreen mode

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 };         }     }  
Enter fullscreen mode Exit fullscreen mode

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;  }  
Enter fullscreen mode Exit fullscreen mode

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 }  
Enter fullscreen mode Exit fullscreen mode

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)  }) } 
Enter fullscreen mode Exit fullscreen mode

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)  } 
Enter fullscreen mode Exit fullscreen mode

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.

aptosbeginnersweb3webdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.