package brawl // A fighter's inputs for a whole match are one character per tick. Each // character holds a move (a direction 0..15, or Still) and an action: // value = move + 17*action, written with the alphabet below. const alphabet = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~!" // per character: move and action, or -1 if the character isn't used var decMove, decAct [128]int func init() { for i := range decMove { decMove[i], decAct[i] = -1, -1 } for i := 0; i < 17*5; i++ { decMove[alphabet[i]], decAct[alphabet[i]] = i%17, i/17 } } // Encode writes one tick's input as a character. func Encode(move, act int) byte { return alphabet[move+17*act] } // Play replays a whole match from both fighters' inputs and returns the gold // each one ended with. Short inputs mean standing still for the rest; a bad // character panics. func Play(seed uint32, classA, classB int, inA, inB string) (int, int) { return play(seed, classA, classB, inA, inB, false, nil, nil) } // Duel is Play for a ghost: every run is recorded from A's corner, so B's // inputs (an earlier run) are turned round to play from the far corner. The // floor is mirrored through the middle, so the ghost sees the same arena. func Duel(seed uint32, classA, classB int, inA, inB string) (int, int) { return play(seed, classA, classB, inA, inB, true, nil, nil) } func play(seed uint32, classA, classB int, inA, inB string, mirrorB bool, moveA, moveB *Recipe) (int, int) { g := New(seed, classA, classB) g.Move[0], g.Move[1] = moveA, moveB if len(inA) > Ticks || len(inB) > Ticks { panic("brawl: inputs longer than a match") } for t := 0; t < Ticks; t++ { m0, a0, m1, a1 := Still, None, Still, None if t < len(inA) { c := inA[t] if c >= 128 || decMove[c] < 0 { panic("brawl: bad input character") } m0, a0 = decMove[c], decAct[c] } if t < len(inB) { c := inB[t] if c >= 128 || decMove[c] < 0 { panic("brawl: bad input character") } m1, a1 = decMove[c], decAct[c] if mirrorB && m1 != Still { m1 = (m1 + 8) % 16 } } g.Step(m0, a0, m1, a1) } return g.F[0].Gold, g.F[1].Gold } // Valid reports whether s is a well-formed set of inputs for one fighter. func Valid(s string) bool { if len(s) > Ticks { return false } for i := 0; i < len(s); i++ { if s[i] >= 128 || decMove[s[i]] < 0 { return false } } return true } func in(s string, t int) (int, int) { if t >= len(s) { return Still, None } c := s[t] if c >= 128 || decMove[c] < 0 { panic("brawl: bad input character") } return decMove[c], decAct[c] }