Skip to content

Commit 134d451

Browse files
committed
store high score next to executable
1 parent e6f54ca commit 134d451

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -----------------------------------------------------------------------------
22
# Game-specific
33
# -----------------------------------------------------------------------------
4-
.gopherdash_highscore
4+
#.gopherdash_highscore
55
# -----------------------------------------------------------------------------
66
# Go tooling
77
# -----------------------------------------------------------------------------

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,18 @@ func main() {
118118
// HIGH‑SCORE PERSISTENCE
119119
// ----------------------------------------------------------------------------
120120

121-
func scoreFilePath() string {
122-
wd, err := os.Getwd()
121+
func highscorePath() string {
122+
exe, err := os.Executable() // full path to the running binary
123123
if err != nil {
124-
return highScoreFile
124+
// fallback: use CWD so the game still works during `go run`
125+
return ".gopherdash_highscore"
125126
}
126-
return filepath.Join(wd, highScoreFile)
127+
dir := filepath.Dir(exe)
128+
return filepath.Join(dir, ".gopherdash_highscore")
127129
}
128130

129131
func loadHighScore() int {
130-
data, err := os.ReadFile(scoreFilePath())
132+
data, err := os.ReadFile(highscorePath())
131133
if err != nil {
132134
return 0
133135
}
@@ -139,7 +141,7 @@ func loadHighScore() int {
139141
}
140142

141143
func saveHighScore(score int) {
142-
_ = os.WriteFile(scoreFilePath(), []byte(strconv.Itoa(score)), 0o644)
144+
_ = os.WriteFile(highscorePath(), []byte(strconv.Itoa(score)), 0o644)
143145
}
144146

145147
// ----------------------------------------------------------------------------

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Go](https://github.com/krisfur/gopherdash/actions/workflows/go.yml/badge.svg)](https://github.com/krisfur/gopherdash/actions/workflows/go.yml)
44

5+
![Go](https://img.shields.io/badge/Go-00ADD8?style=flat&logo=go&logoColor=white) [![Go](https://img.shields.io/badge/Go-1.24.4-blue)](https://go.dev/)
6+
57
> A tiny terminal endless‑runner written in Go using [Bubble Tea](https://github.com/charmbracelet/bubbletea) & [Lip Gloss](https://github.com/charmbracelet/lipgloss).
68
>
79
> Jump rocks, leap holes, chase the high score—all in your shell.
@@ -74,7 +76,7 @@ PRs welcome! Bug fixes, difficulty tweaks, new themes—go for it.
7476

7577
1. Fork & clone
7678
2. `git checkout -b feature/my‑thing`
77-
3. Hack away, keep `go vet ./...` & `go test` green
79+
3. Hack away, keep the `go test` green
7880
4. Open a pull request
7981

8082
---

0 commit comments

Comments
 (0)