Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

brawl_test.gno

5.08 Kb · 216 lines
  1package brawl
  2
  3import "testing"
  4
  5// noisy makes a full match of inputs: hold a direction for a few ticks,
  6// press a button now and then.
  7func noisy(seed uint32) string {
  8	b := make([]byte, Ticks)
  9	s := seed
 10	dir, hold := 0, 0
 11	for t := 0; t < Ticks; t++ {
 12		s = s*1103515245 + 12345
 13		r := int(s >> 8)
 14		if hold == 0 {
 15			dir = r % 17
 16			hold = 3 + r%8
 17		}
 18		hold--
 19		act := None
 20		if r%7 == 0 {
 21			act = 1 + (r>>4)%4
 22		}
 23		b[t] = Encode(dir, act)
 24	}
 25	return string(b)
 26}
 27
 28func TestInputsOnly(t *testing.T) {
 29	a, b := noisy(1), noisy(2)
 30	if len(a) != Ticks || len(b) != Ticks {
 31		t.Fatal("length")
 32	}
 33}
 34
 35func TestFullMatch(t *testing.T) {
 36	a, b := noisy(1), noisy(2)
 37	ga, gb := Play(42, Hunter, Brawler, a, b)
 38	t.Logf("gold %d - %d", ga, gb)
 39	if ga+gb == 0 {
 40		t.Fatal("nobody scored")
 41	}
 42}
 43
 44func TestDeterministic(t *testing.T) {
 45	a, b := noisy(5), noisy(6)
 46	x1, y1 := Play(7, Coward, Reader, a, b)
 47	x2, y2 := Play(7, Coward, Reader, a, b)
 48	if x1 != x2 || y1 != y2 {
 49		t.Fatal("replay differs")
 50	}
 51}
 52
 53func TestWallsHold(t *testing.T) {
 54	for seed := uint32(0); seed < 6; seed++ {
 55		g := New(seed, Coward, Brawler)
 56		a, b := noisy(seed+10), noisy(seed+20)
 57		for i := 0; i < Ticks; i++ {
 58			m0, a0 := in(a, i)
 59			m1, a1 := in(b, i)
 60			g.Step(m0, a0, m1, a1)
 61			for s := 0; s < 2; s++ {
 62				f := g.F[s]
 63				cx, cy := f.X/U, f.Y/U
 64				if g.floor(cx, cy) == Wall {
 65					lx, ly := f.X-cx*U, f.Y-cy*U
 66					if lx > 100 && lx < 900 && ly > 100 && ly < 900 {
 67						t.Fatalf("seed %d tick %d: fighter %d inside a wall", seed, i, s)
 68					}
 69				}
 70			}
 71		}
 72	}
 73}
 74
 75func TestLayouts(t *testing.T) {
 76	for arena := uint32(0); arena < 3; arena++ {
 77		g := New(arena, Hunter, Hunter)
 78		n := 0
 79		for c := 0; c < Cells; c++ {
 80			k := g.Floor[c]
 81			if k == Empty {
 82				continue
 83			}
 84			n++
 85			if g.Floor[Cells-1-c] != k {
 86				t.Fatalf("%s: square %d not mirrored", ArenaName(arena), c)
 87			}
 88			x, y := c%Size, c/Size
 89			if (x-1)*(x-1)+(y-1)*(y-1) <= 10 || (x-13)*(x-13)+(y-13)*(y-13) <= 10 {
 90				t.Fatalf("%s: square %d,%d too close to a start", ArenaName(arena), x, y)
 91			}
 92			if abs(x-Mid) <= 1 && abs(y-Mid) <= 1 {
 93				t.Fatalf("%s: square %d,%d in the middle", ArenaName(arena), x, y)
 94			}
 95		}
 96		want := 0
 97		for _, l := range layouts[arena] {
 98			want += len(l)
 99		}
100		if n != want {
101			t.Fatalf("%s: %d squares, want %d (overlap?)", ArenaName(arena), n, want)
102		}
103	}
104}
105
106// arena sets two fighters a square apart in the open middle of the Rooftop.
107func arena() *Game {
108	g := New(1, Brawler, Reader)
109	g.Coin = [Cells]byte{}
110	g.NCoins = 0
111	g.F[0].X, g.F[0].Y = 6500, 8500
112	g.F[1].X, g.F[1].Y = 7500, 8500
113	g.F[1].Gold = 10
114	return g
115}
116
117// A hit knocks gold onto the floor next to the victim, on the side nearest
118// the hitter, instead of into the hitter's purse.
119func TestKnockOut(t *testing.T) {
120	g := arena()
121	g.Step(Still, Attack, Still, None)
122	if g.F[1].Gold != 9 || g.F[0].Gold != 0 || g.NCoins != 1 {
123		t.Fatalf("gold %d-%d, %d coins on the floor", g.F[0].Gold, g.F[1].Gold, g.NCoins)
124	}
125	// victim on square 7,8; the free squares next to it nearest the hitter
126	// (on 6,8) are 6,9 and 6,7, and 6,9 comes first
127	if g.Coin[9*Size+6] != 1 {
128		t.Fatal("coin not on 6,9")
129	}
130}
131
132func TestParryPaysDirect(t *testing.T) {
133	g := arena()
134	g.F[0].Gold = 4
135	g.Step(Still, None, Still, Block)
136	g.Step(Still, Attack, Still, None)
137	if g.F[0].Gold != 1 || g.F[1].Gold != 13 || g.T >= g.F[0].StunT {
138		t.Fatalf("parry: %d-%d, stun until %d", g.F[0].Gold, g.F[1].Gold, g.F[0].StunT)
139	}
140}
141
142func TestComboStuns(t *testing.T) {
143	g := arena()
144	g.hit(0, 1)
145	g.T = 10
146	g.hit(0, 1)
147	if g.F[1].StunT > g.T {
148		t.Fatal("two hits stunned")
149	}
150	g.T = 25
151	g.hit(0, 1)
152	if g.F[1].StunT != 25+ComboStun || g.F[0].Combo != 0 {
153		t.Fatalf("third hit: stun until %d", g.F[1].StunT)
154	}
155	// too slow: the combo starts again
156	g.T = 30
157	g.hit(0, 1)
158	g.T = 60
159	g.hit(0, 1)
160	if g.F[0].Combo != 1 {
161		t.Fatalf("combo %d after a gap", g.F[0].Combo)
162	}
163	// being hit breaks your own combo
164	g.T = 62
165	g.hit(1, 1)
166	if g.F[0].Combo != 0 {
167		t.Fatal("combo kept after taking a hit")
168	}
169}
170
171func TestBounty(t *testing.T) {
172	g := arena()
173	g.F[1].Gold = 20 // 20 ahead of a rival with 0
174	g.hit(0, 1)
175	if g.F[1].Gold != 18 || g.NCoins != 2 {
176		t.Fatalf("bounty hit left %d, %d coins", g.F[1].Gold, g.NCoins)
177	}
178	g.F[1].Shield = true
179	g.hit(0, 2) // 4, less 1 for the shield
180	if g.F[1].Gold != 15 {
181		t.Fatalf("shielded bounty hit left %d", g.F[1].Gold)
182	}
183	g.hit(0, 1) // 15 ahead still pays double
184	if g.F[1].Gold != 13 {
185		t.Fatalf("left %d", g.F[1].Gold)
186	}
187	g.hit(0, 1) // 13 ahead: back to 1
188	if g.F[1].Gold != 12 {
189		t.Fatalf("left %d", g.F[1].Gold)
190	}
191}
192
193// While one side leads by Bounty, new coins land in the trailing side's half.
194func TestSpawnFavoursTrailing(t *testing.T) {
195	for s := 0; s < 2; s++ {
196		g := New(uint32(10+s), Hunter, Hunter)
197		g.Coin = [Cells]byte{}
198		g.NCoins = 0
199		g.F[1-s].Gold = Bounty
200		for i := 0; i < 6; i++ {
201			g.spawnPair(false, 1)
202		}
203		if g.NCoins != 12 {
204			t.Fatalf("%d coins", g.NCoins)
205		}
206		for c := 0; c < Cells; c++ {
207			if g.Coin[c] == 0 {
208				continue
209			}
210			h := c%Size + c/Size
211			if s == 0 && h > Size-1 || s == 1 && h < Size-1 {
212				t.Fatalf("side %d trails but a coin fell on %d,%d", s, c%Size, c/Size)
213			}
214		}
215	}
216}