Black Jack App, Excluding through a Boolean variable

Hi there :slight_smile:
I am trying to program a Black Jack app. There are three CPU players (with the dealer). I have written a separate class “Cpu” and in this class the cpus get their numbers (cards, I have no images yet) and also they become a true statement if they are <= 21 and false if above 21. I did this because I wanted to exclude the “false” players, so that I can neglect them when I am comparing the numbers to see who wins against who. As you can imagine there are dozens of different situations and making so many if else statement would be crazy.
What is the best solution for my problem?

Furthermore I get these yellow errors and I don’t know how to get them away.

Hey @Justin_Sams Welcome!

Since players are play against the house, you could switch on the house results and have the corresponding message based on the hand at each case. The default case could be a draw.

Your warnings, I think (so take with a grain of salt), is because you utilize the same variable name for both Cpu() and dealCards(). I am not sure of your structure, but you could change the name of one of the variable and I bet the warning will go away.

Happy coding.
Blessings,
—Mark

Hi Mark,
Thank you very much! I will try to make a switch statement.
I don’t get the warnings, but I will rewatch Chris’ class tutorial haha

Oh the warning I was referring to are the yellow caution warnings Xcode provides you inquired about. I got the same one today on a piece of code I was working on and found that I was not using the result of the variable in a call. Xcode was not lying to me! :slight_smile:

It is just a caution, you app should run OK, if just will not do anyting with the call to dealCards()

Blessings,
—Mark

yes the class works just fine. Although I still have the original problem. It would be great if you could give me some advice how to set up the switch
statement. I know the syntax but somehow I don’t get how to make one in my situation. :slight_smile:

Hi,

When I started thinking about a switch, I realized there rule be more conditions than a single switch my handle. Had not though about compound them though, that would be interesting to play around with in a Playground.

This is not a full solution, but it will get you going anyway. Still need to handle ties, double down, and blackjack, but this basic structure should at least get you going.

When trying a logic problem like this, I usually work toward fining a solution. When it works, like this does for basic payouts, then go back and see how you can streamline and add other functions.

Hope this helps.
Blessings,
—Mark

var dealer = 18
var player1 = 20
var player2 = 20
var player3 = 18

let players = [player1, player2, player3]

for player in players {
    
    if dealer >= 17 && dealer < 22 {
        
        if player >= dealer && player < 22 {
            print("Pay \(player)")
        }
    } else {
        if player < 22 {
            print("pay default \(player)")
        }
    }
}

Thank you very much mark! I will test and try it out right away.
All the best,
Justin

Hey Mark,
So I finished what you sent me and also it works very good with my class and everything is good, BUT…
it says -> print("(gambler) wins")
In reality I want to access the Label on the mainstory board.
In my case for example it should say tomScoreLabel.text = String(tom). How do I make this kind of access to work as a variable ?

Hi,

I glad to hear you are making headway!

I assume you want to send the results to a different viewController? If so, I have found saving the results of the current task to a Realm database and then reading that database to display the data had been very effective and easy. Chris has a spectacular tutorial about using Realm. I used to use Core Data, but it is much more complicated and requires a significant more amount of code to write to achieve the same end.

If you want to pass a single variable to a new view, the prepareForSegue can handle that for you.

There are a bunch of tutorials online to walk you thing it, this one seems pretty up to day and I have used some of their material in the past.
https://www.hackingwithswift.com/example-code/system/how-to-pass-data-between-two-view-controllers

Good luck. Let us know how it works out.
Blessings,
—Mark

Hi Mark,
So what I am trying to do is, I want to replace the print statements with labels that are actually visible on the main storyboard.
I am not sure if I need a “different” View Controller

Heya,

I assume you are in a single viewController that contains this code. If so, that is easy.

tomScoreLabel.text = "You can display any message you like"

Make sense?
Blessings,
—Mark

Hi :slight_smile: Yes I know I can change the label like this, but how would I integrate this in my project? Because at the moment “gambler” can take on tom, lena and player.
But with the Label the program doesn’t know which player actually wins and what I wrote here is of course completely wrong :confused:

Ah ha, I see what you are doing. Having multiple players like that really expands the logic and possibilities.

For version 1 of you app, you may want to focus on all the players playing against the house. Standard casino rules. This way the logic is much easier, test each player against the house and see if it is a push or win.

Later, you can update the app with the multi-player option. This way you can work from a solid foundation of logic against the house and then start to think about how to extend that.

I am currently working on a similar type of logic problem, although mine has to do with floating calendar dates and future dates. I have written hundreds of lines of code to solve what on the surface is a simple problem. What you start adding the “what if” scenarios, this is were it gets complex. My guess a multi player card games has some similarities. I have yet attempted to tackle such a project.

If a brainstorm hits, I will certainly be happy to share it.
Blessings,
—Mark

my players play against the house, but still I have to change the label for each time a player wins.
But I can’t make it happen that my players labels change in the if statement. see “gambler” takes the results from the array but the labels (tomActionLabel) do not realize when they win

On the top there is the view controller with all the links to the main storyboard. Class Cpu() is the first picture and gives results to dealer, lena, tom

When your final hand runs you can loop through you players, and first determine win or loss amid then grab the current score and add or subtract the hand. Then just update the score field. And save the data.

Are you maintaining the win/loss data in userData? If the app closes, does it all reset?

You might want to look at Chris’ most excellent course on Databases. I am a REAL fan of the Realm database. It is a great framework and really easy to use. It can keep track of the player scores, keep track of the single deck or shoe, as, for example, if it is a single deck game, the Ace of hearts should only appear once. It keeps track of the data, small you have to do is call upon it, update, and then present it. Many headaches go away!!

Blessings,
—Mark

Hey Justin, can you see if this change would help?
CloudApp

Basically instead of having the hands in the array, put in the player/cpu objects instead.
That way when you determine a winning hand, you have access to the object itself and hopefully you have a property in that CPU class for a name or some way to identify whether it’s Tom, Lena or the dealer. If you can identify which one of them it is, then you can update the corresponding label element too.

1 Like