Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 1: Getting Started

04. Challenge: Create a Blackjack Dealer

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Subscriber Operators and Subjects Next episode: 05. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Time to put your new combined skills to the test and make a handy Blackjack card dealer while you're at it. In case you're not familiar with it, Blackjack is a card game where the goal is to get 21 or as close as possible without going over, which is called getting busted. If that's not the biggest over-simplification of the game of Blackjack, I don't know what it is, but it's enough for our purposes. The starter playground for this challenge implements a pass through subject to model a hand of cards. In the sources folder for this playground, there is a support code file. It contains code to create an array of tuples to represent a standard deck of cards, including the emoji character for each card and its point value. Aces are high, so an ace of spades has a value of 11 compared to a queen of hearts, which has a value of 10. There are also two type aliases, card and hand to model, well, a card and a hand of cards. An extension on hand provides two helper computed properties. Card string and points. Card string will join together a hand of cards using their emoji characters, and points will return the sum of a hands cards points. Finally, there's an enum to model a hand error. In this case, there's only one error, busted. Hand error conforms to custom string convertible and returns the string busted, when you print the busted error. Back in the main playground page, there's also a deal function that will create a hand of the number of cards that you pass in for the card count parameter. Your first challenge is to add code immediately below the comment slash slash add code to update dealt hand here. That evaluates the result return from the hands points property. If the result is greater than 21, send the hand error dot busted through the dealt hand subject. Otherwise, send the hand value. Next, add code immediately below the comment, add subscription to dealt hand here to subscribe to dealt hand and handle receiving both values and an error. For receive values, print a string containing the results of the hands card string and points properties. For an error, just print it out. A tip, though. You can receive either a dot finished or a dot failure in the received completion block, so you'll want to distinguish whether that completion is a failure or not. The call to deal currently passes three. So three cards are dealt each time you run the playground. In a real game of Blackjack, you're initially dealt two cards, and then you have to decide to take one or more additional cards called hits until you either hit 21 or bust. For this simple example, you're just getting three cards straight away. See how many times you go bust versus how times you stay in the game. Are the odds stacked up against you in Vegas or what? The card emoji characters are pretty small when printed in the console. So you can temporarily increase the font size of the executable console output for this challenge if you want to be able to see them better. To do so, select x code, preferences, themes, console, then select executable console output and click the t button in the bottom right to change it to a larger font such as 48. So for task one, it will go something like this. If points is greater than 21, send busted, otherwise, send the hand. And for task two, you'll subscribe to dealt hand and handle receive values and a failure event that contain an error. All right, that's it. So pause the video now, and good luck. (upbeat music) Okay, here's what I came up with. For the first task, if I used an if else statement and if the result of hand at points is greater than 21, I send the busted error and a failure completion event. Otherwise, the hand did not bust and is good, so send it on dealt hand. For the second task, I subscribe to dealt hand, and in the receive value handler, I printed the hands card string and points values. And if an error occurred, in other words, the hand busted, I just printed out the error.