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.

strategies.gno

1.59 Kb · 63 lines
 1// Package strategies holds starter bots written as plain functions. Copy one
 2// as a starting point for your own bot.
 3package strategies
 4
 5import "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
 6
 7// Hunter goes for the nearest coin and attacks when the enemy is next to it.
 8func Hunter(s bot.State) bot.Action {
 9	if bot.Adjacent(s.Me, s.Enemy) && s.EnemyGold > 0 {
10		return bot.Attack
11	}
12	if c, ok := s.NearestCoin(); ok {
13		return bot.StepToward(s.Me, c)
14	}
15	return bot.StepToward(s.Me, s.Enemy)
16}
17
18// Brawler chases the enemy and fights. Once it is ahead it defends.
19func Brawler(s bot.State) bot.Action {
20	if bot.Adjacent(s.Me, s.Enemy) {
21		if s.Winning() {
22			return bot.Defend
23		}
24		return bot.Attack
25	}
26	if s.EnemyGold == 0 {
27		if c, ok := s.NearestCoin(); ok {
28			return bot.StepToward(s.Me, c)
29		}
30	}
31	return bot.StepToward(s.Me, s.Enemy)
32}
33
34// Trickster collects coins and, when the enemy is next to it, alternates
35// between parrying and attacking so a predictable attacker walks into parries.
36func Trickster(s bot.State) bot.Action {
37	if bot.Adjacent(s.Me, s.Enemy) {
38		if s.Turn%2 == 0 && s.MyGold > 0 {
39			return bot.Defend
40		}
41		if s.EnemyGold > 0 {
42			return bot.Attack
43		}
44	}
45	if c, ok := s.NearestCoin(); ok {
46		return bot.StepToward(s.Me, c)
47	}
48	return bot.Defend
49}
50
51// Coward collects coins but runs from the enemy, and defends when cornered.
52func Coward(s bot.State) bot.Action {
53	if bot.Dist(s.Me, s.Enemy) <= 2 {
54		if a := bot.StepAway(s.Me, s.Enemy); a != bot.Stay {
55			return a
56		}
57		return bot.Defend
58	}
59	if c, ok := s.NearestCoin(); ok {
60		return bot.StepToward(s.Me, c)
61	}
62	return bot.Stay
63}