Learn Courses My Dashboard

14 Day: Challenge 13 - Built using Dictionary and Methods

Hi Guys,

I went a little crazy with dictionaries and built out the challenge differently.

I wanted to see if anyone can give me some feedback and critique.

struct ContentView: View {
    // Properties
    @State private var credits: Int = 1025
    
    @State private var slotOptions = [1: "star", 2: "cherry", 3: "apple"]
    
    @State private var slotOne: String = "star"
    @State private var slotTwo: String = "cherry"
    @State private var slotThree: String = "apple"

    
    
    // View
    var body: some View {
        VStack {
            Spacer()
            Text("SwiftUI Slots!")
                .font(.largeTitle)
                .fontWeight(.bold)
                .multilineTextAlignment(.center)
                
            Spacer()
            Text("Credits: \(credits)")
                .font(.title2)
                .fontWeight(.bold)
            
            Spacer()
            
            HStack {
                
                Image("\(slotOne)")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding(5)
                Image("\(slotTwo)")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding(5)
                Image("\(slotThree)")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding(5)
                
            }
            .padding(30.0)
            
            Spacer()
            
            Button(action: {
                
                slotOne = slotOptions[spinTheSlots()] ?? "star"
                slotTwo = slotOptions[(spinTheSlots())] ?? "cherry"
                slotThree = slotOptions[(spinTheSlots())] ?? "apple"
                
                if slotOne == slotTwo && slotTwo == slotThree {
                    credits = credits + 100
                } else {
                    credits = credits - 20
                }
                
            }, label: {
                Text("SPIN BABY SPINT")
                    .fontWeight(.bold)
                    
            })
                .padding(.vertical, 10)
                .padding(.horizontal, 50)
                .background(Color.pink)
                .cornerRadius(100)
                .foregroundColor(Color.white)
            Spacer()
    
        }
        
    }
    
    
    
    // Methods
    private func spinTheSlots() -> Int {
        return Int.random(in: 1...3)
    }
    

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thanks.