Time for action – check for a win
The CheckForWin
function is ready to start cranking out the hits! Add a win check to the ClickSquare
function:
square.GetComponent.<Square>().player = currentPlayer;
if(CheckForWin(square)) print("player " + currentPlayer + " wins!!!");
Smashed into a single succinct line, this code runs the CheckForWin
function, passing it a reference to the Square that was just clicked. If that function call resolves to true
(because CheckForWin
returns true), then Unity should print a statement saying that the current player has won.
Here is the long form version of that same code:
if(CheckForWin(square) == true) { print("player " + currentPlayer + " wins!!!"); }
Test the game. Try letting either X or O win the game. It works, and the message is displayed to the bottom of the Unity interface, and to the Console window: