diff --git a/QuizApp/Resources/questions.json b/QuizApp/Resources/questions.json index fc6f5a6..60dc4d9 100644 --- a/QuizApp/Resources/questions.json +++ b/QuizApp/Resources/questions.json @@ -184,21 +184,61 @@ } ], "Schätzen": [ - { - "type": "estimation", - "question": "Wie hoch ist der Eiffelturm?", - "minValue": 0, - "maxValue": 1000, - "correctValue": 330, - "unit": "m" - }, - { - "type": "estimation", - "question": "In welchem Jahr begann der Bau des Kölner Doms?", - "minValue": 1000, - "maxValue": 2025, - "correctValue": 1248, - "unit": "Jahr" - } + { + "type": "estimation", + "question": "Wie hoch ist der Eiffelturm?", + "minValue": 100, + "maxValue": 350, + "correctValue": 330, + "unit": "m" + }, + { + "type": "estimation", + "question": "In welchem Jahr begann der Bau des Kölner Doms?", + "minValue": 1000, + "maxValue": 1900, + "correctValue": 1248, + "unit": "Jahr" + }, + { + "type": "estimation", + "question": "Wie weit ist die Entfernung zwischen Berlin und New York (Luftlinie)?", + "minValue": 5000, + "maxValue": 10000, + "correctValue": 6380, + "unit": "km" + }, + { + "type": "estimation", + "question": "Wie hoch ist der Mount Everest?", + "minValue": 2000, + "maxValue": 10000, + "correctValue": 8848, + "unit": "m" + }, + { + "type": "estimation", + "question": "Wie viele Kilometer beträgt der Äquatorumfang der Erde?", + "minValue": 20000, + "maxValue": 50000, + "correctValue": 40075, + "unit": "km" + }, + { + "type": "estimation", + "question": "Wie tief ist der Marianengraben?", + "minValue": 1000, + "maxValue": 20000, + "correctValue": 11000, + "unit": "m" + }, + { + "type": "estimation", + "question": "In welchem Jahr wurde die Glühbirne von Thomas Edison patentiert?", + "minValue": 1700, + "maxValue": 2000, + "correctValue": 1879, + "unit": "" + } ] } diff --git a/QuizApp/ViewModels/ViewModel.swift b/QuizApp/ViewModels/ViewModel.swift index 37b8403..3c4340d 100644 --- a/QuizApp/ViewModels/ViewModel.swift +++ b/QuizApp/ViewModels/ViewModel.swift @@ -79,20 +79,16 @@ class ViewModel: ObservableObject { func pointsForEstimation( guess: Double, correct: Double, - minValue: Double, - maxValue: Double, - maxPoints: Int = 3, - thresholdPercent: Double = 0.3 + maxPoints: Int = 1, + ) -> Int { - let span = maxValue - minValue - guard span > 0 else { return 0 } + let absError = abs(guess - correct) // Absolute Abweichung + let relError = absError / abs(correct) // Relative Abweichung - let absError = abs(guess - correct) - let relError = absError / span - - if relError >= thresholdPercent { return 0 } - - let ratio = 1.0 - (relError / thresholdPercent) - return max(0, Int(round(Double(maxPoints) * ratio))) + if relError <= 0.1 { + return maxPoints + } else { + return 0 + } } } diff --git a/QuizApp/Views/EstimationQuestionView.swift b/QuizApp/Views/EstimationQuestionView.swift index 088a7bf..8ea96d0 100644 --- a/QuizApp/Views/EstimationQuestionView.swift +++ b/QuizApp/Views/EstimationQuestionView.swift @@ -7,33 +7,27 @@ import SwiftUI struct EstimationQuestionView: View { + let questionID: AnyHashable // <- neu let minValue: Double let maxValue: Double let correctValue: Double let unit: String let viewModel: ViewModel - + @Binding var value: Double @Binding var submitted: Bool @Binding var gainedPoints: Int let onSubmit: (Int) -> Void - + var body: some View { VStack(spacing: 16) { Slider( - value: Binding( - get: { value }, - set: { value = min(max($0, minValue), maxValue) } // clamp - ), + value: $value, in: minValue...maxValue, step: (maxValue - minValue) > 200 ? 1 : 0.5 ) - .onAppear { - if value < minValue || value > maxValue { - value = (minValue + maxValue) / 2 - } - } - + .disabled(submitted) + HStack { Text("\(Int(minValue)) \(unit)") Spacer() @@ -42,14 +36,12 @@ struct EstimationQuestionView: View { Text("\(Int(maxValue)) \(unit)") } .font(.caption) - + if !submitted { Button { gainedPoints = viewModel.pointsForEstimation( guess: value, - correct: correctValue, - minValue: minValue, - maxValue: maxValue + correct: correctValue ) submitted = true onSubmit(gainedPoints) @@ -66,7 +58,7 @@ struct EstimationQuestionView: View { let diff = abs(value - correctValue) let span = maxValue - minValue let rel = span > 0 ? (diff / span) : 1.0 - + Text("✅ Richtiger Wert: \(Int(correctValue)) \(unit)") .font(.headline) .foregroundColor(.green) @@ -78,5 +70,12 @@ struct EstimationQuestionView: View { } } .padding(.horizontal) + // bei jedem Fragewechsel + .task(id: questionID) { + value = minValue + submitted = false + gainedPoints = 0 + } } } + diff --git a/QuizApp/Views/QuizView.swift b/QuizApp/Views/QuizView.swift index b54d450..eae5043 100644 --- a/QuizApp/Views/QuizView.swift +++ b/QuizApp/Views/QuizView.swift @@ -67,6 +67,7 @@ struct QuizView: View { let correctV = frage.correctValue { // Estimation EstimationQuestionView( + questionID: viewModel.currentQuestionIndex, // <- neu minValue: minV, maxValue: maxV, correctValue: correctV, @@ -85,7 +86,6 @@ struct QuizView: View { if estimationSubmitted { Button { viewModel.loadNextQuestion() - // Reset für nächste Estimation estimationSubmitted = false isAnswered = false selectedAnswerIndex = nil