Time for action – pseu pseu pseudocode
To translate our prioritized peudocode strategy list into actual code, I propose that we turn each attempt at playing a piece into its own function. If we find a valid move, the function will return the square where the computer should play a piece. If there's no valid move, the function will return null
, which is the computer code equivalent of "does not exist/totally isn't a thing".
In the ComputerTakeATurn
function, let's get rid of (or comment out) the code that randomly chooses a square, which is bolded in the following code:
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)]; PlacePiece(OPiece, square); }
We'll replace it...