I am following this tutorial to make a basic Tic Tac Toe game.(Unity Tutorial: 2player Tictactoe basics - YouTube) I have followed whole the tutorial. Now I have to make a winning script to show which player has won. On a question, the video uploader has said this:
For winning condition, 1st make an array of length 9 in GameScript, which will keep the status of each cell (Status can be Empty,Cross or Nought). Whenever a player makes his move, check all the rows and columns and both diagonals. If any of them contains 3 Noughts or 3 Crosses then it’s a win situation, else if the board contains an Empty cell then next player will make his move. If previous two condition fails then it’s a draw.
I am new to unity scripting. I don’t know how to do what he has told. The code I’ve come to till now is this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameScript : MonoBehaviour
{
public GameObject cross, nought;
public enum Seed { EMPTY, CROSS, NOUGHT };
public Seed turn;
public Text Instructions;
void Awake()
{
Instructions.text = "Turn: " + turn.ToString();
}
public void SpawnNew(GameObject obj)
{
if (turn == Seed.CROSS)
{
Instantiate(cross, obj.transform.position, Quaternion.identity);
turn = Seed.NOUGHT;
}
else
{
Instantiate(nought, obj.transform.position, Quaternion.identity);
turn = Seed.CROSS;
}
Destroy(obj.gameObject);
Instructions.text = "Turn: " + turn.ToString();
}
}
Like I said before, I ain’t understanding what he has told to make the winning script. But I’ve made up a basic algorithm.
After each turn is played the script will check these steps:
1.Check that if Cells{(1 2 3) or(4 5 6)or(7 8 9)or(1 4 7)or (2 5 8) or (3 6 9) or (1 5 9)or(3 5 7)} contain Noughts.
If so, go to scene named ‘aftern’. Or else, go to step 2.
2.Check that if Cells{(1 2 3) or(4 5 6)or(7 8 9)or(1 4 7)or (2 5 8) or (3 6 9) or (1 5 9)or(3 5 7)} contain Crosses.
If so, go to scene named ‘afterc’. Or else, go to step 3.
3.Check if there are any emptycells. If so, carry on playing. Or else, go to Step 4.
4.Go to scene named ‘afterd’.
NOTE: I’ve set the cells with names(Such as the first cell in first column is ‘emptycell 1’, the second cell in the first column is ‘emptycell 2’, the 3rd cell in first column is ‘emptycell 3’, the first cell in 2nd column is ‘emptycell 4’ and so on).
Though I’ve set up the algorithm, I can’t write the code. If anyone knows what will be the code and where it will be(I mean what the final code will be) and answers it, I would be very thankful