Codesplosion

In earlier chapters, I've said that it's more important to get your code working than to make it elegant. However in some situations, like the one you're about to experience, the code is so preposterously inelegant that it cries out to be refactored on the spot.

Just like in math class where they make you add numbers up a million times before teaching multiplication, let's start by doing this the long way. Don't bother writing any of this code down. Just follow along, and look for ways in which it can be cleaned up.

We can look for 3-in-a-row as follows:

// Check the first row: if(aGrid[0,0].GetComponent.<Square>().player == currentPlayer && aGrid[1,0].GetComponent.<Square>().player == currentPlayer && aGrid[2,0].GetComponent.<Square>().player ...

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.