Defending the AI’s Turf and Stealing Center Stage

The computer now knows how to take action when it’s one tile away from a win. In the five-step outline for the AI, the second priority was finding and blocking a human player’s winning tile; basically, the AI wants to defend its turf. The methods from step 1 are generic enough that you can reuse them to defend the AI’s turf in step 2. The only difference is that you have to pass the human player ID instead of the computer ID to rowCheck and setImageForSpot:

      // The player has two in a row      if let result = rowCheck(value: 1) {          var whereToPlayResult = whereToPlay(result[0], pattern: result[1])          if !isOccupied(whereToPlayResult) {              setImageForSpot(whereToPlayResult, ...

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.