This commit is contained in:
Meghdad committed 2026-11-27 19:04:23 +03:30
commit efe2c8a9c8
13 files changed
+1700

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
// Package game contains the rules for a single Thermal Snake run.
package game
import (
"math/rand/v2"
"slices"
"time"
)
const (
Width = 22
Height = 13
PointsPerHeat = 100
HeatPerLevel = 3
MaxLevel = 8
)
type Point struct{ X, Y int }
type Direction uint8
const (
Up Direction = iota
Right
Down
Left
)
var offsets = [...]Point{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
type Outcome string
const (
Wall Outcome = "wall"
Coil Outcome = "coil"
Clear Outcome = "clear"
)
type Run struct {
body []Point // Head first.
food Point
direction Direction
turns [2]Direction
queued int
heat int
outcome Outcome
impact Point
random *rand.Rand
}
func New(seed uint64) *Run {
return &Run{
body: []Point{{6, 6}, {5, 6}, {4, 6}, {3, 6}},
food: Point{10, 6},
direction: Right,
random: rand.New(rand.NewPCG(seed, 0)),
}
}
func (r *Run) Score() int { return r.heat * PointsPerHeat }
func (r *Run) Heat() int { return r.heat }
func (r *Run) Level() int { return min(1+r.heat/HeatPerLevel, MaxLevel) }
func (r *Run) Outcome() Outcome { return r.outcome }
func (r *Run) Interval() time.Duration {
return time.Duration(175-15*(r.Level()-1)) * time.Millisecond
}
// Frame is an independent snapshot suitable for rendering a live or finished run.
type Frame struct {
Body []Point
Food Point
Direction Direction
Outcome Outcome
Impact Point
}
func (r *Run) Frame() Frame {
return Frame{slices.Clone(r.body), r.food, r.direction, r.outcome, r.impact}
}
// Steer buffers at most two perpendicular turns and applies one per step.
func (r *Run) Steer(direction Direction) {
if r.outcome != "" || direction > Left || r.queued == len(r.turns) {
return
}
previous := r.direction
if r.queued != 0 {
previous = r.turns[r.queued-1]
}
if direction%2 == previous%2 {
return
}
r.turns[r.queued] = direction
r.queued++
}
func (r *Run) Step() {
if r.outcome != "" {
return
}
if r.queued != 0 {
r.direction = r.turns[0]
r.turns[0] = r.turns[1]
r.queued--
}
head := r.body[0]
delta := offsets[r.direction]
next := Point{head.X + delta.X, head.Y + delta.Y}
if next.X < 0 || next.X >= Width || next.Y < 0 || next.Y >= Height {
r.outcome, r.impact = Wall, next
return
}
eating := next == r.food
occupied := r.body
if !eating {
occupied = occupied[:len(occupied)-1] // The tail moves away this step.
}
if slices.Contains(occupied, next) {
r.outcome, r.impact = Coil, next
return
}
if eating {
r.body = append(r.body, Point{})
}
copy(r.body[1:], r.body)
r.body[0] = next
if eating {
r.heat++
r.placeFood()
}
}
func (r *Run) placeFood() {
if len(r.body) == Width*Height {
r.outcome = Clear
return
}
var occupied [Height][Width]bool
for _, p := range r.body {
occupied[p.Y][p.X] = true
}
index := r.random.IntN(Width*Height - len(r.body))
for y := range Height {
for x := range Width {
if occupied[y][x] {
continue
}
if index == 0 {
r.food = Point{x, y}
return
}
index--
}
}
}
+86
View File
@@ -0,0 +1,86 @@
package game
import (
"slices"
"testing"
)
func TestHeatGrowthAndPace(t *testing.T) {
run := New(42)
for range 4 {
run.Step()
}
if run.Score() != 100 || len(run.body) != 5 || slices.Contains(run.body, run.food) {
t.Fatalf("after first heat got score %d, body %v and food %v", run.Score(), run.body, run.food)
}
firstInterval := run.Interval()
for range 2 {
head := run.body[0]
run.food = Point{head.X + 1, head.Y}
run.Step()
}
if run.Score() != 300 || run.Level() != 2 || run.Interval() >= firstInterval {
t.Fatalf("after three heat got score %d, level %d, interval %s", run.Score(), run.Level(), run.Interval())
}
}
func TestTurnsAreBufferedAndCannotReverse(t *testing.T) {
run := New(42)
run.Steer(Left) // Direct reversal is ignored.
run.Steer(Up)
run.Steer(Down) // Reverse of a queued turn is ignored too.
run.Steer(Left)
run.Step()
if head := run.body[0]; head != (Point{6, 5}) {
t.Fatalf("first turn reached %v", head)
}
run.Step()
if head := run.body[0]; head != (Point{5, 5}) {
t.Fatalf("second turn reached %v", head)
}
}
func TestCollisionRules(t *testing.T) {
run := New(42)
run.body = []Point{{3, 3}, {3, 2}, {2, 2}, {2, 3}}
run.direction = Left
run.Step()
if run.Outcome() != "" || run.body[0] != (Point{2, 3}) {
t.Fatal("moving into a vacating tail should be legal")
}
run.body = []Point{{3, 3}, {3, 2}, {2, 2}, {2, 3}, {2, 4}}
run.Step()
if run.Outcome() != Coil || run.impact != (Point{2, 3}) {
t.Fatalf("body collision got %q at %v", run.Outcome(), run.impact)
}
run = New(42)
run.Steer(Up)
for range Height {
run.Step()
}
if run.Outcome() != Wall || run.impact != (Point{6, -1}) || run.Score() != 0 {
t.Fatalf("wall collision got %q at %v with score %d", run.Outcome(), run.impact, run.Score())
}
}
func TestChamberClear(t *testing.T) {
run := New(42)
run.body = []Point{{0, 0}}
run.food = Point{1, 0}
for y := range Height {
for x := range Width {
if (Point{x, y}) != (Point{0, 0}) && (Point{x, y}) != run.food {
run.body = append(run.body, Point{x, y})
}
}
}
run.Step()
if run.Outcome() != Clear || len(run.body) != Width*Height {
t.Fatalf("last cell got outcome %q with length %d", run.Outcome(), len(run.body))
}
run.Step()
if run.Outcome() != Clear || run.Score() != 100 {
t.Fatal("finished runs should not advance")
}
}