Time for action – it takes two to Tic Tac Toe
A one-playerTic Tac Toe game sounds like a recipe for a bad time. Let's get the O's in there to liven things up.
Add this line to the GameLogic script:
var XPiece:GameObject;
var OPiece:GameObject;
var currentPlayer:int = 1;
And later:
function ClickSquare(square:GameObject) { var piece:GameObject; if(currentPlayer == 1) { piece = XPiece; } else { piece = OPiece; } Instantiate(piece, square.transform.position, Quaternion.identity); currentPlayer ++; if(currentPlayer > 2) currentPlayer = 1; }
Try it out. Now when you click, the game alternates between X's and O's.
What just happened – alternating between players
Let's take a closer look at that code.
var currentPlayer:int = 1;
We begin the game with currentPlayer
set to 1
.
var piece:GameObject; if(currentPlayer == 1) { piece = XPiece; } else { piece = OPiece; }
This chunk of code defines a variable called piece
. If currentPlayer
is 1
, it gets set to XPiece
(which is defined...