NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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 Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.