Finding Victory Conditions

The if statements in aiTurn are where the bulk of the logic takes place. They determine whether either player has two tiles in a row. While the steps for each check are basically the same, we’ll focus on the first one: the check to see whether a computer player is one tile away from a win. To answer this question, you can build a method called rowCheck and a series of short, related functions:

  func rowCheck(#value:Int) -> [String]?{      var acceptableFinds = ["011","110","101"]      var findFuncs = [checkTop,checkBottom,checkLeft,checkRight,          checkMiddleAcross,checkMiddleDown,          checkDiagLeftRight,checkDiagRightLeft]      var algorithmResults = findFuncs[0](value: value) ...

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.