feat: added score counter and end of quiz

This commit is contained in:
2025-08-05 21:00:13 +02:00
parent 7cacfc984b
commit 1ce4ca6241
2 changed files with 48 additions and 6 deletions

View File

@@ -13,10 +13,30 @@ struct ContentView: View {
@State private var selectedAnswerIndex: Int? = nil @State private var selectedAnswerIndex: Int? = nil
@State private var isAnswered = false @State private var isAnswered = false
var body: some View { var body: some View {
VStack { VStack {
if let frage = viewModel.currentQuestion { if viewModel.isQuizFinished {
VStack(spacing: 20) {
Text("🎉 Quiz beendet!")
.font(.title)
Text("Dein Ergebnis: \(viewModel.score) von \(viewModel.questions.count) Punkten")
.font(.headline)
Button("Nochmal spielen") {
restartQuiz()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
} else if let frage = viewModel.currentQuestion {
Text("Punkte: \(viewModel.score)")
.font(.headline)
.padding(.bottom, 10)
Text(frage.question) Text(frage.question)
.font(.title) .font(.title)
.multilineTextAlignment(.center) .multilineTextAlignment(.center)
@@ -28,15 +48,21 @@ struct ContentView: View {
isAnswered = true isAnswered = true
print("Selected:", index) print("Selected:", index)
print("Correct:", frage.correctAnswer) print("Correct:", frage.correctAnswer)
viewModel.incrementScore(selectedIndex: index)
}) { }) {
Text(frage.answers[index]) Text(frage.answers[index])
.padding() .padding(.vertical, 12)
.padding(.horizontal, 20)
.frame(maxWidth: 300) .frame(maxWidth: 300)
.background(buttonColor(for: index, correctIndex: frage.correctAnswer)) .background(buttonColor(for: index, correctIndex: frage.correctAnswer))
.foregroundColor(.white) .foregroundColor(.white)
.cornerRadius(10) .cornerRadius(10)
.scaleEffect(isAnswered && selectedAnswerIndex == index ? 1.05 : 1.0)
.shadow(radius: isAnswered && selectedAnswerIndex == index ? 5 : 2)
} }
.animation(.easeInOut(duration: 0.3), value: isAnswered)
.disabled(isAnswered) .disabled(isAnswered)
} }
VStack { VStack {
if isAnswered { if isAnswered {
@@ -61,7 +87,7 @@ struct ContentView: View {
} }
} }
} }
.frame(height: 80) .frame(height: 100)
} else { } else {
Text("Fragen werden geladen...") Text("Fragen werden geladen...")
} }
@@ -69,7 +95,7 @@ struct ContentView: View {
} }
private func buttonColor(for index: Int, correctIndex: Int) -> Color { private func buttonColor(for index: Int, correctIndex: Int) -> Color {
guard isAnswered else { return .blue } guard isAnswered else { return .blue }
if index == correctIndex { if index == correctIndex {
return .green return .green
} else if index == selectedAnswerIndex { } else if index == selectedAnswerIndex {
@@ -78,6 +104,14 @@ struct ContentView: View {
return .gray return .gray
} }
} }
private func restartQuiz() {
viewModel.currentQuestionIndex = 0
viewModel.score = 0
viewModel.isQuizFinished = false
selectedAnswerIndex = nil
isAnswered = false
}
} }
#Preview { #Preview {

View File

@@ -11,6 +11,8 @@ class ViewModel: ObservableObject {
@Published var questions: [QuizQuestion] = [] @Published var questions: [QuizQuestion] = []
@Published var currentQuestionIndex: Int = 0 @Published var currentQuestionIndex: Int = 0
@Published var score: Int = 0
@Published var isQuizFinished: Bool = false
var currentQuestion: QuizQuestion? { var currentQuestion: QuizQuestion? {
guard currentQuestionIndex < questions.count else { return nil } guard currentQuestionIndex < questions.count else { return nil }
@@ -36,7 +38,13 @@ class ViewModel: ObservableObject {
if currentQuestionIndex + 1 < questions.count { if currentQuestionIndex + 1 < questions.count {
currentQuestionIndex += 1 currentQuestionIndex += 1
} else { } else {
currentQuestionIndex = 0 // oder Quiz-Ende behandeln isQuizFinished = true
}
}
func incrementScore(selectedIndex: Int) {
if let correct = currentQuestion?.correctAnswer, selectedIndex == correct {
score += 1
} }
} }
} }