NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: Stuck in blackjack tutorial.

Forums > Swift

I have worked through a blackjack tutorial and its almost complete. The app works fine both on simulator and phone but I cannot get the money won or lost in the bet to add or deduct from the players bank.

The methods for this are located in the bank.swift and pot.swift classes. In Game.scene swift the code to add to the player bank will go into line 212 and subtract from the player bank in line 216. Any help or advise would be really appreciated.

The link to the code and classes on Github is below.


Thanks. joe.


If I understood correctly, bank is accessible via player1 and player2. So you'd need to do, for instance.


Hi, hope things are all well at your side.

Thanks for the response really appreciate it. Completely new to coding to be honest and this one was probably punching above my weight for the stage I am at but I am getting there(almost). I was missing "addChild" for the bank funds also but managed to get this in the view.

On your solution I had to drop the player1 but it is now adding funds to the bank.

The last issue, fingers crossed is how to I get the amount in the pot x 1.5 to be added to the player bank. At the moment i have

bank.addMoney(amount: 100)

Everytime the player wins 100 is added to the bank but when I try to change 100 to amount*1.5 (where amount = amount bet in Pot class) I get a"not a prefix unary operator" error.

This is probably a very basic syntax error to return an integer in the (amount: 100 ) without using a number to point to the value to .




Sorry for the delay. Hopefully you found out how to do it by now. But ir not, here's my suggestion: bank.addMoney(amount: Int(Double(pot.getMoney()) * 1.5))


You are trying to times a Int and Double .

You could do as @guseulalio says or change amount to double when declared then you be able to amount*1.5. when you display you can "£\(currentAmount, specifier: "%.2f" that will show to 2 decimal place.

Just other option


Hi, thanks a lot @guseulalio that worked perfectly. I have almost poured coffee over the laptop with frustration . I tried a few similar things but I think I was missing some of the parenthisis which was stopping it working. Very much appreciated.



Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.