161 lines
3.0 KiB
Go
161 lines
3.0 KiB
Go
// 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--
|
|
}
|
|
}
|
|
}
|