Displaying the Player’s Moves

Having defined the UIImageViews in code, you can dynamically display some images. The goal is to show an X image on tiles that the user taps and an O on tiles tapped by the AI. “Tapped” is a figure of speech here, of course, because an algorithm will decide which tiles the computer player picks. Don’t worry too much about AI for now. You’ll learn about that algorithm in the next section, after we finish laying the groundwork for UI events.

For now, you need to detect when a human player taps a tile, so use this code:

enum Player: Int {    case ComputerPlayer = 0, UserPlayer = 1}override func viewDidLoad() {    super.viewDidLoad()    ticTacImages = [ticTacImage1, ticTacImage2, ticTacImage3, ...

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.