daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [System.Serializable]
  2.   public class Player
  3.  {
  4. public Image panel;
  5. public Text text;
  6. public Button button;
  7. }
  8. [System.Serializable]
  9. public class PlayerColor
  10. {
  11. public Color panelColor;
  12. public Color textColor;
  13. }
  14.  
  15. public class GameController : MonoBehaviour
  16. {
  17. public Text[,,] buttonList;
  18. public GameObject gameOverPanel;
  19. public Text gameOverText;
  20. private string playerSide;
  21. private string computerSide;
  22. private int moveCount;
  23. public GameObject restartButton;
  24. public GameObject startInfo;
  25.  
  26. public Player playerX;
  27. public Player playerO;
  28. public PlayerColor activePlayerColor;
  29. public PlayerColor inactivePlayerColor;
  30.  
  31. public bool playerMove;
  32. public float delay;
  33. private int value;
  34.  
  35. public enum State {Null, X, O};
  36. public State[,] states = new State[0,0];
  37.  
  38. public List<State> state = new List<State>();
  39.  
  40. void Awake()
  41. {
  42.     SetGameControllerReferenceOnButtons();
  43.     gameOverPanel.SetActive(false);
  44.     moveCount = 0;
  45.     restartButton.SetActive(false);
  46.     playerMove = true;
  47. }
  48.  
  49. public void Update()
  50. {
  51.     if (playerMove == false)
  52.     {
  53.         delay += delay * Time.deltaTime;
  54.         if (delay >= 100)
  55.         {
  56.             value = Random.Range(0, 8);
  57.             if (buttonList[value].GetComponentInParent<Button>().interactable == true)
  58.             {
  59.                 buttonList[value].text = GetComputerSide();
  60.                 buttonList[value].GetComponentInParent<Button>().interactable = false;
  61.                 EndTurn();
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. void SetGameControllerReferenceOnButtons()
  68. {
  69.     for (int i = 0; i < buttonList.Length; i++)
  70.     {
  71.        buttonList[i].GetComponentInParent<GridSpace>().SetGameControllerReference(this);  
  72.     }
  73. }
  74.  
  75. public void SetStartingSide(string startingSide)
  76. {
  77.     playerSide = startingSide;
  78.     if(playerSide == "X")
  79.     {
  80.         computerSide = "O";
  81.         SetPlayerColors(playerX, playerO);
  82.     }
  83.     else
  84.     {
  85.         computerSide = "X";
  86.         SetPlayerColors(playerO, playerX);
  87.     }
  88.  
  89.     StartGame();
  90. }
  91.  
  92. void StartGame()
  93. {
  94.     SetBoardInteractable(true);
  95.     SetPlayerButtons(false);
  96.     startInfo.SetActive(false);
  97. }
  98.  
  99. public string GetPlayerSide()
  100. {
  101.     return playerSide;
  102. }
  103.  
  104. public string GetComputerSide()
  105. {
  106.     return computerSide;
  107. }
  108.  
  109. public void EndTurn()
  110. {
  111.     moveCount++;
  112.  
  113.     for (int i = 0; i < buttonList.Length; i++)
  114.     {
  115.         if (buttonList[0, 1, 2].text == playerSide && buttonList[3, 4, 5].text == playerSide && buttonList[6, 7, 8].text == playerSide)
  116.         {
  117.             GameOver(playerSide);
  118.         }
  119.  
  120.         else if (buttonList[0, 3, 6].text == playerSide && buttonList[1, 4, 7].text == playerSide && buttonList[2, 5, 8].text == playerSide)
  121.         {
  122.             GameOver(playerSide);
  123.         }
  124.  
  125.         else if (buttonList[0, 4, 8].text == playerSide && buttonList[2, 4, 6].text == playerSide)
  126.         {
  127.             GameOver(playerSide);
  128.         }
  129.  
  130.         else if (buttonList[0, 1, 2].text == computerSide && buttonList[3, 4, 5].text == computerSide && buttonList[6, 7, 8].text == computerSide)
  131.         {
  132.             GameOver(computerSide);
  133.         }
  134.  
  135.         else if (buttonList[0, 3, 6].text == computerSide && buttonList[1, 4, 7].text == computerSide && buttonList[2, 5, 8].text == computerSide)
  136.         {
  137.             GameOver(computerSide);
  138.         }
  139.  
  140.         else if (buttonList[0, 4, 8].text == computerSide && buttonList[2, 4, 6].text == computerSide)
  141.         {
  142.             GameOver(computerSide);
  143.         }
  144.  
  145.         else if (moveCount >= 9)
  146.         {
  147.             GameOver("draw");
  148.         }
  149.         else
  150.         {
  151.             ChangeSides();
  152.             delay = 10;
  153.         }
  154.     }
  155.  
  156. }
  157.  
  158. void ChangeSides()
  159. {
  160.     //playerSide = (playerSide == "X") ? "O" : "X";
  161.     playerMove = (playerMove == true) ? false : true;
  162.     //if(playerSide == "X")
  163.     if(playerMove == true)
  164.     {
  165.         SetPlayerColors(playerX, playerO);
  166.     }
  167.     else
  168.     {
  169.         SetPlayerColors(playerO, playerX);
  170.     }
  171. }
  172.  
  173. void SetPlayerColors(Player newPlayer, Player oldPlayer)
  174. {
  175.     newPlayer.panel.color = activePlayerColor.panelColor;
  176.     newPlayer.text.color = activePlayerColor.textColor;
  177.  
  178.     oldPlayer.panel.color = inactivePlayerColor.panelColor;
  179.     oldPlayer.text.color = inactivePlayerColor.textColor;
  180. }
  181.  
  182. void GameOver(string winningPlayer)
  183. {
  184.     SetBoardInteractable(false);
  185.     if (winningPlayer == "draw")
  186.     {
  187.         SetGameOverText("Draw");
  188.         SetPlayerColorsInactive();
  189.     }
  190.     else
  191.     {
  192.         SetGameOverText(winningPlayer + " wins");
  193.     }
  194.     restartButton.SetActive(true);
  195. }
  196.  
  197. void SetGameOverText(string value)
  198. {
  199.     gameOverPanel.SetActive(true);
  200.     gameOverText.text = value;
  201. }
  202.  
  203. public void RestartGame()
  204. {
  205.     moveCount = 0;
  206.     gameOverPanel.SetActive(false);
  207.     restartButton.SetActive(false);
  208.     SetPlayerButtons(true);
  209.     startInfo.SetActive(true);
  210.     SetPlayerColorsInactive();
  211.     playerMove = true;
  212.     delay = 10;
  213.  
  214.     for (int i = 0; i < buttonList.Length; i++)
  215.     {
  216.         buttonList[i].text = "";
  217.     }
  218. }
  219.  
  220. void SetBoardInteractable(bool toggle)
  221. {
  222.     for (int i = 0; i < buttonList.Length; i++)
  223.     {
  224.         buttonList[i].GetComponentInParent<Button>().interactable = toggle;
  225.     }
  226. }
  227.  
  228. void SetPlayerButtons(bool toggle)
  229. {
  230.     playerX.button.interactable = toggle;
  231.     playerO.button.interactable = toggle;
  232. }
  233.  
  234. void SetPlayerColorsInactive()
  235. {
  236.     playerX.panel.color = inactivePlayerColor.panelColor;
  237.     playerX.text.color = inactivePlayerColor.textColor;
  238.     playerO.panel.color = inactivePlayerColor.panelColor;
  239.     playerO.panel.color = inactivePlayerColor.textColor;
  240. }
  241.  
  242.  
  243. }
  244.    
  245. if (buttonList[0].text == playerSide && buttonList[1].text == playerSide && buttonList[2].text == playerSide)
  246.         {
  247.             GameOver(playerSide);
  248.         }
  249.  
  250.         else if (buttonList[3].text == playerSide && buttonList[4].text == playerSide && buttonList[5].text == playerSide)
  251.         {
  252.             GameOver(playerSide);
  253.    
  254. for (int i = 0; i < buttonList.Length; i++)
  255.         {
  256.             if (buttonList[0, 1, 2].text == playerSide && buttonList[3, 4, 5].text == playerSide && buttonList[6, 7, 8].text == playerSide)
  257.             {
  258.                 GameOver(playerSide);
  259.             }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top