WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.