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

Day 35: Complete

Forums > 100 Days of SwiftUI

Hello everyone!

First off all, I'm so sorry for my English. After 8 hours, I was able to do it. I even created 4 buttons. Three with wrong answers and one with good answer. I know it's nothing, but I'm glad I was able to do it myself, without any help

import SwiftUI

struct ContentView: View {

    @State private var num1 = Int()
    @State private var num2 = Int()
    @State private var correctAnswer = Int()
    @State private var randomAnswers = [Int]()
    @State private var difficulty = 2
    @State private var score = Int()

    @State private var questionNumber = Int()
    @State private var selectedNumberOfQuestions = 5
    let numberOfQuestions = [5, 10, 20]

    @State private var isVisible = false
    @State private var endGame = false

    var body: some View {
         VStack {
               VStack {
                Stepper("Select Difficulty:  \(difficulty)", value: $difficulty, in: 2...12)
                    .padding()
                    .shadow(radius: 10)

                HStack {
                    Text("Number of Questions: ")
                    Picker("Number of Question", selection: $selectedNumberOfQuestions) {
                        ForEach(numberOfQuestions, id: \.self) {
                            Text("\($0)")
                        }
                    }
                    .pickerStyle(.segmented)
                }
                .padding()

                Divider()
                }

                Text("Question number: \(questionNumber)")
                Spacer()

                Text("What is \(num1) x \(num2) = ? ")
                    .padding()

                HStack {
                    ForEach(randomAnswers, id:\.self) { num in
                        Button {
                            withAnimation{
                            numberTapped(num)
                            }

                        } label: {
                            Text("\(num)")
                                .frame(width: 50, height: 50)
                                .foregroundColor(.white)
                                .font(.title2.bold())
                                .background(.indigo)
                                .clipShape(Circle())
                        }
                        .padding(.vertical, 8.0)
                        .alert("It's GameOVER", isPresented: $endGame) {
                            Button("Ok", action: newGame)
                        } message: {
                            Text("Your score is \(score)")
                        }

                    }
                }
                Spacer()
                if !isVisible {
                    Button {
                        answer()
                        isVisible.toggle()
                    } label: {
                        Text("Start Game")
                            .foregroundColor(.indigo)

                    }
                    .padding()
                }

                Text("Your score is \(score)")
            }
    }

    func answer() {
        for _ in 1...4 {
        num1 = Int.random(in: 1...difficulty)
        num2 = Int.random(in: 1...difficulty)

        correctAnswer = num1 * num2
        randomAnswers.append(correctAnswer)
        }
        createRandomAnswers()

    }

    func createRandomAnswers() {
        randomAnswers.shuffle()
    }

    func numberTapped(_ num: Int){
        if num == correctAnswer {
            score += 1
            questionNumber += 1
            randomAnswers.removeAll()
            answer()
        } else {
            randomAnswers.removeAll()
            questionNumber += 1
            answer()
        }
        if selectedNumberOfQuestions == questionNumber {
            endGame = true
        }
    }

    func newGame() {
        score = 0
        questionNumber = 0
        randomAnswers.removeAll()
        isVisible.toggle()
    }

}

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

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.