Create Tic Tac Toe game in unity with script code

Create Tic Tac Toe game in unity with script code

To create a Tic-Tac-Toe game ( oxo ) in Unity, you will need to do the following:

  1. Create a new Unity project and set up your game scene. You will need to create a grid of 3x3 game tiles and place them in your scene. You can use a 3D model for the tiles, or you can simply use 2D sprites.

  2. Set up the game logic. You will need to create a script to handle the game logic, including checking for wins, draws, and valid moves. You can use a 2D array to store the state of the game board, with elements representing each tile on the board. You will also need to create a way to alternate between player moves and check for wins or draws.

  3. Add user input. You will need to create a way for the players to make their moves, either by clicking on the game tiles or by using touch input. You can use the OnMouseDown() method to detect clicks on the game tiles and update the game board accordingly.

  4. Create a user interface. You will need to create a UI for the game, including a way to display the current player's turn and the game status (win, draw, etc.). You can use Unity's built-in UI elements, such as buttons and text labels, to create the UI.

  5. Test and debug your game. Make sure to test your game thoroughly to catch any bugs or issues. You can use Unity's built-in debugging tools, such as the console and the profiler, to help you identify and fix any problems.

C# TicTacToe Mobile Game

Here is an example of a simple Tic-tac-toe game written in C# that you can use as a starting point for creating your own Tic-tac-toe game for a mobile platform:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TicTacToe : MonoBehaviour
{
    // 2D array to represent the game board
    // 0 represents an empty space
    // 1 represents a player 1 piece
    // 2 represents a player 2 piece
    int[,] board = new int[3,3];

    // Keep track of which player's turn it is
    int currentPlayer = 1;

    // Function to place a piece on the board
    void PlacePiece(int x, int y)
    {
        // Make sure the space is empty before placing a piece
        if (board[x,y] == 0)
        {
            // Place the piece for the current player
            board[x,y] = currentPlayer;

            // Switch to the other player's turn
            if (currentPlayer == 1)
            {
                currentPlayer = 2;
            }
            else
            {
                currentPlayer = 1;
            }
        }
    }

    // Function to check if there is a winner
    int CheckForWinner()
    {
        // Check rows for a winner
        for (int i = 0; i < 3; i++)
        {
            if (board[i,0] == board[i,1] && board[i,1] == board[i,2])
            {
                return board[i,0];
            }
        }

        // Check columns for a winner
        for (int j = 0; j < 3; j++)
        {
            if (board[0,j] == board[1,j] && board[1,j] == board[2,j])
            {
                return board[0,j];
            }
        }

        // Check diagonals for a winner
        if (board[0,0] == board[1,1] && board[1,1] == board[2,2])
        {
            return board[0,0];
        }
        if (board[0,2] == board[1,1] && board[1,1] == board[2,0])
        {
            return board[0,2];
        }

        // If there is no winner, return 0
        return 0;
    }

    // Update is called once per frame
    void Update()
    {
        // Check for input to place a piece
        if (Input.GetMouseButtonDown(0))
        {
            // Get the mouse position on the screen
            Vector3 mousePos = Input.mousePosition;

            // Convert the mouse position to a point on the game board
            int x = (int)(mousePos.x / (Screen.width / 3));
            int y = (int)(mousePos.y / (Screen.height / 3));

            // Place a piece at the selected position
            PlacePiece(x, y);

            // Check for a winner
            int winner = CheckForWinner();
            if (winner > 0)
            {
                Debug.Log("Player " + winner + " wins!");
            }
        }
    }
}