Taking Sides
You’re almost done creating an AI in Swift. If the computer player can’t seize a victory, block the opponent’s win, or take a center tile, it has only two options. It tries step 4, claiming a corner tile. If all corners are taken, it falls back to its last resort, marking a middle tile. Steps 4 and 5 are similar in terms of logic:
func firstAvailable(#isCorner:Bool) -> Int? { var spots = isCorner ? [0,2,6,8] : [1,3,5,7] for spot in spots { println("checking \(spot)") if !isOccupied(spot) { println("not occupied \(spot)") return spot } } return nil }
The firstAvailable
method loops through an array of tile ...
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.