Search Unity

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

Discussion in 'Scripting' started by catafest, Jun 6, 2018.

  1. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
    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 ) :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8.     public Text[] buttonList;
    9.     private string playerSide;
    10.  
    11.     public GameObject gameOverPanel;
    12.     public Text gameOverText;
    13.  
    14.     private int moveCount ;
    15.  
    16.     public GameObject restartButton;
    17.  
    18.     void Awake()
    19.     {
    20.         moveCount = 25;
    21.         gameOverPanel.SetActive(false);
    22.         SetBoardInteractable(true);
    23.         SetGameControllerReferenceOnButtons();
    24.         playerSide = "X";
    25.         restartButton.SetActive(false);
    26.     }
    27.  
    28.     void SetGameControllerReferenceOnButtons() {
    29.         for (int i=0; i < buttonList.Length; i++) {
    30.             buttonList[i].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
    31.         }
    32.     }
    33.  
    34.     public string GetPlayerSide() {
    35.         return playerSide;
    36.         }
    37.  
    38.     public void EndTurn() {
    39.         moveCount--;
    40.  
    41.         foreach (Text str in buttonList)
    42.         {
    43.         }
    44.  
    45.         //Debug.Log(message: item.ToString());
    46.         /* this part
    47.         if (buttonList[11].text == playerSide &&
    48.                 buttonList[14].text == playerSide &&
    49.                 buttonList[12].text == playerSide &&
    50.                 buttonList[13].text == playerSide)
    51.             {
    52.                 GameOver(playerSide);
    53.             }
    54.  
    55.             if (buttonList[0].text == playerSide &&
    56.                 buttonList[5].text == playerSide &&
    57.                 buttonList[10].text == playerSide &&
    58.                 buttonList[15].text == playerSide)
    59.             {
    60.                 GameOver(playerSide);
    61.             }
    62.             */
    63.             if (moveCount <= 0)
    64.             {
    65.                 GameOver("draw");
    66.             }
    67.  
    68.         ChangeSides();
    69.     }
    70.  
    71.     void GameOver(string winningPlayer)
    72.     {
    73.         SetBoardInteractable(false);
    74.  
    75.         if (winningPlayer == "draw") {
    76.             SetGameOverText(" Draw ");
    77.         } else {
    78.             SetGameOverText(playerSide + "Wins !");
    79.         }
    80.  
    81.         restartButton.SetActive(true);
    82.  
    83.     }
    84.  
    85.     void ChangeSides() {
    86.         playerSide = (playerSide == "X") ? "0" : "X";
    87.     }
    88.  
    89.     void SetGameOverText(string value) {
    90.         gameOverPanel.SetActive(true);
    91.         gameOverText.text = value;
    92.     }
    93.  
    94.     public void RestartGame() {
    95.         playerSide = "X";
    96.         moveCount = 25;
    97.         gameOverPanel.SetActive(false);
    98.         SetBoardInteractable(true);
    99.  
    100.         for (int i = 0; i < buttonList.Length; i++)
    101.         {      
    102.             buttonList[i].text = "";
    103.         }
    104.         restartButton.SetActive(false);
    105.     }
    106.  
    107.     void SetBoardInteractable(bool toggle) {
    108.         for (int i = 0; i < buttonList.Length; i++)
    109.         {
    110.             buttonList[i].GetComponentInParent<Button>().interactable = toggle;
    111.        
    112.         }
    113.     }
    114. }
     
    Boz0r likes this.
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    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:

    Code (CSharp):
    1. [x][y] == [x+1][y] == [x+2][y] == [x+3][y]
    2. [x][y] == [x+1][y+1] == [x+2][y+2] == [x+3][y+3]
    3. ...
     
  3. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
    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 ?