O'Reilly logo

Learning Swift™ Programming by Jacob Schatz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required