package brawl import "testing" // noisy makes a full match of inputs: hold a direction for a few ticks, // press a button now and then. func noisy(seed uint32) string { b := make([]byte, Ticks) s := seed dir, hold := 0, 0 for t := 0; t < Ticks; t++ { s = s*1103515245 + 12345 r := int(s >> 8) if hold == 0 { dir = r % 17 hold = 3 + r%8 } hold-- act := None if r%7 == 0 { act = 1 + (r>>4)%4 } b[t] = Encode(dir, act) } return string(b) } func TestInputsOnly(t *testing.T) { a, b := noisy(1), noisy(2) if len(a) != Ticks || len(b) != Ticks { t.Fatal("length") } } func TestFullMatch(t *testing.T) { a, b := noisy(1), noisy(2) ga, gb := Play(42, Hunter, Brawler, a, b) t.Logf("gold %d - %d", ga, gb) if ga+gb == 0 { t.Fatal("nobody scored") } } func TestDeterministic(t *testing.T) { a, b := noisy(5), noisy(6) x1, y1 := Play(7, Coward, Reader, a, b) x2, y2 := Play(7, Coward, Reader, a, b) if x1 != x2 || y1 != y2 { t.Fatal("replay differs") } } func TestWallsHold(t *testing.T) { for seed := uint32(0); seed < 6; seed++ { g := New(seed, Coward, Brawler) a, b := noisy(seed+10), noisy(seed+20) for i := 0; i < Ticks; i++ { m0, a0 := in(a, i) m1, a1 := in(b, i) g.Step(m0, a0, m1, a1) for s := 0; s < 2; s++ { f := g.F[s] cx, cy := f.X/U, f.Y/U if g.floor(cx, cy) == Wall { lx, ly := f.X-cx*U, f.Y-cy*U if lx > 100 && lx < 900 && ly > 100 && ly < 900 { t.Fatalf("seed %d tick %d: fighter %d inside a wall", seed, i, s) } } } } } } func TestLayouts(t *testing.T) { for arena := uint32(0); arena < 3; arena++ { g := New(arena, Hunter, Hunter) n := 0 for c := 0; c < Cells; c++ { k := g.Floor[c] if k == Empty { continue } n++ if g.Floor[Cells-1-c] != k { t.Fatalf("%s: square %d not mirrored", ArenaName(arena), c) } x, y := c%Size, c/Size if (x-1)*(x-1)+(y-1)*(y-1) <= 10 || (x-13)*(x-13)+(y-13)*(y-13) <= 10 { t.Fatalf("%s: square %d,%d too close to a start", ArenaName(arena), x, y) } if abs(x-Mid) <= 1 && abs(y-Mid) <= 1 { t.Fatalf("%s: square %d,%d in the middle", ArenaName(arena), x, y) } } want := 0 for _, l := range layouts[arena] { want += len(l) } if n != want { t.Fatalf("%s: %d squares, want %d (overlap?)", ArenaName(arena), n, want) } } } // arena sets two fighters a square apart in the open middle of the Rooftop. func arena() *Game { g := New(1, Brawler, Reader) g.Coin = [Cells]byte{} g.NCoins = 0 g.F[0].X, g.F[0].Y = 6500, 8500 g.F[1].X, g.F[1].Y = 7500, 8500 g.F[1].Gold = 10 return g } // A hit knocks gold onto the floor next to the victim, on the side nearest // the hitter, instead of into the hitter's purse. func TestKnockOut(t *testing.T) { g := arena() g.Step(Still, Attack, Still, None) if g.F[1].Gold != 9 || g.F[0].Gold != 0 || g.NCoins != 1 { t.Fatalf("gold %d-%d, %d coins on the floor", g.F[0].Gold, g.F[1].Gold, g.NCoins) } // victim on square 7,8; the free squares next to it nearest the hitter // (on 6,8) are 6,9 and 6,7, and 6,9 comes first if g.Coin[9*Size+6] != 1 { t.Fatal("coin not on 6,9") } } func TestParryPaysDirect(t *testing.T) { g := arena() g.F[0].Gold = 4 g.Step(Still, None, Still, Block) g.Step(Still, Attack, Still, None) if g.F[0].Gold != 3 || g.F[1].Gold != 11 || g.T >= g.F[0].StunT { t.Fatalf("parry: %d-%d, stun until %d", g.F[0].Gold, g.F[1].Gold, g.F[0].StunT) } } func TestComboStuns(t *testing.T) { g := arena() g.hit(0, 1) g.T = 10 g.hit(0, 1) if g.F[1].StunT > g.T { t.Fatal("two hits stunned") } g.T = 25 g.hit(0, 1) if g.F[1].StunT != 25+ComboStun || g.F[0].Combo != 0 { t.Fatalf("third hit: stun until %d", g.F[1].StunT) } // too slow: the combo starts again g.T = 30 g.hit(0, 1) g.T = 60 g.hit(0, 1) if g.F[0].Combo != 1 { t.Fatalf("combo %d after a gap", g.F[0].Combo) } // being hit breaks your own combo g.T = 62 g.hit(1, 1) if g.F[0].Combo != 0 { t.Fatal("combo kept after taking a hit") } } func TestBounty(t *testing.T) { g := arena() g.F[1].Gold = 20 // 20 ahead of a rival with 0 g.hit(0, 1) if g.F[1].Gold != 18 || g.NCoins != 2 { t.Fatalf("bounty hit left %d, %d coins", g.F[1].Gold, g.NCoins) } g.F[1].Shield = true g.hit(0, 2) // 4, less 1 for the shield if g.F[1].Gold != 15 { t.Fatalf("shielded bounty hit left %d", g.F[1].Gold) } g.hit(0, 1) // 15 ahead still pays double if g.F[1].Gold != 13 { t.Fatalf("left %d", g.F[1].Gold) } g.hit(0, 1) // 13 ahead: back to 1 if g.F[1].Gold != 12 { t.Fatalf("left %d", g.F[1].Gold) } } // While one side leads by Bounty, new coins land in the trailing side's half. func TestSpawnFavoursTrailing(t *testing.T) { for s := 0; s < 2; s++ { g := New(uint32(10+s), Hunter, Hunter) g.Coin = [Cells]byte{} g.NCoins = 0 g.F[1-s].Gold = Bounty for i := 0; i < 6; i++ { g.spawnPair(false, 1) } if g.NCoins != 12 { t.Fatalf("%d coins", g.NCoins) } for c := 0; c < Cells; c++ { if g.Coin[c] == 0 { continue } h := c%Size + c/Size if s == 0 && h > Size-1 || s == 1 && h < Size-1 { t.Fatalf("side %d trails but a coin fell on %d,%d", s, c%Size, c/Size) } } } }