fix: some adjustments for estimation questions

This commit is contained in:
2025-08-09 22:22:17 +02:00
parent a6dd53dbd9
commit a7907d59b9
4 changed files with 82 additions and 47 deletions

View File

@@ -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": ""
}
]
}

View File

@@ -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
}
}
}

View File

@@ -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
}
}
}

View File

@@ -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