UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

GitHub

https://github.com/tutsplus/create-a-blackjack-game-in-swift-and-spritekit/tree/master/blackjack

Thanks. joe.

2      

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

2      

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 .

Thanks.

Joe

2      

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))

3      

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

4      

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.

Joe

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.