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.

game_test.gno

6.36 Kb · 214 lines
  1package game
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
  7)
  8
  9// set places both bots, their gold and the coins, for rule tests.
 10func set(g *Game, a, b bot.Pos, ga, gb int, coins ...bot.Pos) {
 11	g.CellA, g.CellB = cell(a), cell(b)
 12	g.GoldA, g.GoldB = ga, gb
 13	g.Coins = ""
 14	for _, c := range coins {
 15		g.Coins += string([]byte{coinByte(c)})
 16	}
 17}
 18
 19func TestNewBoardIsFairAndDeterministic(t *testing.T) {
 20	for seed := 0; seed < 20; seed++ {
 21		g := New(seed)
 22		coins := g.CoinList()
 23		if len(coins) != CoinCount {
 24			t.Fatalf("seed %d: %d coins", seed, len(coins))
 25		}
 26		for _, c := range coins {
 27			mirror := bot.Pos{bot.Size - 1 - c.X, bot.Size - 1 - c.Y}
 28			if !containsPos(coins, mirror) {
 29				t.Fatalf("seed %d: coin %v has no mirror", seed, c)
 30			}
 31			if isCorner(c) {
 32				t.Fatalf("seed %d: coin on a corner", seed)
 33			}
 34		}
 35		if New(seed).Coins != g.Coins {
 36			t.Fatalf("seed %d: board not deterministic", seed)
 37		}
 38	}
 39	if New(1).Coins == New(2).Coins {
 40		t.Error("different seeds should give different boards")
 41	}
 42}
 43
 44func TestPickupAndInvalidActions(t *testing.T) {
 45	g := New(1)
 46	set(g, bot.Pos{0, 0}, bot.Pos{8, 8}, 0, 0, bot.Pos{1, 0})
 47	g.Step(bot.Right, bot.Action(99))
 48	if g.GoldA != 1 || g.Coins != "" {
 49		t.Fatalf("pickup failed: gold %d coins %q", g.GoldA, g.Coins)
 50	}
 51	if g.Pos(1) != (bot.Pos{8, 8}) || bot.Action(g.Moves[1]) != bot.Stay {
 52		t.Fatal("invalid action should be recorded as stay")
 53	}
 54}
 55
 56func TestAttackDefendAndSteal(t *testing.T) {
 57	g := New(1)
 58	set(g, bot.Pos{4, 4}, bot.Pos{4, 5}, 2, 3)
 59
 60	if ev := g.Step(bot.Attack, bot.Defend); !ev.Parried[1] || g.GoldA != 1 || g.GoldB != 4 {
 61		t.Fatalf("defend should parry and take a coin back: %d-%d", g.GoldA, g.GoldB)
 62	}
 63	if ev := g.Step(bot.Attack, bot.Stay); !ev.Stole[0] || g.GoldA != 2 || g.GoldB != 3 {
 64		t.Fatal("attack should steal")
 65	}
 66	g.Step(bot.Attack, bot.Attack)
 67	if g.GoldA != 2 || g.GoldB != 3 {
 68		t.Fatal("mutual attacks cancel out")
 69	}
 70	set(g, bot.Pos{0, 0}, bot.Pos{5, 5}, 3, 2)
 71	g.Step(bot.Attack, bot.Defend)
 72	if g.GoldA != 3 || g.GoldB != 2 {
 73		t.Fatal("attack out of range must do nothing")
 74	}
 75	set(g, bot.Pos{4, 4}, bot.Pos{4, 5}, 0, 0)
 76	if ev := g.Step(bot.Attack, bot.Stay); ev.Stole[0] {
 77		t.Fatal("nothing to steal from an empty enemy")
 78	}
 79	set(g, bot.Pos{4, 4}, bot.Pos{4, 5}, 0, 3)
 80	if ev := g.Step(bot.Attack, bot.Defend); ev.Parried[1] || g.GoldB != 3 {
 81		t.Fatal("parrying an attacker with no gold takes nothing")
 82	}
 83}
 84
 85func TestBotsNeverShareASquare(t *testing.T) {
 86	// both step onto the same coin: priority alternates by turn
 87	g := New(1)
 88	set(g, bot.Pos{3, 4}, bot.Pos{5, 4}, 0, 0, bot.Pos{4, 4})
 89	g.Step(bot.Right, bot.Left) // turn 0: side 0 has priority
 90	if g.Pos(0) != (bot.Pos{4, 4}) || g.Pos(1) != (bot.Pos{5, 4}) || g.GoldA != 1 || g.GoldB != 0 {
 91		t.Fatalf("turn 0 contest: %v %v gold %d-%d", g.Pos(0), g.Pos(1), g.GoldA, g.GoldB)
 92	}
 93	set(g, bot.Pos{3, 4}, bot.Pos{5, 4}, 0, 0, bot.Pos{4, 4})
 94	g.Step(bot.Right, bot.Left) // turn 1: side 1 has priority
 95	if g.Pos(1) != (bot.Pos{4, 4}) || g.Pos(0) != (bot.Pos{3, 4}) || g.GoldB != 1 {
 96		t.Fatalf("turn 1 contest: %v %v", g.Pos(0), g.Pos(1))
 97	}
 98	// stepping into a bot that stays is blocked
 99	set(g, bot.Pos{3, 4}, bot.Pos{4, 4}, 0, 0)
100	g.Step(bot.Right, bot.Stay)
101	if g.Pos(0) != (bot.Pos{3, 4}) {
102		t.Fatal("moved into an occupied square")
103	}
104	// swapping places is blocked
105	g.Step(bot.Right, bot.Left)
106	if g.Pos(0) != (bot.Pos{3, 4}) || g.Pos(1) != (bot.Pos{4, 4}) {
107		t.Fatal("bots swapped places")
108	}
109	// following a bot that moves away is fine
110	g.Step(bot.Right, bot.Right)
111	if g.Pos(0) != (bot.Pos{4, 4}) || g.Pos(1) != (bot.Pos{5, 4}) {
112		t.Fatalf("follow failed: %v %v", g.Pos(0), g.Pos(1))
113	}
114	// whatever the moves, they never overlap
115	acts := []bot.Action{bot.Up, bot.Down, bot.Left, bot.Right, bot.Stay}
116	h := New(2)
117	for i := 0; !h.Over(); i++ {
118		h.Step(acts[i%5], acts[(i*3+1)%5])
119		if h.Pos(0) == h.Pos(1) {
120			t.Fatalf("overlap on turn %d", h.Turn)
121		}
122	}
123}
124
125func TestRespawnKeepsBoardAliveAndFair(t *testing.T) {
126	g := New(4)
127	for !g.Over() {
128		before := len(g.Coins)
129		ev := g.Step(bot.Stay, bot.Stay)
130		if g.Turn%RespawnEvery == 0 && g.Turn < bot.Turns && before <= MaxCoins-2 && ev.Spawned != 2 {
131			t.Fatalf("turn %d: expected a pair to spawn", g.Turn)
132		}
133		coins := g.CoinList()
134		if len(coins) > MaxCoins {
135			t.Fatalf("turn %d: %d coins", g.Turn, len(coins))
136		}
137		for _, c := range coins {
138			if !containsPos(coins, bot.Pos{bot.Size - 1 - c.X, bot.Size - 1 - c.Y}) {
139				t.Fatalf("turn %d: coin %v has no mirror", g.Turn, c)
140			}
141		}
142	}
143	// a hungry bot keeps finding coins all game long
144	h := New(4)
145	lastPickup := 0
146	for !h.Over() {
147		s := h.View(0, 0)
148		a := bot.Stay
149		if c, ok := s.NearestCoin(); ok {
150			a = bot.StepToward(s.Me, c)
151		}
152		before := h.GoldA
153		h.Step(a, bot.Stay)
154		if h.GoldA > before {
155			lastPickup = h.Turn
156		}
157	}
158	if lastPickup < 40 {
159		t.Fatalf("coins ran out too early: last pickup on turn %d", lastPickup)
160	}
161}
162
163func TestGameEndsAndWinner(t *testing.T) {
164	g := New(3)
165	for !g.Over() {
166		g.Step(bot.Stay, bot.Stay)
167	}
168	if g.Turn != bot.Turns || len(g.Moves) != 2*bot.Turns || g.Winner() != -1 {
169		t.Fatal("50 idle turns should be a draw")
170	}
171	defer func() {
172		if recover() == nil {
173			t.Error("stepping a finished game should panic")
174		}
175	}()
176	g.Step(bot.Stay, bot.Stay)
177}
178
179func TestReplayMatchesLiveGame(t *testing.T) {
180	g := New(9)
181	script := []bot.Action{bot.Right, bot.Down, bot.Attack, bot.Defend, bot.Left, bot.Up}
182	var live []Snapshot
183	for i := 0; !g.Over(); i++ {
184		a, b := script[i%len(script)], script[(i+2)%len(script)]
185		g.Step(a, b)
186		live = append(live, Snapshot{Turn: g.Turn, Pos: [2]bot.Pos{g.Pos(0), g.Pos(1)}, Gold: [2]int{g.GoldA, g.GoldB}})
187	}
188	for _, want := range live {
189		got := g.At(want.Turn)
190		if got.Pos != want.Pos || got.Gold != want.Gold {
191			t.Fatalf("turn %d: replay %v %v, live %v %v", want.Turn, got.Pos, got.Gold, want.Pos, want.Gold)
192		}
193	}
194	if start := g.At(0); len(start.Coins) != CoinCount || start.Pos[1] != (bot.Pos{8, 8}) {
195		t.Fatal("turn 0 should be the starting board")
196	}
197}
198
199func TestViewIsACopy(t *testing.T) {
200	g := New(5)
201	v := g.View(7, 1)
202	before := g.Coins
203	v.Coins[0] = bot.Pos{0, 0}
204	if g.Coins != before {
205		t.Fatal("view must not alias the board")
206	}
207	if v.Me != g.Pos(1) || v.Enemy != g.Pos(0) || v.Match != 7 {
208		t.Fatal("view from side 1 is wrong")
209	}
210	g.Step(bot.Defend, bot.Attack)
211	if v := g.View(7, 1); v.MyLast != bot.Attack || v.EnemyLast != bot.Defend {
212		t.Fatal("last actions wrong")
213	}
214}