[iOS]Building an app from scratch — SlapJack. Part 2

MYH
彼得潘的 Swift iOS / Flutter App 開發教室
4 min readJul 28, 2021

Sorry guys. I wanted to put all the things that I wanted to share in one article, but it exceeds the maximum length so I was forced to separate it into two parts.

Part 1 is right here.

Setting up the StoryBoard

Another thing that worth mentioning before we get to the hardest part is how I set up my storyboard.
As you can see, all the cards are made of UIViews which contain two labels representing Suit & Rank respectively.
Once the view loaded up, I want to see three cards on the table, one for me as the player, and two for the dealer with one covered. I did this by setting a variable called rounds. It will help me decide which Card View should show up and which not.
Here’s the code.

//I'm only going to show the player's version here, just to be concise.
private var playerRounds = 0
override func viewDidLoad() {super.viewDidLoad()//dealer draw a card to player and show the first carddrawCard() cardView[playerRounds].isHidden = false//hid other cardsfor i in (1...4) {cardView[i].isHidden = true}

The “ONE” Dilemma

Finally, the hardest part in this project, the “ONE” dilemma.

As we all know, “Ace” can be one or eleven in blackjack. However, it’s harder for the computer to decide which one should be given the circumstances. And I’m going to show you why is that.

(Here’s my code. I used getter and setter to decide the value of my cards so that I can do calculation later.)

Let’s say that the dealer draws a one out of the deck, a “1” got passed in to the value. Then, the dealer gives you a “J”, so now the total value is 11, but in truth, you already get a blackjack on your hands.
Another scenario. The dealer first give you a “4” and hit you with an “ace ”, making your total value a 5 or a 15. Then you ask the dealer to hit you again, and you get a “J”. Now we all know that we aren’t busted yet because the value would be 5+10 = 15 in this case. But how does the computer decide when to use 1 as one and 1 as eleven? The answer is that it can’t. At least not in this case, since I’m only storing the total value, after I passed the value of the card and added to the former value, the computer won’t be able to know whether we have one or not. Thus, we have to do some alternation beforehand.

This was my initial solution.

If I get a “1”, I would automatically check whether or not the “total value + 11” will exceed 21. If it does, the “1” would have the value of 1; if it doesn’t, the “1” would represent the value of 11.
While that sounds pretty decent, I found out that there will still be trouble in certain circumstances. For example, what happened if I get more than one “1”? Let’s say [1, 1, J] were the cards that you got. Based on our function, our number would be 22(11+1+10) where it really should be just 12. Hence, I needed a new function to help me identify our “1”. Let’s take a look.

I’m not sure how other people deal with this problem, but this was what I came up with.

What I did right here is that:
1. create a bool variable called “hasEleven” to identify whether or not there was a eleven in total value.
2. If the input number is 1, we will add 11 to the total value and change the bool hasEleven to true.

So now no matter how many 1 you get in your cards, this function will do you just fine.

Okay, that’s a wrap for me. I know I didn’t do my best elaborating my project, but I really hope you guys will like it. It took me quite a while to finish this one, and I really enjoy the thinking process. Oh yes, here’s the link of my file on Github. If you want to learn more about it, you are free to download it and have it a go.

--

--