Skip to content

Commit 83c7960

Browse files
committed
Replace HighScores array and slot indirection with a map
Key HighScores directly by StartingBridge, eliminating the slot constants, lookup table, and HighScoreSlot() function. Rename updateHighScore to registerScore to better describe the operation.
1 parent ef1729b commit 83c7960

8 files changed

Lines changed: 33 additions & 61 deletions

File tree

pkg/domain/constants.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,6 @@ const (
4040
PlaneY = 120
4141
)
4242

43-
// High score slot indices (0-based) corresponding to each StartingBridge option.
44-
const (
45-
HighScoreSlotBridge01 = 0
46-
HighScoreSlotBridge05 = 1
47-
HighScoreSlotBridge20 = 2
48-
HighScoreSlotBridge30 = 3
49-
)
50-
51-
// highScoreSlotTable maps StartingBridge to a 0-based HighScores slot index.
52-
var highScoreSlotTable = map[StartingBridge]int{ //nolint:gochecknoglobals // constant lookup table
53-
StartingBridge01: HighScoreSlotBridge01,
54-
StartingBridge05: HighScoreSlotBridge05,
55-
StartingBridge20: HighScoreSlotBridge20,
56-
StartingBridge30: HighScoreSlotBridge30,
57-
}
58-
59-
// HighScoreSlot returns the 0-based HighScores slot index for a StartingBridge value.
60-
func HighScoreSlot(sb StartingBridge) int {
61-
slot, ok := highScoreSlotTable[sb]
62-
if !ok {
63-
panic("domain: unknown StartingBridge value")
64-
}
65-
66-
return slot
67-
}
68-
6943
const (
7044
// DyingFrameCount is the number of frames the dying animation runs (~1.33 s).
7145
DyingFrameCount = Tps * 4 / 3

pkg/game/overview_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,12 @@ func TestUpdateGameOver_PreservesHighScore(t *testing.T) {
175175
g := NewGame()
176176
g.state.Screen = domain.ScreenGameOver
177177
g.state.Config.StartingBridge = domain.StartingBridge01
178-
slot := domain.HighScoreSlot(domain.StartingBridge01)
179-
g.state.HighScores[slot] = 12345
178+
g.state.HighScores[domain.StartingBridge01] = 12345
180179

181180
g.updateGameOver()
182181

183-
if g.state.HighScores[slot] != 12345 {
184-
t.Errorf("HighScores[%d] = %d, want 12345 (score lost during overview init)", slot, g.state.HighScores[slot])
182+
if g.state.HighScores[domain.StartingBridge01] != 12345 {
183+
t.Errorf("HighScores[Bridge01] = %d, want 12345 (score lost during overview init)", g.state.HighScores[domain.StartingBridge01])
185184
}
186185
}
187186

pkg/logic/death.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ func handlePostDeath(s *state.GameState, terrain TerrainRenderer) {
7171
// triggerGameOver updates the high score and transitions to the game over screen.
7272
// In two-player mode the higher of both players' scores is used.
7373
func triggerGameOver(s *state.GameState) {
74-
slot := domain.HighScoreSlot(s.Config.StartingBridge)
7574
score := s.Players[s.CurrentPlayer].Score
7675
if s.Config.IsTwoPlayer {
7776
other := s.CurrentPlayer.Other()
7877
if s.Players[other].Score > score {
7978
score = s.Players[other].Score
8079
}
8180
}
82-
updateHighScore(&s.HighScores, slot, score)
81+
registerScore(s.HighScores, s.Config.StartingBridge, score)
8382
s.Screen = domain.ScreenGameOver
8483
}
8584

pkg/logic/death_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,8 @@ func TestTriggerGameOver_TwoPlayer_UsesHigherScore(t *testing.T) {
307307

308308
triggerGameOver(s)
309309

310-
slot := domain.HighScoreSlot(domain.StartingBridge01)
311-
if s.HighScores[slot] != 8000 {
312-
t.Errorf("HighScores[%d] = %d, want 8000 (P2's higher score)", slot, s.HighScores[slot])
310+
if s.HighScores[domain.StartingBridge01] != 8000 {
311+
t.Errorf("HighScores[Bridge01] = %d, want 8000 (P2's higher score)", s.HighScores[domain.StartingBridge01])
313312
}
314313
}
315314

pkg/logic/scoring.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logic
22

33
import (
4+
"github.com/morozov/river-raid-ebiten/pkg/domain"
45
"github.com/morozov/river-raid-ebiten/pkg/state"
56
)
67

@@ -21,10 +22,10 @@ func addScore(player *state.PlayerState, controls *state.ControlFlags, points in
2122
}
2223
}
2324

24-
// updateHighScore replaces the high score for the given starting bridge slot if
25-
// the provided score exceeds it.
26-
func updateHighScore(highScores *[4]int, slot, score int) {
27-
if score > highScores[slot] {
28-
highScores[slot] = score
25+
// registerScore records the game score as the high score for the given starting
26+
// bridge if it exceeds the current record.
27+
func registerScore(highScores map[domain.StartingBridge]int, bridge domain.StartingBridge, score int) {
28+
if score > highScores[bridge] {
29+
highScores[bridge] = score
2930
}
3031
}

pkg/logic/scoring_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package logic
33
import (
44
"testing"
55

6+
"github.com/morozov/river-raid-ebiten/pkg/domain"
67
"github.com/morozov/river-raid-ebiten/pkg/state"
78
)
89

@@ -50,41 +51,39 @@ func TestAddScore_NoBonusLifeWithinSameThreshold(t *testing.T) {
5051
}
5152
}
5253

53-
func TestUpdateHighScore_ReplacesIfHigher(t *testing.T) {
54+
func TestRegisterScore_ReplacesIfHigher(t *testing.T) {
5455
t.Parallel()
5556

56-
var hs [4]int
57-
hs[0] = 1000
58-
updateHighScore(&hs, 0, 2000)
57+
hs := map[domain.StartingBridge]int{domain.StartingBridge01: 1000}
58+
registerScore(hs, domain.StartingBridge01, 2000)
5959

60-
if hs[0] != 2000 {
61-
t.Errorf("high score = %d, want 2000", hs[0]) //nolint:gosec // G602: fixed-size [4]int, index 0 is always valid
60+
if hs[domain.StartingBridge01] != 2000 {
61+
t.Errorf("high score = %d, want 2000", hs[domain.StartingBridge01])
6262
}
6363
}
6464

65-
func TestUpdateHighScore_NoChangeIfLower(t *testing.T) {
65+
func TestRegisterScore_NoChangeIfLower(t *testing.T) {
6666
t.Parallel()
6767

68-
var hs [4]int
69-
hs[0] = 5000
70-
updateHighScore(&hs, 0, 3000)
68+
hs := map[domain.StartingBridge]int{domain.StartingBridge01: 5000}
69+
registerScore(hs, domain.StartingBridge01, 3000)
7170

72-
if hs[0] != 5000 {
73-
t.Errorf("high score = %d, want 5000", hs[0]) //nolint:gosec // G602: fixed-size [4]int, index 0 is always valid
71+
if hs[domain.StartingBridge01] != 5000 {
72+
t.Errorf("high score = %d, want 5000", hs[domain.StartingBridge01])
7473
}
7574
}
7675

77-
func TestUpdateHighScore_UsesCorrectSlot(t *testing.T) {
76+
func TestRegisterScore_UsesCorrectKey(t *testing.T) {
7877
t.Parallel()
7978

80-
var hs [4]int
81-
updateHighScore(&hs, 2, 9999)
79+
hs := make(map[domain.StartingBridge]int)
80+
registerScore(hs, domain.StartingBridge20, 9999)
8281

83-
if hs[2] != 9999 {
84-
t.Errorf("hs[2] = %d, want 9999", hs[2]) //nolint:gosec // G602: fixed-size [4]int, index 2 is always valid
82+
if hs[domain.StartingBridge20] != 9999 {
83+
t.Errorf("hs[Bridge20] = %d, want 9999", hs[domain.StartingBridge20])
8584
}
8685

87-
if hs[0] != 0 || hs[1] != 0 || hs[3] != 0 { //nolint:gosec // G602: fixed-size [4]int, indices 0/1/3 are always valid
88-
t.Error("other slots should be unchanged")
86+
if hs[domain.StartingBridge01] != 0 || hs[domain.StartingBridge05] != 0 || hs[domain.StartingBridge30] != 0 {
87+
t.Error("other keys should be unchanged")
8988
}
9089
}

pkg/render/hud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func DrawHUD(screen draw.Image, s *state.GameState) {
7474
{Row: hudRow22, Col: hudColHIScore, Ink: platform.ColorCyan, Text: p2Score},
7575
})
7676
} else {
77-
hiScore := fmt.Sprintf("%0*d", hudHIScoreDigits, s.HighScores[domain.HighScoreSlot(s.Config.StartingBridge)])
77+
hiScore := fmt.Sprintf("%0*d", hudHIScoreDigits, s.HighScores[s.Config.StartingBridge])
7878
DrawText(screen, []assets.TextSpan{
7979
{Row: hudRow22, Col: hudColHILabel, Ink: platform.ColorWhite, Text: "HI"},
8080
{Row: hudRow22, Col: hudColHIScore, Ink: platform.ColorWhite, Text: hiScore},

pkg/state/state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ type GameState struct {
4444
Missile *PlayerMissile
4545
TankShell *TankShell
4646
HeliMissile *HeliMissile
47+
HighScores map[domain.StartingBridge]int
4748
InputInterface input.Interface
4849
Explosion Explosion
4950
Players [2]PlayerState
50-
HighScores [4]int
5151
Controls ControlFlags
5252
Config domain.GameConfig
5353
BridgeYPosition domain.SP
@@ -88,6 +88,7 @@ func NewGameState() *GameState {
8888
{Lives: domain.LivesInitial},
8989
{Lives: domain.LivesInitial},
9090
},
91+
HighScores: make(map[domain.StartingBridge]int),
9192
InputInterface: input.InterfaceFor(0),
9293
}
9394
}

0 commit comments

Comments
 (0)