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)]; }
Add this line to the top of the script:
#pragma strict
import System.Collections.Generic;
What just happened – making a list, checking it twice
Let's break this code down.
var square:GameObject...