Slots game - Avoid debt (negative credits)

Hey Code Crew, it’s almost midnight for me but I’ve been excited for all the learning that I wanted to share with you an additional feature for the Slots App that Chris showed us.

The following is to avoid having credits < 0 and not allowing you to spin if you don’t have enough credits to pay, i.e. if you have 20 credits it will be allow to spin for the 5 credits button but no the one that costs 25.

You can see my code in here (it’s only the button section that triggers the alert message, and allows or not to spin):

Also at the beginning of the code declare the variable:
@State private var showingAlert = false

// Buttons

            HStack {
                Spacer()
                VStack {
                    Button (action: {
                        
                        self.showingAlert = false
                        
                        if self.credits < 5 {
                            
                            self.showingAlert = true
                        }
                        else {
                        // Process a single spin
                            self.processResults()
                        }
                        }) {
                            
                        Text("Spin")
                            .bold()
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .padding(.horizontal, 30)
                            .background(Color(red: 100/255, green: 50/255, blue: 80/255))
                            .cornerRadius(20)
                            
                        }.alert(isPresented: $showingAlert) {
                            Alert(title: Text("Game Over"), message: Text("No more credits to play"), dismissButton: .default(Text("Got it")))}
                        
                    Text("\(betCost) credits").padding(.top, 10).font(.callout)
                }
                Spacer()
                VStack {
                    Button (action: {
                        
                         if self.credits <= 24 {
                            self.showingAlert = true
                         }
                         else {
                            // Process a single spin
                            self.processResults(true)
                         }
                    }) {
                        Text("Max Spin")
                            .bold()
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .padding(.horizontal, 30)
                            .background(Color(red: 150/255, green: 50/255, blue: 80/255))
                            .cornerRadius(20)
                    }.alert(isPresented: $showingAlert) {
                    Alert(title: Text("Game Over"), message: Text("No more credits to play"), dismissButton: .default(Text("Got it")))}
                    Text("\(betCost * 5) credits").padding(.top, 10).font(.callout)
                }
                Spacer()
            }