You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
// Package valueobjects provides domain value objects for the KnowFoolery application.
|
|
package valueobjects
|
|
|
|
// Scoring constants
|
|
const (
|
|
// MaxScorePerQuestion is the maximum score for a correct answer without hint.
|
|
MaxScorePerQuestion = 2
|
|
// ScoreWithHint is the score for a correct answer with hint.
|
|
ScoreWithHint = 1
|
|
// ScoreIncorrect is the score for an incorrect answer.
|
|
ScoreIncorrect = 0
|
|
// MaxAttempts is the maximum number of attempts per question.
|
|
MaxAttempts = 3
|
|
)
|
|
|
|
// Score represents a game score value.
|
|
type Score struct {
|
|
value int
|
|
}
|
|
|
|
// NewScore creates a new Score with validation.
|
|
func NewScore(value int) Score {
|
|
if value < 0 {
|
|
value = 0
|
|
}
|
|
return Score{value: value}
|
|
}
|
|
|
|
// Zero returns a zero score.
|
|
func Zero() Score {
|
|
return Score{value: 0}
|
|
}
|
|
|
|
// Value returns the score value.
|
|
func (s Score) Value() int {
|
|
return s.value
|
|
}
|
|
|
|
// Add adds points to the score and returns a new Score.
|
|
func (s Score) Add(points int) Score {
|
|
newValue := s.value + points
|
|
if newValue < 0 {
|
|
newValue = 0
|
|
}
|
|
return Score{value: newValue}
|
|
}
|
|
|
|
// CalculateQuestionScore calculates the score for a question based on correctness and hint usage.
|
|
func CalculateQuestionScore(isCorrect bool, usedHint bool) int {
|
|
if !isCorrect {
|
|
return ScoreIncorrect
|
|
}
|
|
if usedHint {
|
|
return ScoreWithHint
|
|
}
|
|
return MaxScorePerQuestion
|
|
}
|
|
|
|
// Attempt represents an attempt at answering a question.
|
|
type Attempt struct {
|
|
Number int
|
|
Answer string
|
|
Correct bool
|
|
UsedHint bool
|
|
Score int
|
|
}
|
|
|
|
// NewAttempt creates a new Attempt.
|
|
func NewAttempt(number int, answer string, correct bool, usedHint bool) Attempt {
|
|
return Attempt{
|
|
Number: number,
|
|
Answer: answer,
|
|
Correct: correct,
|
|
UsedHint: usedHint,
|
|
Score: CalculateQuestionScore(correct, usedHint),
|
|
}
|
|
}
|
|
|
|
// CanRetry checks if another attempt is allowed.
|
|
func CanRetry(attemptNumber int) bool {
|
|
return attemptNumber < MaxAttempts
|
|
}
|
|
|
|
// RemainingAttempts returns the number of remaining attempts.
|
|
func RemainingAttempts(attemptNumber int) int {
|
|
remaining := MaxAttempts - attemptNumber
|
|
if remaining < 0 {
|
|
return 0
|
|
}
|
|
return remaining
|
|
}
|