Implementing the AI Logic
Now that you’ve added logic for detecting when a player wins or loses, you can make the computer player strive to achieve a victory. This concerns steps 1 and 2 of the algorithm outlined earlier: The AI should move in for the kill, so to speak, if it has two tiles in a row, and it should block the human player if the human has two tiles in a row. To start processing the AI’s turn in the game, you call a new method named aiTurn
. You place the new method calls at the bottom of the imageClicked
function:
func imageClicked(reco: UITapGestureRecognizer) { var imageViewTapped = reco.view as UIImageView if plays[imageViewTapped.tag] == nil && !aiDeciding && !done { setImageForSpot(imageViewTapped.tag, ...
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.