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, ...
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.