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") } }