C# TicTacToe: AI replacing chosen squares or not choosing at all

I’ve been trying to make a TicTacToe game with C#, but instead of using X’s and O’s, I use colors: blue and red. THe cod ethat I’ve written works to some extent, but for some reason, the AI chooses the quares that I’ve chosen even though I had them checked. Also it seems sometimes the AI decides not even make a move.

Does anyone have any ideas what’s wrong here?

Here’s the code:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour
{
    public GameObject cubeA1;
    public GameObject cubeA2;
    public GameObject cubeA3;
    public GameObject cubeB1;
    public GameObject cubeB2;
    public GameObject cubeB3;
    public GameObject cubeC1;
    public GameObject cubeC2;
    public GameObject cubeC3;
    bool DidWin;
    int AINumber = 0;
    int AILine = 0;
    int c = 0;
    int d = 0;
    int i = 0;
    public bool CanAIMove = false;

    void Start()
    {
        DidWin = false;
    }

    void Update()
    {
        //Winning conditions
        if ((cubeA1.tag == "Chosen") && (cubeA2.tag == "Chosen") && (cubeA3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeB1.tag == "Chosen") && (cubeB2.tag == "Chosen") && (cubeB3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeC1.tag == "Chosen") && (cubeC2.tag == "Chosen") && (cubeC3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeA1.tag == "Chosen") && (cubeB1.tag == "Chosen") && (cubeC1.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeA1.tag == "Chosen") && (cubeB2.tag == "Chosen") && (cubeC3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeC1.tag == "Chosen") && (cubeB2.tag == "Chosen") && (cubeA3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeA2.tag == "Chosen") && (cubeB2.tag == "Chosen") && (cubeC2.tag == "Chosen"))
        {
            DidWin = true;
        }

        if ((cubeA3.tag == "Chosen") && (cubeB3.tag == "Chosen") && (cubeC3.tag == "Chosen"))
        {
            DidWin = true;
        }

        if(DidWin == true)
        {
            CanAIMove = false;
            Debug.Log(DidWin);
        }

    //AI movement
    NumberCheck:
        AINumber = Random.Range(1, 4);
        AILine = Random.Range(1, 4);
        if (i==0)
        {
            i++;
            c = AINumber;
            d = AILine;
            goto AIMoves;
        }
        else if(i!=0 && AINumber != c && AILine != d)
        {
            c = AINumber;
            d = AILine;
            goto NumberCheck;

        }

    AIMoves:
        if (AINumber == 1 && AILine == 1 && CanAIMove == true && (cubeA1.tag != "Chosen" && cubeA1.tag != "AIChosen"))
        {
            cubeA1.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 2 && AILine == 1 && CanAIMove == true && (cubeA2.tag != "Chosen" && cubeA2.tag != "AIChosen"))
        {
            cubeA1.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 3 && AILine == 1 && CanAIMove == true && (cubeA3.tag != "Chosen" && cubeA3.tag != "AIChosen"))
        {
            cubeA2.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 1 && AILine == 2 && CanAIMove == true && (cubeB1.tag != "Chosen" && cubeB1.tag != "AIChosen"))
        {
            cubeA3.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 2 && AILine == 2 && CanAIMove == true && (cubeB2.tag != "Chosen" && cubeB2.tag != "AIChosen"))
        {
            cubeB1.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 3 && AILine == 2 && CanAIMove == true && (cubeB3.tag != "Chosen" && cubeB3.tag != "AIChosen"))
        {
            cubeB2.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 1 && AILine == 3 && CanAIMove == true && (cubeC1.tag != "Chosen" && cubeC1.tag != "AIChosen"))
        {
            cubeC1.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

        else if (AINumber == 2 && AILine == 3 && CanAIMove == true && (cubeC2.tag != "Chosen" && cubeC2.tag != "AIChosen"))
        {
            cubeC2.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }

       else if (AINumber == 3 && AILine == 3 && CanAIMove == true && (cubeC3.tag != "Chosen" && cubeC3.tag != "AIChosen"))
        {
            cubeC3.tag = "AIChosen";
            CanAIMove = false;
            goto NumberCheck;
        }
    }
}

I’ve also included code from a script attached to every cube that can be pressed. It adds tags to selected cubes and handles colors and such:

public class BoxPresser : MonoBehaviour
{
    bool IsPressed = false;
    public Renderer render;
    Color PlayerColor = new Color(0, 0, 1, 1);
    Color AIColor = new Color(1, 0, 0, 1);
    public GameObject cube;
    public Controller controller;

    void Update()
    {
        if(cube.tag == "AIChosen")
        {
            render.material.SetColor("_Color", AIColor);
        }
    }
    
    void OnMouseOver()
    {

        if (Input.GetMouseButtonDown(0) && IsPressed == false && cube.tag != "AIChosen" && controller.CanAIMove == false)
        {
            render.material.SetColor("_Color", PlayerColor);
            cube.tag = "Chosen";
            IsPressed = true;
            controller.CanAIMove = true;
        }
    }

Hmmm… That’s some pretty involved code for a tic-tac-toe game…

While there are any number of ways you could simplify things, the immediate problem seems to be that in your “AIMoves” section, when the AI chooses a square, you assign the “AIChosen” tag to the square so you can avoid it later. However, if you look closely, you’re not assigning the tag to the appropriate square in each if block.

You should be assigning the tag to the 9 squares A1, A2, A3, B1, B2, B3, C1, C2, C3. Instead, you assign the tag to the squares: A1, A1, A2, A3, B1, B2, C1, C2, C3.

I assume the above is just caused by some cut/paste process where you forgot to update the cube variable names.