diff --git a/internal/logik/logik.go b/internal/logik/logik.go new file mode 100644 index 0000000..d0f06d5 --- /dev/null +++ b/internal/logik/logik.go @@ -0,0 +1,88 @@ +// 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() +} diff --git a/internal/logik/logik_test.go b/internal/logik/logik_test.go new file mode 100644 index 0000000..2ef5ee0 --- /dev/null +++ b/internal/logik/logik_test.go @@ -0,0 +1,162 @@ +package logik + +import "testing" + +func TestNewScoreboard(t *testing.T) { + sb := NewScoreboard() + + if sb.CurrentRound != 1 { + t.Errorf("NewScoreboard() CurrentRound = %d, want 1", sb.CurrentRound) + } + if sb.RedScore != 0 { + t.Errorf("NewScoreboard() RedScore = %d, want 0", sb.RedScore) + } + if sb.GreenScore != 0 { + t.Errorf("NewScoreboard() GreenScore = %d, want 0", sb.GreenScore) + } + if len(sb.Rounds) != 0 { + t.Errorf("NewScoreboard() len(Rounds) = %d, want 0", len(sb.Rounds)) + } +} + +func TestScore_Red(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + + if sb.RedScore != 1 { + t.Errorf("Score(TeamRed) RedScore = %d, want 1", sb.RedScore) + } + if sb.GreenScore != 0 { + t.Errorf("Score(TeamRed) GreenScore = %d, want 0", sb.GreenScore) + } +} + +func TestScore_Green(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamGreen) + + if sb.GreenScore != 1 { + t.Errorf("Score(TeamGreen) GreenScore = %d, want 1", sb.GreenScore) + } + if sb.RedScore != 0 { + t.Errorf("Score(TeamGreen) RedScore = %d, want 0", sb.RedScore) + } +} + +func TestScore_Multiple(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Score(TeamGreen) + + if sb.RedScore != 3 { + t.Errorf("Score() RedScore = %d, want 3", sb.RedScore) + } + if sb.GreenScore != 1 { + t.Errorf("Score() GreenScore = %d, want 1", sb.GreenScore) + } +} + +func TestNextRound(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Score(TeamGreen) + sb.NextRound() + + if sb.CurrentRound != 2 { + t.Errorf("NextRound() CurrentRound = %d, want 2", sb.CurrentRound) + } + if sb.RedScore != 0 { + t.Errorf("NextRound() RedScore = %d, want 0", sb.RedScore) + } + if sb.GreenScore != 0 { + t.Errorf("NextRound() GreenScore = %d, want 0", sb.GreenScore) + } + if len(sb.Rounds) != 1 { + t.Errorf("NextRound() len(Rounds) = %d, want 1", len(sb.Rounds)) + } + if sb.Rounds[0].Red != 2 { + t.Errorf("NextRound() Rounds[0].Red = %d, want 2", sb.Rounds[0].Red) + } + if sb.Rounds[0].Green != 1 { + t.Errorf("NextRound() Rounds[0].Green = %d, want 1", sb.Rounds[0].Green) + } +} + +func TestNextRound_Multiple(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.NextRound() + + sb.Score(TeamGreen) + sb.Score(TeamGreen) + sb.Score(TeamGreen) + sb.Score(TeamGreen) + sb.NextRound() + + if sb.CurrentRound != 3 { + t.Errorf("NextRound() CurrentRound = %d, want 2", sb.CurrentRound) + } + if len(sb.Rounds) != 2 { + t.Errorf("NextRound() len(Rounds) = %d, want 2", len(sb.Rounds)) + } + if sb.Rounds[1].Green != 4 { + t.Errorf("NextRound() Rounds[1].Green = %d, want 4", sb.Rounds[1].Green) + } +} + +func TestReset(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.Score(TeamGreen) + sb.Score(TeamGreen) + sb.NextRound() + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Reset() + + if sb.CurrentRound != 1 { + t.Errorf("Reset() CurrentRound = %d, want 1", sb.CurrentRound) + } + if sb.RedScore != 0 { + t.Errorf("Reset() RedScore = %d, want 0", sb.RedScore) + } + if sb.GreenScore != 0 { + t.Errorf("Reset() GreenScore = %d, want 0", sb.GreenScore) + } + if len(sb.Rounds) != 0 { + t.Errorf("Reset() len(Rounds) = %d, want 0", len(sb.Rounds)) + } +} + +func TestString(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.Score(TeamRed) + sb.Score(TeamGreen) + output := sb.String() + + expected := "Runde 1\nrot: 2 | gruen: 1\n" + if output != expected { + t.Errorf("String() = %q, want %q", output, expected) + } +} + +func TestString_WithRounds(t *testing.T) { + sb := NewScoreboard() + sb.Score(TeamRed) + sb.NextRound() + sb.Score(TeamGreen) + sb.Score(TeamGreen) + sb.NextRound() + output := sb.String() + + expected := "Runde 3\nrot: 0 | gruen: 0\n---\nRunde 1: 1:0\nRunde 2: 0:2\n" + if output != expected { + t.Errorf("String() = %q, want %q", output, expected) + } +}