Guest User

Untitled

a guest
Nov 8th, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.27 KB | Source Code | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State private var num1 = Int()
  5.     @State private var num2 = Int()
  6.     @State private var difficulty = 2
  7.     @State private var selectedNumberOfQuestions = 5
  8.     @State private var questionNumber = 0
  9.     @State private var numbers = [1...12]
  10.     @State private var correctAnswer = Int()
  11.     @State private var score = 0
  12.     @State private var randomAnswers = [Int]()
  13.     @State private var isVisible = false
  14.     @State private var endGame = false
  15.     let numberOfQuestions = [5, 10, 20]
  16.    
  17.    
  18.     var body: some View {
  19.         NavigationView {
  20.             VStack {
  21.                 VStack {
  22.                     Stepper("Select difficulty: \(difficulty)", value: $difficulty, in: 2...12)
  23.                         .padding()
  24.                        
  25.                    
  26.                     HStack {
  27.                         Text("Number of questions")
  28.                         Picker("", selection: $selectedNumberOfQuestions) {
  29.                             ForEach(numberOfQuestions, id: \.self) {
  30.                                 Text("\($0)")
  31.                             }
  32.                         }
  33.                         .pickerStyle(.segmented)
  34.                     }
  35.                     .padding()
  36.                     Divider()
  37.                 }
  38.                
  39.                 Text("Question number: \(questionNumber)")
  40.                 Spacer()
  41.                
  42.                 Text("What is: \(num1) x \(num2)")
  43.                     .padding()
  44.                
  45.                 HStack {
  46.                     ForEach(randomAnswers, id: \.self){ num in
  47.                         Button {
  48.                             withAnimation{
  49.                                 numberTapped(num)
  50.                             }
  51.                            
  52.                         } label: {
  53.                             Text("\(num)")
  54.                                 .frame(width: 50, height: 50)
  55.                                 .foregroundColor(.white)
  56.                                 .font(.title2.bold())
  57.                                 .background(.indigo)
  58.                                 .clipShape(Circle())
  59.                         }
  60.                         .padding(.vertical, 8.0)
  61.                         .alert("Game Over", isPresented: $endGame) {
  62.                             Button("Ok", action: newGame)
  63.                         } message: {
  64.                             Text("Your score is \(score)")
  65.                         }
  66.                     }
  67.                 }
  68.                 Spacer()
  69.                
  70.                 if !isVisible {
  71.                     Button {
  72.                         answer()
  73.                         isVisible.toggle()
  74.                     } label: {
  75.                         Text("Start Game")
  76.                     }
  77.                     .padding()
  78.                 }
  79.                 Text("Score: \(score)")
  80.             }
  81.             .navigationTitle("Math Game")
  82.         }
  83.     }
  84.            
  85.            
  86.             func answer() {
  87.                 for _ in 1...4 {
  88.                     num1 = Int.random(in: 1...difficulty)
  89.                     num2 = Int.random(in: 1...difficulty)
  90.                    
  91.                     correctAnswer = num1 * num2
  92.                     randomAnswers.append(correctAnswer)
  93.                 }
  94.                 createRandomAnswer()
  95.             }
  96.            
  97.             func createRandomAnswer() {
  98.                 randomAnswers.shuffle()
  99.             }
  100.            
  101.             func numberTapped(_ num: Int) {
  102.                 if num == correctAnswer {
  103.                     score += 1
  104.                     questionNumber += 1
  105.                     randomAnswers.removeAll()
  106.                     answer()
  107.                 } else {
  108.                     randomAnswers.removeAll()
  109.                     questionNumber += 1
  110.                     answer()
  111.                 }
  112.                 if questionNumber == selectedNumberOfQuestions {
  113.                     endGame = true
  114.                 }
  115.             }
  116.    
  117.     func newGame() {
  118.         score = 0
  119.         questionNumber = 0
  120.         randomAnswers.removeAll()
  121.         isVisible.toggle()
  122.     }
  123. }
  124.  
  125.  
  126. struct ContentView_Previews: PreviewProvider {
  127.     static var previews: some View {
  128.         ContentView()
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment