Need-to-know basis

By writing the line in this long-winded way, we're checking all of the possible win scenarios by searching the whole grid, when we should really only be concerned about the square where the current player just finished placing a piece.

Given any square's x and y values, we can just check the squares in the same row and column, instead of checking every single win scenario. Here's what a line of code looks like if we only check the square's row:

if(aGrid[0,square.GetComponent.<Square>().y].GetComponent.<Square>().player == currentPlayer && aGrid[1, square.GetComponent.<Square>().y].GetComponent.<Square>().player == currentPlayer && aGrid[2, square.GetComponent.<Square>().y].GetComponent.<Square>().player == currentPlayer) return ...

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.