reader.gno
2.86 Kb · 101 lines
1// Package reader is a realm bot that learns. Every time it stands next to an
2// opponent it writes down what that opponent did, and it keeps those notes
3// across matches. Up close it plays the answer to the opponent's habit: parry
4// a bot that likes to attack, step around one that likes to parry, and attack
5// anyone else. The more it plays a bot, the better it reads it.
6package reader
7
8import (
9 "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
10 "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/strategies"
11 "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/arena/v2"
12 "gno.land/p/nt/avl/v0"
13 "gno.land/p/nt/ufmt/v0"
14)
15
16// habits is what reader has seen an opponent do when the two were adjacent.
17type habits struct {
18 Attacks, Defends, Others int
19}
20
21func (h *habits) seen() int { return h.Attacks + h.Defends + h.Others }
22
23var (
24 notes = avl.NewTree() // opponent name -> *habits
25 wasClose bool // adjacent at the end of our last move
26 lastMatch int // match and turn of our last move, to spot new matches
27 lastTurn int
28)
29
30func habitsOf(name string) *habits {
31 if v := notes.Get(name); v != nil {
32 return v.(*habits)
33 }
34 h := &habits{}
35 notes.Set(name, h)
36 return h
37}
38
39type readerBot struct{}
40
41// Move is called by the arena every turn.
42func (readerBot) Move(s bot.State) bot.Action {
43 h := habitsOf(s.EnemyBot)
44 if wasClose && lastMatch == s.Match && lastTurn == s.Turn-1 {
45 switch s.EnemyLast {
46 case bot.Attack:
47 h.Attacks++
48 case bot.Defend:
49 h.Defends++
50 default:
51 h.Others++
52 }
53 }
54 lastMatch, lastTurn = s.Match, s.Turn
55 wasClose = bot.Adjacent(s.Me, s.Enemy)
56
57 if wasClose {
58 switch {
59 case h.Attacks*2 > h.seen() && s.MyGold > 0:
60 return bot.Defend // it attacks a lot: parry
61 case h.Defends*2 > h.seen():
62 return coinMove(s) // it parries a lot: don't attack into it
63 case s.EnemyGold > 0:
64 return bot.Attack
65 }
66 }
67 return coinMove(s)
68}
69
70func coinMove(s bot.State) bot.Action {
71 if c, ok := s.NearestCoin(); ok {
72 return bot.StepToward(s.Me, c)
73 }
74 return strategies.Hunter(s)
75}
76
77func init(cur realm) {
78 arena.RegisterBot(cross(cur), "reader", readerBot{})
79}
80
81// Render shows what reader has learned about each opponent.
82func Render(path string) string {
83 out := "# reader\n\nA realm bot that learns: it notes what each opponent does up close and plays the answer. [Record](/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/arena/v2:bot/reader)\n\n"
84 if notes.Size() == 0 {
85 return out + "No notes yet.\n"
86 }
87 out += "| Opponent | Attacks | Parries | Other | Reader's answer |\n|---|---|---|---|---|\n"
88 notes.Iterate("", "", func(name string, v any) bool {
89 h := v.(*habits)
90 answer := "attack"
91 switch {
92 case h.Attacks*2 > h.seen():
93 answer = "parry"
94 case h.Defends*2 > h.seen():
95 answer = "step around it"
96 }
97 out += ufmt.Sprintf("| %s | %d | %d | %d | %s |\n", name, h.Attacks, h.Defends, h.Others, answer)
98 return false
99 })
100 return out
101}