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.

inputs.gno

2.56 Kb · 89 lines
 1package brawl
 2
 3// A fighter's inputs for a whole match are one character per tick. Each
 4// character holds a move (a direction 0..15, or Still) and an action:
 5// value = move + 17*action, written with the alphabet below.
 6const alphabet = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~!"
 7
 8// per character: move and action, or -1 if the character isn't used
 9var decMove, decAct [128]int
10
11func init() {
12	for i := range decMove {
13		decMove[i], decAct[i] = -1, -1
14	}
15	for i := 0; i < 17*5; i++ {
16		decMove[alphabet[i]], decAct[alphabet[i]] = i%17, i/17
17	}
18}
19
20// Encode writes one tick's input as a character.
21func Encode(move, act int) byte { return alphabet[move+17*act] }
22
23// Play replays a whole match from both fighters' inputs and returns the gold
24// each one ended with. Short inputs mean standing still for the rest; a bad
25// character panics.
26func Play(seed uint32, classA, classB int, inA, inB string) (int, int) {
27	return play(seed, classA, classB, inA, inB, false, nil, nil)
28}
29
30// Duel is Play for a ghost: every run is recorded from A's corner, so B's
31// inputs (an earlier run) are turned round to play from the far corner. The
32// floor is mirrored through the middle, so the ghost sees the same arena.
33func Duel(seed uint32, classA, classB int, inA, inB string) (int, int) {
34	return play(seed, classA, classB, inA, inB, true, nil, nil)
35}
36
37func play(seed uint32, classA, classB int, inA, inB string, mirrorB bool, moveA, moveB *Recipe) (int, int) {
38	g := New(seed, classA, classB)
39	g.Move[0], g.Move[1] = moveA, moveB
40	if len(inA) > Ticks || len(inB) > Ticks {
41		panic("brawl: inputs longer than a match")
42	}
43	for t := 0; t < Ticks; t++ {
44		m0, a0, m1, a1 := Still, None, Still, None
45		if t < len(inA) {
46			c := inA[t]
47			if c >= 128 || decMove[c] < 0 {
48				panic("brawl: bad input character")
49			}
50			m0, a0 = decMove[c], decAct[c]
51		}
52		if t < len(inB) {
53			c := inB[t]
54			if c >= 128 || decMove[c] < 0 {
55				panic("brawl: bad input character")
56			}
57			m1, a1 = decMove[c], decAct[c]
58			if mirrorB && m1 != Still {
59				m1 = (m1 + 8) % 16
60			}
61		}
62		g.Step(m0, a0, m1, a1)
63	}
64	return g.F[0].Gold, g.F[1].Gold
65}
66
67// Valid reports whether s is a well-formed set of inputs for one fighter.
68func Valid(s string) bool {
69	if len(s) > Ticks {
70		return false
71	}
72	for i := 0; i < len(s); i++ {
73		if s[i] >= 128 || decMove[s[i]] < 0 {
74			return false
75		}
76	}
77	return true
78}
79
80func in(s string, t int) (int, int) {
81	if t >= len(s) {
82		return Still, None
83	}
84	c := s[t]
85	if c >= 128 || decMove[c] < 0 {
86		panic("brawl: bad input character")
87	}
88	return decMove[c], decAct[c]
89}