Advertisement
GregLeck

Guess The Flag - Day 24 Challenge

Oct 17th, 2019
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.62 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct FlagImage: View {
  4.    
  5.     var someImage: String
  6.    
  7.     var body: some View {
  8.         Image(someImage)
  9.             .renderingMode(.original)
  10.             .clipShape(Capsule())
  11.             .overlay(Capsule().stroke(Color.black, lineWidth: 1))
  12.             .shadow(color: .black, radius: 2)    }
  13. }
  14.  
  15.  
  16.  
  17. struct ContentView: View {
  18.    
  19.     @State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()
  20.    
  21.     @State private var correctAnswer = Int.random(in: 0...2)
  22.    
  23.     @State private var showingScore = false
  24.     @State private var scoreTitle = ""
  25.    
  26.     @State private var score = 0
  27.    
  28.     var body: some View {
  29.        
  30.         ZStack {
  31.            
  32.             LinearGradient(gradient: Gradient(colors: [.blue, .black]), startPoint: .top, endPoint: .bottom)
  33.                 .edgesIgnoringSafeArea(.all)
  34.            
  35.             VStack(spacing: 30) {
  36.                
  37.                 VStack {
  38.                     Text("Tap the flag of")
  39.                         .foregroundColor(.white)
  40.                     Text(countries[correctAnswer])
  41.                         .foregroundColor(.white)
  42.                         .font(.largeTitle)
  43.                         .fontWeight(.black)
  44.                 }
  45.                
  46.                 ForEach(0..<3) { number in
  47.                     Button(action: {
  48.                         self.flagTapped(number)
  49.                     }) {
  50.                         FlagImage(someImage: self.countries[number])
  51.                     }
  52.                 }
  53.                
  54.                 Text("Your score is: \(score)")
  55.                     .font(.largeTitle)
  56.                     .foregroundColor(Color.white)
  57.                
  58.                 Spacer()
  59.             }
  60.         }
  61.         .alert(isPresented: $showingScore) {
  62.             Alert(title: Text(scoreTitle), message: Text("Your score is \(score)"), dismissButton: .default(Text("Continue")) {
  63.                 self.askQuestion()
  64.                 })
  65.         }
  66.        
  67.     }
  68.    
  69.     func flagTapped(_ number: Int) {
  70.         if number == correctAnswer {
  71.             scoreTitle = "Correct"
  72.             score += 1
  73.         } else {
  74.             scoreTitle = "Wrong, that's the flag of \(countries[number])"
  75.             score -= 1
  76.         }
  77.        
  78.         showingScore = true
  79.     }
  80.    
  81.     func askQuestion() {
  82.         countries.shuffle()
  83.         correctAnswer = Int.random(in: 0...2)
  84.     }
  85. }
  86.  
  87. struct ContentView_Previews: PreviewProvider {
  88.     static var previews: some View {
  89.         ContentView()
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement