Untitled

tictactoe [ unity ]
 avatar
unknown
csharp
4 years ago
3.0 kB
9
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager_ : MonoBehaviour
{
    public Text statusText;
    public GameObject blockPanel;

    public int switchTurn = 1;
    List<int> board = new List<int>
    { 0, 0, 0,
      0, 0, 0,
      0, 0, 0 };

    private void Awake()
    {
        ResetBoard();
    }

    void ResetBoard()
    {
        for (int i = 0; i < board.Count; i++)
        {
            board[i] = 0;
        }

        statusText.text = "";
    }

    public void Place(Button button, int index)
    {
        //Disable button
        //Set Colour of button and disable it.
        ColorBlock colors = button.colors;

        if (switchTurn == 1)
        {
            //Turn into Red
            colors.disabledColor = new Color32(255, 100, 100, 255);

            //Place
            board[index] = 1;

        }

        else if (switchTurn == 2)
        {
            //Turn into Blue
            colors.disabledColor = new Color32(0, 0, 255, 255);

            //Place
            board[index] = 2;
        }

        button.colors = colors;
        button.interactable = false;

        checkWin();
    }

    void checkWin()
    {
        //row 1
        if(board[0] == switchTurn && board[1] == switchTurn && board[2] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //row 2
        else if (board[3] == switchTurn && board[4] == switchTurn && board[5] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //row 3
        else if (board[6] == switchTurn && board[7] == switchTurn && board[8] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //Column 1
        if (board[0] == switchTurn && board[3] == switchTurn && board[6] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //Column 1
        else if (board[1] == switchTurn && board[4] == switchTurn && board[7] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //Column 1
        else if (board[2] == switchTurn && board[5] == switchTurn && board[8] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        //diagonal
        if(board[0] == switchTurn && board[4] == switchTurn && board[8] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        else if (board[2] == switchTurn && board[4] == switchTurn && board[6] == switchTurn) { statusText.text = "Player " + switchTurn + " wins!"; blockPanel.SetActive(true); }

        switchTurn++;

        if (switchTurn > 2)
            switchTurn = 1;

    }

}
Editor is loading...