game theory algorithms to solve script of lists/matrix ( tic tac toe )

I want to create a game similar with tic tac toe , but with a large matrix.
I start with a working example tic tac toe and working well.
About my issue:
I need scripts for computer player script and a solver script for win tasks ( G = number of cells intu line or
line or diagonal)
I start with a simple grid interface with buttons named from GridSpace (0) to GridSpace (N).
I need some help to create a computer the opponent’s script (computer player script )
I know can I can start from basics (if else) scripting to Unity Machine Learning Agents (ML-Agents for short).
I think to the basic and a good idea is to create lists for player ( human and computer) but also to find a algorithm to parse this lists.
The big issue is the large number of buttons ( If I used for N = 6 will be a matrix 6x6) and if I set the limit of solve (G = 4 ) this will increase the number of conditions to resolve if the next will be smaller (G = 2).
An idea would be to use list intersections and list shaders like lists
My requirement:
Take a look at the script to the multiline commentary (/* this part …) and try to come up with a practical solution .
Initial working script (this check the basic matrix and show win when the player align cells - the basic tic tac toe game interface ) :

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

public class GameController : MonoBehaviour {

    public Text[] buttonList;
    private string playerSide;

    public GameObject gameOverPanel;
    public Text gameOverText;

    private int moveCount ;

    public GameObject restartButton;

    void Awake()
    {
        moveCount = 25;
        gameOverPanel.SetActive(false);
        SetBoardInteractable(true);
        SetGameControllerReferenceOnButtons();
        playerSide = "X";
        restartButton.SetActive(false);
    }

    void SetGameControllerReferenceOnButtons() {
        for (int i=0; i < buttonList.Length; i++) {
            buttonList[i].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
        }
    }

    public string GetPlayerSide() {
        return playerSide;
        }

    public void EndTurn() {
        moveCount--;

        foreach (Text str in buttonList)
        {
        }

        //Debug.Log(message: item.ToString());
        /* this part
        if (buttonList[11].text == playerSide &&
                buttonList[14].text == playerSide &&
                buttonList[12].text == playerSide &&
                buttonList[13].text == playerSide)
            {
                GameOver(playerSide);
            }

            if (buttonList[0].text == playerSide &&
                buttonList[5].text == playerSide &&
                buttonList[10].text == playerSide &&
                buttonList[15].text == playerSide)
            {
                GameOver(playerSide);
            }
            */
            if (moveCount <= 0)
            {
                GameOver("draw");
            }
  
        ChangeSides();
    }

    void GameOver(string winningPlayer)
    {
        SetBoardInteractable(false);

        if (winningPlayer == "draw") {
            SetGameOverText(" Draw ");
        } else {
            SetGameOverText(playerSide + "Wins !");
        }

        restartButton.SetActive(true);

    }

    void ChangeSides() {
        playerSide = (playerSide == "X") ? "0" : "X";
    }

    void SetGameOverText(string value) {
        gameOverPanel.SetActive(true);
        gameOverText.text = value;
    }

    public void RestartGame() {
        playerSide = "X";
        moveCount = 25;
        gameOverPanel.SetActive(false);
        SetBoardInteractable(true);

        for (int i = 0; i < buttonList.Length; i++)
        {      
            buttonList[i].text = "";
        }
        restartButton.SetActive(false);
    }

    void SetBoardInteractable(bool toggle) {
        for (int i = 0; i < buttonList.Length; i++)
        {
            buttonList[i].GetComponentInParent<Button>().interactable = toggle;
        
        }
    }
}
1 Like

Do yourself a favour and separate controller and GUI code into two different classes. Checking on the text in a list of buttons is a hassle, Try using a multi-dimensional array of enums instead.

Think about your winning conditions. You basically have four different winning outcomes: four vertical, four horizontal, for four diagonal down and four diagional up. Try something in this vein:

[x][y] == [x+1][y] == [x+2][y] == [x+3][y]
[x][y] == [x+1][y+1] == [x+2][y+2] == [x+3][y+3]
...

What ? This is a complex issue and divide my default example will start from a good algorithm versus a good script/ scripts.
The problem start from parse the area of data into algorithm and parse into to win condition , not to using a multi-dimensional array because this will increase the …
what C# code is this ?