Advertisement
shlomibergic

blindgame

Nov 20th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. contentview.swiftui
  2. **********************
  3. import SwiftUI
  4.  
  5. struct ContentView: View {
  6. //create an rgb color random values
  7. let rTarget = Double.random(in: 0..<1)
  8. let gTarget = Double.random(in: 0..<1)
  9. let bTarget = Double.random(in: 0..<1)
  10.  
  11. //show the state of show alert (if we need to present it or hide it)
  12. @State var showAlert=false
  13. //the user guess numbers
  14. @State var rGuess:Double
  15. @State var gGuess:Double
  16. @State var bGuess:Double
  17.  
  18.  
  19. func computeScore()->Int{
  20. let rDiff = rGuess - rTarget
  21. let gDiff = gGuess - gTarget
  22. let bDiff = bGuess - bTarget
  23. //sqrt(rdiff^2 + gDiff^2 + bDiff^2)
  24. let diff = sqrt(rDiff * rDiff + gDiff*gDiff + bDiff*bDiff)
  25. return Int((1.0-diff)*100+0.5)
  26. }
  27.  
  28. var body: some View {
  29. VStack{
  30. HStack{
  31. //target color block
  32. VStack{
  33. Rectangle()
  34. .foregroundColor(Color(red: rTarget, green: gTarget, blue: bTarget, opacity: 1.0))
  35. Text("Match this color")
  36. }
  37. //guess color block
  38. VStack{
  39. Rectangle()
  40. .foregroundColor(Color(red: rGuess, green: gGuess, blue: bGuess, opacity: 1.0))
  41. HStack{
  42. Text("R: \(Int(rGuess*255.0))")
  43. Text("G: \(Int(gGuess*255.0))")
  44. Text("B: \(Int(bGuess*255.0))")
  45. }
  46. }
  47. }
  48. Button(action: {
  49. //what to do when we hit the button
  50. self.showAlert = true
  51. }){
  52. Text ("Hit me")
  53. }
  54. .alert(isPresented: $showAlert){
  55. Alert(title: Text("Your score"), message: Text("\(computeScore())"))
  56. }
  57. VStack{
  58. ColorSlider(value: $rGuess , textColor: .red)
  59. ColorSlider(value: $gGuess, textColor: .green)
  60. ColorSlider(value: $bGuess, textColor: .blue)
  61. }
  62. }
  63. }
  64. }
  65.  
  66. struct ColorSlider : View {
  67. @Binding var value:Double
  68. var textColor: Color
  69. var body: some View{
  70. HStack{
  71. Text("0")
  72. .foregroundColor(textColor)
  73. Slider(value: $value)
  74. Text("255")
  75. .foregroundColor(textColor)
  76. }
  77. .padding()
  78. }
  79. }
  80.  
  81. struct ContentView_Previews: PreviewProvider {
  82. static var previews: some View {
  83. ContentView(rGuess: 0.5, gGuess: 0.5, bGuess: 0.5)
  84. }
  85. }
  86.  
  87. sceneDelegate.swift
  88. *******************************************
  89.  
  90. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  91. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  92. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  93. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  94.  
  95. // Create the SwiftUI view that provides the window contents.
  96. let contentView = ContentView(rGuess: 0.5, gGuess: 0.5, bGuess: 0.5)
  97.  
  98. // Use a UIHostingController as window root view controller.
  99. if let windowScene = scene as? UIWindowScene {
  100. let window = UIWindow(windowScene: windowScene)
  101. window.rootViewController = UIHostingController(rootView: contentView)
  102. self.window = window
  103. window.makeKeyAndVisible()
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement