I don't know about you, but I certainly enjoyed this tutorial – not only does it involve some of the most impressive iOS features, but it's always fun to watch an artificial intelligence "think" its way through a problem and come to a solution. Plus, I got the chance to sneak in more
UIStackView action, which is always a good thing!
If you're looking to extend this project, the first thing you're likely to target is the heuristic function. As I said at the beginning, this project is based on some less-than-perfect Apple sample code, which I went on to rewrite in Swift, then refactor to make it easier to understand. The heuristic code is what makes the AI smart, but it doesn't take into account how many moves it takes for a win to happen, and so it performs fairly poorly.
As for other improvements, you've seen how this game could work in one- or two-player modes, so you could easily add a user interface to let the player select what kind of game they wanted. Then, by adjusting the level of look ahead, you could implement Easy, Medium and Hard computer opponents.
For a much easier improvement to make, you could switch out our
UIView chips for
UIImageViews, then draw your own red and black chip graphics. There isn't much coding required to make this happen, but let's face it: you've just written a mountain of code, so you probably deserve a break!
Anyway, that's it for this project. Once again you've made a useful, real-world project that is now your own to extend in whichever direction you want. As a heavy user of iOS apps, I'm particularly looking forward to seeing how apps (not games!) will use AI – can it recommend songs with some real intelligence, for example? Have fun!
You finished another project, and I'm glad Hacking with Swift helped you. Now I need your help. Please take just a moment out of your day to tell others about Hacking with Swift so they can benefit too.
You can click below to post a tweet straight to this project. Or if you're feeling particularly generous, you can click here to link to Hacking with Swift on your website and help spread the word.
Thank you. Your support is what keeps me going!
Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and Mario Kart world champion. OK, so that last part isn't true. If you're curious you can learn more here.
Get six books for only $150
The Swift Power Pack includes my first six books for one low price, helping you jumpstart a new career in iOS development – check it out!