When It’s a Draw

The if statements in aiTurn account for every possible move available to the AI. The statements also include a return so that if a block executes, the following if blocks won’t be reached. These facts tell you that the compiler won’t reach the bottom of aiTurn unless all tiles are taken and neither player has won. In other words, you know that the game has ended in a tie. Here’s how you add logic to the end of aiTurn to handle this scenario:

userMessage.hidden = falseuserMessage.text = "Looks like it was a tie!"reset()

After the AI outsmarts the user, the human player will be itching to jump back into the game. You can oblige him or her by defining a reset function that will clear the board:

Get Learning Swift™ Programming 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.