Files
goSportpunktetafel/internal/logik/logik.go
T

89 lines
1.8 KiB
Go

// Package logik implements the scoring logic for the sports scoreboard.
package logik
import (
"fmt"
"strings"
)
// Team represents a team on the scoreboard.
type Team string
const (
TeamRed Team = "rot"
TeamGreen Team = "gruen"
)
// Round stores the score for both teams in a single round.
type Round struct {
Red int
Green int
}
// Scoreboard holds the complete game state.
type Scoreboard struct {
CurrentRound int
Rounds []Round
RedScore int
GreenScore int
}
// NewScoreboard creates a new scoreboard.
func NewScoreboard() *Scoreboard {
return &Scoreboard{
CurrentRound: 1,
Rounds: []Round{},
RedScore: 0,
GreenScore: 0,
}
}
// Score increases the score for the specified team in the current round.
func (sb *Scoreboard) Score(team Team) {
switch team {
case TeamRed:
sb.RedScore++
case TeamGreen:
sb.GreenScore++
}
}
// NextRound saves the current round and starts a new one (0:0).
func (sb *Scoreboard) NextRound() {
// Save current round
sb.Rounds = append(sb.Rounds, Round{
Red: sb.RedScore,
Green: sb.GreenScore,
})
// Reset for next round
sb.CurrentRound++
sb.RedScore = 0
sb.GreenScore = 0
}
// Reset clears all rounds and resets to round 1 (0:0).
func (sb *Scoreboard) Reset() {
sb.CurrentRound = 1
sb.Rounds = []Round{}
sb.RedScore = 0
sb.GreenScore = 0
}
// String returns a human-readable string representation.
func (sb *Scoreboard) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("Runde %d\n", sb.CurrentRound))
b.WriteString(fmt.Sprintf("%s: %d | %s: %d\n", TeamRed, sb.RedScore, TeamGreen, sb.GreenScore))
if len(sb.Rounds) > 0 {
b.WriteString("---\n")
for i, r := range sb.Rounds {
b.WriteString(fmt.Sprintf("Runde %d: %d:%d\n", i+1, r.Red, r.Green))
}
}
return b.String()
}