Time for action – begin at the end

There's no shame in starting simple. Let's start writing these functions from the bottom up, until we reach the most complex ones at the top.

The GetRandomEmptySquare() function does what our stupidest possible computer player does: finds any empty square and marks it with no thought to strategy. We can just reuse code we've already written:

function GetRandomEmptySquare():GameObject
{
  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)];
  return square;
}

Note

This ...

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.