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

WHAT TO KNOW - Sep 10 - - Dev Community

<!DOCTYPE html>





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

<br> body {<br> font-family: Arial, sans-serif;<br> margin: 0;<br> padding: 20px;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { color: #333; } code { background-color: #f2f2f2; padding: 2px 5px; border-radius: 3px; } pre { background-color: #f2f2f2; padding: 10px; border-radius: 5px; overflow-x: auto; } </code></pre></div> <p>



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



Introduction


In the previous part of this series, we laid the foundation for our Tic Tac Toe game on Aptos by setting up the project, defining the game board, and establishing the basic player interactions. Now, in this second part, we will delve deeper into the core logic of the game, implementing the winning conditions and player turn management.

This article aims to provide a comprehensive guide to building a Tic Tac Toe game on Aptos using the Move language. It will cover the following:

  • Implementing Winning Conditions: We will write Move code to determine if a player has won the game.
  • Managing Player Turns: We'll create logic to switch between players' turns and ensure fair play.
  • Handling Game End: We will implement code to identify the winner or a draw, and to conclude the game.

Let's dive into the details!


Implementing Winning Conditions


Determining if a player has won in Tic Tac Toe requires checking all possible winning combinations: three in a row, column, or diagonal. We will achieve this in Move by creating a function that iterates through all these combinations and checks if they are filled by the same player's symbol.
fun check_win(board: &amp;vector
  <u8>
   , player: u8): bool {
    // Horizontal check
    for i in 0..3 {
        if board[i * 3] == player &amp;&amp; board[i * 3 + 1] == player &amp;&amp; board[i * 3 + 2] == player {
            return true;
        }
    }

    // Vertical check
    for i in 0..3 {
        if board[i] == player &amp;&amp; board[i + 3] == player &amp;&amp; board[i + 6] == player {
            return true;
        }
    }

    // Diagonal checks
    if board[0] == player &amp;&amp; board[4] == player &amp;&amp; board[8] == player {
        return true;
    }

    if board[2] == player &amp;&amp; board[4] == player &amp;&amp; board[6] == player {
        return true;
    }

    false // No winning combination found
}

This function takes the game board and the current player's symbol as input. It then iterates through all possible winning combinations and returns true if the player has three of their symbols in a row, column, or diagonal, otherwise it returns false.


Managing Player Turns


To ensure a fair game, we need to alternate between player turns. We will introduce a variable in our state called current_player to track the active player.
struct TicTacToe {
    board: vector
   <u8>
    ,
    current_player: u8,
    game_over: bool,
}

Now, we need to update the make_move function to change the current_player after each move.

fun make_move(tic_tac_toe: &amp;mut TicTacToe, position: u8, player: u8) {
    // ... (rest of the code from Part 1)

    // Update current player
    tic_tac_toe.current_player = if tic_tac_toe.current_player == 1 { 2 } else { 1 };
}

This code checks the current player and switches it to the other player after each move.


Handling Game End


Once a player wins or the board is full, the game should end. We will update the make_move function to detect these conditions and set the game_over flag accordingly.
fun make_move(tic_tac_toe: &amp;mut TicTacToe, position: u8, player: u8) {
    // ... (rest of the code from Part 1 and Part 2)

    // Check for win condition
    if check_win(&amp;tic_tac_toe.board, player) {
        tic_tac_toe.game_over = true;
        // ... (handle win logic)
    }

    // Check for draw condition
    if tic_tac_toe.board.len() == 9 {
        tic_tac_toe.game_over = true;
        // ... (handle draw logic)
    }
}

Here, we first check if the current move leads to a win for the player. If it does, we set game_over to true and handle the win logic. We then check if the board is full, and if so, set game_over to true and handle the draw logic.


Conclusion


In this second part, we have covered the core gameplay logic of our Tic Tac Toe game on Aptos, implementing winning conditions, managing player turns, and handling game end. We have learned how to use the Move language to create functions that check for winning combinations, switch player turns, and determine the outcome of the game.

Building this game on Aptos offers several advantages:

  • Decentralization: The game logic is hosted on a blockchain, making it resistant to censorship and manipulation.
  • Transparency: All game actions and data are publicly available on the blockchain, ensuring fairness and accountability.
  • Immutability: Once a game is played, the outcome and all actions are permanently recorded on the blockchain, preventing tampering.

While we have implemented the core game logic, this is just the beginning. We can further enhance the game by:

  • Adding user interfaces: Create a user-friendly interface for players to interact with the game.
  • Implementing betting features: Allow players to wager on the outcome of the game.
  • Introducing more complex game modes: Explore different game modes and variants of Tic Tac Toe.

By building on this foundation, we can create a fully functional and engaging Tic Tac Toe game on Aptos that leverages the benefits of blockchain technology.


Example Code


```move

module TicTacToe {
use std::vector;

struct TicTacToe has key {
    board: vector
<u8>
 ,
    current_player: u8,
    game_over: bool,
}

fun init() {
    let board: vector
 <u8>
  = vector::empty();
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);
    vector::push_back(&amp;mut board, 0);

    let tic_tac_toe = TicTacToe {
        board: board,
        current_player: 1,
        game_over: false,
    };

    move_to(Self::account_address(), tic_tac_toe);
}

fun make_move(tic_tac_toe: &amp;mut TicTacToe, position: u8, player: u8) {
    // Check if game is over
    if tic_tac_toe.game_over {
        return;
    }

    // Check if position is valid
    if position &lt; 0 || position &gt; 8 {
        return;
    }

    // Check if position is empty
    if tic_tac_toe.board[position as usize] != 0 {
        return;
    }

    // Make the move
    tic_tac_toe.board[position as usize] = player;

    // Check for win condition
    if check_win(&amp;tic_tac_toe.board, player) {
        tic_tac_toe.game_over = true;
        // ... (handle win logic)
    }

    // Check for draw condition
    if tic_tac_toe.board.len() == 9 {
        tic_tac_toe.game_over = true;
        // ... (handle draw logic)
    }

    // Update current player
    tic_tac_toe.current_player = if tic_tac_toe.current_player == 1 { 2 } else { 1 };
}

fun check_win(board: &amp;vector
  <u8>
   , player: u8): bool {
    // Horizontal check
    for i in 0..3 {
        if board[i * 3] == player &amp;&amp; board[i * 3 + 1] == player &amp;&amp; board[i * 3 + 2] == player {
            return true;
        }
    }

    // Vertical check
    for i in 0..3 {
        if board[i] == player &amp;&amp; board[i + 3] == player &amp;&amp; board[i + 6] == player {
            return true;
        }
    }

    // Diagonal checks
    if board[0] == player &amp;&amp; board[4] == player &amp;&amp; board[8] == player {
        return true;
    }

    if board[2] == player &amp;&amp; board[4] == player &amp;&amp; board[6] == player {
        return true;
    }

    false // No winning combination found
}

}




This code provides a basic implementation of the Tic Tac Toe game on Aptos. You can use this code as a starting point and build upon it to create a more feature-rich and engaging game.

**Note:** This code example does not include user interfaces or betting features. You will need to implement these features separately if you want to create a complete game.
      </u8>
     </u8>
    </u8>
   </u8>
  </u8>
 </body>
</html>
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player