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.

parity_test.gno

8.29 Kb · 287 lines
  1package brawl
  2
  3import (
  4	"strconv"
  5	"testing"
  6)
  7
  8// gen makes a match of inputs the same way the browser's parity check does.
  9func gen(seed uint32) string {
 10	b := make([]byte, Ticks)
 11	s := seed
 12	dir, hold := 0, 0
 13	for t := 0; t < Ticks; t++ {
 14		s = s*1103515245 + 12345
 15		r := int(s >> 8)
 16		if hold == 0 {
 17			dir = r % 17
 18			hold = 5 + r%12
 19		}
 20		hold--
 21		act := 0
 22		if r%7 == 0 {
 23			act = 1 + (r>>4)%4
 24		}
 25		b[t] = Encode(dir, act)
 26	}
 27	return string(b)
 28}
 29
 30// fingerprint plays a match and folds both fighters' state into a number
 31// every tick, so any difference between two copies of the rules shows up.
 32func fingerprint(seed uint32, ca, cb int, inA, inB string, mirror bool) string {
 33	g := New(seed, ca, cb)
 34	var h uint32
 35	for t := 0; t < Ticks; t++ {
 36		m0, a0 := in(inA, t)
 37		m1, a1 := in(inB, t)
 38		if mirror && m1 != Still {
 39			m1 = (m1 + 8) % 16
 40		}
 41		g.Step(m0, a0, m1, a1)
 42		for s := 0; s < 2; s++ {
 43			f := g.F[s]
 44			h = h*31 + uint32(f.X+7*f.Y+13*f.Gold)
 45		}
 46		h = h*31 + uint32(g.NCoins+5*g.NPower)
 47	}
 48	return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10)
 49}
 50
 51func TestParity(t *testing.T) {
 52	out := ""
 53	for i := 0; i < 30; i++ {
 54		seed := uint32(i * 7)
 55		out += fingerprint(seed, i%5, (i*3+1)%5, gen(uint32(2*i+1)), gen(uint32(2*i+2)), i%2 == 1) + " "
 56	}
 57	t.Logf("PARITY %s", out)
 58}
 59
 60func botFingerprint(seed uint32, ca, level int, inA string) string {
 61	g := New(seed, ca, BotClass[level][seed%3])
 62	b := NewBot(level, seed)
 63	var h uint32
 64	for t := 0; t < Ticks; t++ {
 65		m0, a0 := in(inA, t)
 66		m1, a1 := b.Act(g, 1)
 67		g.Step(m0, a0, m1, a1)
 68		for s := 0; s < 2; s++ {
 69			f := g.F[s]
 70			h = h*31 + uint32(f.X+7*f.Y+13*f.Gold)
 71		}
 72	}
 73	return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10)
 74}
 75
 76func TestBotParity(t *testing.T) {
 77	out := ""
 78	for i := 0; i < 18; i++ {
 79		out += botFingerprint(uint32(i*5+1), i%5, i%3, gen(uint32(100+i))) + " "
 80	}
 81	t.Logf("BOTPARITY %s", out)
 82}
 83
 84// Move recipes: the browser must accept and refuse the same recipes, cost
 85// them the same, and play matches with them the same.
 86var recipeChecks = []string{
 87	"Blink Thief/84/D82S11", "Haymaker/150/S21K91T41", "Sniper/54/S15K25", "Magnet/40/P24",
 88	"Turtle/147/H00I90", "Bully/90/D41T53K94", "Grab Dash/33/P11D62", "Flee/36/D83",
 89	"A/32/S11", "Rush-in/50/D71I30", "O'Hare/150/S22P13T21", "Z/200/P35", "Pick/20/P11",
 90	"A/20/S11", "A/31/S11", "Sniper/53/S15K25", "", "x", "A/20/", "A/020/S11", "A/19/H00", "A/201/H00", " A/20/H00", "A /20/H00", "A/20/H01",
 91	"A/20/S11S11", "A/20/Q11", "A/20/S1", "A/99/S11H00I90K11", "A|B/20/H00", "A/30/S12H00",
 92	"Thief Lord/200/S35", "ABCDEFGHIJKLMNOPQ/20/H00", "A/20/D80", "A/20/D90", "A/20/S01", "A/20/S16",
 93	"A/20/I91", "A/20/T05", "A/20/K95", "A/2x/H00", "A/20/H00/", "A/20/h00",
 94}
 95
 96func recipeLine() string {
 97	out := ""
 98	for _, s := range recipeChecks {
 99		r, err := ParseRecipe(s)
100		if err != nil {
101			out += "x "
102			continue
103		}
104		out += r.String() + "=" + strconv.Itoa(r.Cost()) + " "
105	}
106	return out
107}
108
109func validRecipes() []*Recipe {
110	rs := []*Recipe{}
111	for _, s := range recipeChecks {
112		if r, err := ParseRecipe(s); err == nil {
113			rs = append(rs, r)
114		}
115	}
116	return rs
117}
118
119func moveFingerprint(seed uint32, ca, cb int, inA, inB string, mirror bool, ra, rb *Recipe) string {
120	g := New(seed, ca, cb)
121	g.Equip(0, ra)
122	g.Equip(1, rb)
123	var h uint32
124	for t := 0; t < Ticks; t++ {
125		m0, a0 := in(inA, t)
126		m1, a1 := in(inB, t)
127		if mirror && m1 != Still {
128			m1 = (m1 + 8) % 16
129		}
130		g.Step(m0, a0, m1, a1)
131		for s := 0; s < 2; s++ {
132			f := g.F[s]
133			h = h*31 + uint32(f.X+7*f.Y+13*f.Gold+17*f.StunT+19*f.InvT)
134		}
135		h = h*31 + uint32(g.NCoins+5*g.NPower)
136	}
137	return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10)
138}
139
140func TestParityMoves(t *testing.T) {
141	t.Logf("RECIPES %s", recipeLine())
142	rs := validRecipes()
143	n := len(rs)
144	out := ""
145	for i := 0; i < 22; i++ {
146		ra, rb := rs[i%n], rs[(i*3+1)%n]
147		if i%4 == 0 {
148			rb = nil
149		}
150		out += moveFingerprint(uint32(i*11+3), i%5, (i*2+3)%5, gen(uint32(300+2*i)), gen(uint32(301+2*i)), i%2 == 1, ra, rb) + " "
151	}
152	t.Logf("MOVES %s", out)
153	bo := ""
154	for i := 0; i < 11; i++ {
155		inA := gen(uint32(400 + i))
156		a, b := VersusMove(uint32(i*13+2), i%5, i%3, inA, rs[i%n].String())
157		bo += strconv.Itoa(a) + "-" + strconv.Itoa(b) + " "
158	}
159	t.Logf("MOVEBOT %s", bo)
160}
161
162// Bot profiles: the browser must accept and refuse the same profiles and
163// play the same matches against them.
164var profileChecks = []string{
165	"c1t3i1f6j8r6a7b2s5d4k0", "c0t3i0f9j9r8a9b9s9d9k5", "c4t9i9f0j0r1a0b0s0d0k0", "c2t4i2f1j5r4a3b6s9d8k3",
166	"c3t3i0f7j2r8a9b1s7d2k1", "c0t5i4f3j9r3a5b5s5d5k2", "c1t3i0f0j0r8a0b0s0d0k0",
167	"", "c5t3i1f6j8r6a7b2s5d4k0", "c1t1i1f6j8r6a7b2s5d4k0", "c1t2i1f6j8r6a7b2s5d4k0", "c1t3i1f6j8r9a7b2s5d4k0", "c1t3i1f6j8r0a7b2s5d4k0",
168	"c1t3i1f6j8r6a7b2s5d4k6", "c1t3i1f6j8r6a7b2s5d4k", "c1t3i1f6j8r6a7b2s5d4k00", "t3c1i1f6j8r6a7b2s5d4k0",
169	"C1t3i1f6j8r6a7b2s5d4k0", "c1t3i1f6j8r6a7b2s5d4kx", "c1t3i1f6j8r6a7b2s5d4k/", "c1t3 i1f6j8r6a7b2s5d4k0",
170}
171
172func profileFingerprint(seed uint32, ca int, p *Profile, inA string) string {
173	g := New(seed, ca, p.Class)
174	b := NewProfileBot(p, seed)
175	var h uint32
176	for t := 0; t < Ticks; t++ {
177		m0, a0 := in(inA, t)
178		m1, a1 := b.Act(g, 1)
179		g.Step(m0, a0, m1, a1)
180		for s := 0; s < 2; s++ {
181			f := g.F[s]
182			h = h*31 + uint32(f.X+7*f.Y+13*f.Gold+17*f.StunT+19*f.InvT)
183		}
184	}
185	return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10)
186}
187
188func TestParityProfiles(t *testing.T) {
189	out := ""
190	ps := []*Profile{}
191	for _, s := range profileChecks {
192		p, err := ParseProfile(s)
193		if err != nil {
194			out += "x "
195			continue
196		}
197		ps = append(ps, p)
198		out += p.String() + " "
199	}
200	n := len(ps)
201	for i := 0; i < 14; i++ {
202		out += profileFingerprint(uint32(i*17+4), i%5, ps[i%n], gen(uint32(500+i))) + " "
203	}
204	a, b := VersusProfile(9, 2, ps[0].String(), gen(600))
205	out += strconv.Itoa(a) + "-" + strconv.Itoa(b)
206	t.Logf("PROFILEBOT %s", out)
207}
208
209// Smart plans: the browser must accept and refuse the same plans, with the
210// same errors, and play the same rounds with them.
211var planChecks = []string{
212	"collect 120; chase 60; if near 1.4: attack; auto 60",
213	"run NE 20; goto 7,7; collect 100; if near 5: special; flee 30",
214	"chase 40; attack; block; special; dodge; wait 5; chase 40; if near 2 attack",
215	"auto 300",
216	"goto (12,2) 80; goto 2, 12; collect 300",
217	"RUN se 10\nWAIT 3\nCollect 50;if NEAR 3.5 : Special",
218	"flee 100; run 5 20; collect 120",
219	"",
220	"chase 30; if near 1.4: attack; chase 10; if near 1.4: attack; chase 10; if near 1.4: attack; collect 200",
221	"goto 0,14; goto 14,0; goto 7,3; dodge; collect 150",
222	"run N", "run X 5", "run 16 5", "wait 0", "wait 301", "attack 3", "goto 15,3", "goto 3", "if far 2: attack",
223	"if near 0: attack", "if near 1.45: attack", "if near 2: jump", "fly 3", "collect", "chase -3", "run N 5 ; ; dance",
224	"caf\u00e9 3", "if near 2:", "goto 3,4 0",
225	"guard on; auto 200; guard off; collect 40", "guard on; collect 240", "chase 60; guard on; if near 1.45: attack; chase 60; collect 100",
226	"guard", "guard maybe", "if near 1.456: attack", "if near 0.05: attack",
227}
228
229func planCheckList() []string {
230	l := append([]string{}, planChecks...)
231	long := ""
232	for i := 0; i < 72; i++ {
233		long += "wait 1;"
234	}
235	return append(l, long)
236}
237
238func planFingerprint(seed uint32, ca, cb int, pa, pb string, i int) string {
239	g := NewLength(seed, ca, cb, 240)
240	a, _ := ParsePlan(pa)
241	var b *Plan
242	var bot *Bot
243	if i%2 == 0 {
244		b, _ = ParsePlan(pb)
245	} else {
246		bot = NewBot(i%3, seed)
247	}
248	var h uint32
249	for !g.Over() {
250		m0, a0 := a.Next(g, 0)
251		var m1, a1 int
252		if b != nil {
253			m1, a1 = b.Next(g, 1)
254		} else {
255			m1, a1 = bot.Act(g, 1)
256		}
257		g.Step(m0, a0, m1, a1)
258		for s := 0; s < 2; s++ {
259			f := g.F[s]
260			h = h*31 + uint32(f.X+7*f.Y+13*f.Gold+17*f.StunT+19*m0+23*a0)
261		}
262		h = h*31 + uint32(g.NCoins)
263	}
264	done := 0
265	if a.Done() {
266		done = 1
267	}
268	return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.Itoa(done) + ":" + strconv.FormatUint(uint64(h), 10)
269}
270
271func TestParityPlans(t *testing.T) {
272	out := ""
273	ok := []string{}
274	for _, s := range planCheckList() {
275		if _, err := ParsePlan(s); err != "" {
276			out += "[" + err + "] "
277			continue
278		}
279		ok = append(ok, s)
280		out += "ok "
281	}
282	n := len(ok)
283	for i := 0; i < 16; i++ {
284		out += planFingerprint(uint32(i*19+5), i%5, (i*2+1)%5, ok[i%n], ok[(i+3)%n], i) + " "
285	}
286	t.Logf("PLANS %s", out)
287}