Herpa derp derp

Just to get this up and running, we'll make the artificial intelligence as dumb as possible. The computer will scan the grid for empty Squares, choose one at random, and place an O there.

We'll accomplish this by looping through our 1-dimensional array of Square references, adding any empty Squares to a separate list, and then choosing a Square at random from that list.

Add this code to the ComputerTakeATurn function:

function ComputerTakeATurn()
{
  var square:GameObject;
  var aEmptySquares:List.<GameObject> = new List.<GameObject>();
  
  for(var i:int = 0; i < aSquares.Length; i++)
  {
    square = aSquares[i];
    if(square.GetComponent.<Square>().player == 0) 
      aEmptySquares.Add(square);
  }
 square = aEmptySquares[Random.Range(0,aEmptySquares.Count)]; ...

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.