1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
package game
import (
"strings"
"testing"
"muhq.space/muhqs-game/go/utils"
)
func TestTileTargets(t *testing.T) {
mapDef := `map: |+1
HSTS
HSFS
TSWD
symbols:
T: tower
H: house
F: farm
S: street
W: deep water
D: docks
`
s := NewLocalState()
m, _ := readMap(strings.NewReader(mapDef))
s.SetMap(m)
p := s.AddNewPlayer("player", NewDeck())
u := s.addNewUnit(NewCard("base/archer"), Position{1, 1}, p)
s.addNewUnit(NewCard("base/cavalry"), Position{1, 2}, p)
a := newFullAction(u,
func(Action) ActionResolveFunc { var f ActionResolveFunc; return f },
"mock full action")
tDesc := newTargetDesc("tile")
trgt := newTarget(s, tDesc, a)
opts := trgt.Options()
if len(opts) != 12 {
t.Fatal("expexted 12 candidates not:", len(opts))
}
tDesc = newTargetDesc("water tile")
trgt = newTarget(s, tDesc, a)
opts = trgt.Options()
if len(opts) != 1 {
t.Fatal("expected 1 water candidates not:", len(opts))
}
tDesc = newTargetDesc("free tile")
trgt = newTarget(s, tDesc, a)
opts = trgt.Options()
if len(opts) != 10 {
t.Fatal("expected 10 free candidates not:", len(opts))
}
tDesc = newTargetDesc("available tile")
trgt = newTarget(s, tDesc, a)
opts = trgt.Options()
if len(opts) != 9 {
t.Fatal("expected 9 available candidates not:", len(opts))
}
tDesc = newTargetDesc("adjacent tile")
trgt = newTarget(s, tDesc, a)
opts = trgt.Options()
if len(opts) != 9 {
t.Fatal("expected 9 available candidates not:", len(opts))
}
tDesc = newTargetDesc("adjacent available tile")
trgt = newTarget(s, tDesc, a)
opts = trgt.Options()
if len(opts) != 6 {
t.Fatal("expected 6 available candidates not:", len(opts))
}
fisher := NewCard("nautics/fisher")
p.Hand.AddCard(fisher)
pa := NewPlayAction(p, fisher)
opts = pa.Target().Options()
if len(opts) != 1 {
t.Fatal("expected 1 available candidates not:", len(opts))
}
}
func TestSpawnTargets(t *testing.T) {
s := NewLocalState()
m, _ := GetMap("the-kraken")
s.SetMap(m)
s.activePhase = actionPhase
p := s.AddNewPlayer("player", NewDeck())
ctrl := newMockPlayerControl(p)
ctrl.actionToSend = NewPassPriority(p)
p.Ctrl = ctrl
p.gainResource(2)
farmer := NewCard("misc/farmer")
p.Hand.AddCard(farmer)
fisher := NewCard("nautics/fisher")
p.Hand.AddCard(fisher)
pa := NewPlayAction(p, farmer)
if !pa.Target().RequireSelection() {
t.Fatal("playing farmer has not require selection")
}
if !pa.Target().AllowSelection() {
t.Fatal("playing farmer has no valid targets")
}
farmerPos := Position{3, 13}
err := pa.Target().AddSelection(m.TileAt(farmerPos))
if err != nil {
t.Fatal("Not allowed to play farmer at:", farmerPos)
}
s.declareAction(pa)
if s.stack.IsEmpty() {
t.Fatal("farmer play action not declared")
}
s.stack.resolve()
pa2 := NewPlayAction(p, fisher)
if !pa2.Target().RequireSelection() {
t.Fatal("playing fisher has not require selection")
}
if !pa2.Target().AllowSelection() {
t.Fatal("playing fisher has no valid targets")
}
fisherPos := Position{3, 10}
pa2.Target().AddSelection(m.TileAt(fisherPos))
if err != nil {
t.Fatal("Not allowed to play fisher at:", fisherPos)
}
s.declareAction(pa2)
if s.stack.IsEmpty() {
t.Fatal("fisher play action not declared")
}
s.stack.resolve()
}
func TestFlexAttackTargets(t *testing.T) {
mapDef := `map: |+1
HSTS
HSFS
TSWS
symbols:
T: tower
H: house
F: farm
S: street
W: deep water
`
s := NewLocalState()
m, _ := readMap(strings.NewReader(mapDef))
s.SetMap(m)
p := s.AddNewPlayer("player", NewDeck())
o := s.AddNewPlayer("oponent", NewDeck())
u := s.addNewUnit(NewCard("base/pikeman"), Position{1, 1}, p)
s.addNewUnit(NewCard("base/pikeman"), Position{0, 0}, o)
s.addNewUnit(NewCard("base/pikeman"), Position{3, 1}, o)
s.addNewUnit(NewCard("base/cavalry"), Position{3, 2}, o)
a := NewAttackAction(u)
opts := a.Target().Options()
if len(opts) != 2 {
t.Fatal("expected 2 attackable options")
}
}
func TestDisjunction(t *testing.T) {
mapDef := `map: |+1
HST
HSF
TSW
symbols:
T: tower
H: house
F: farm
S: street
W: deep water
`
s := NewLocalState()
m, _ := readMap(strings.NewReader(mapDef))
s.SetMap(m)
p := s.AddNewPlayer("player", NewDeck())
pioneer := NewUnit(NewCard("base/pioneer"), s.Map().TileAt(Position{1, 1}), p)
s.addPermanent(pioneer)
sword := newEquipmentFromPath("base/sword", s.Map().TileAt(Position{0, 1}), p)
s.addPermanent(sword)
fa := pioneer.FullActions[0]
targets := fa.Targets()
if err := targets.AddSelection(s.Map().TileAt(Position{1, 1})); err != nil {
t.Fatalf("TileAt(1,1) not a valid target for %v", fa)
}
if err := targets.CheckTargets(s); err != nil {
t.Fatalf("TileAt(1,1) not a valid target for %v", fa)
}
targets.ClearSelections()
if err := targets.AddSelection(sword); err != nil {
t.Fatalf("%v not a valid target for %v", sword, fa)
}
if err := targets.CheckTargets(s); err != nil {
t.Fatalf("%v not a valid target for %v", sword, fa)
}
}
func TestUnitConstraint(t *testing.T) {
s, _, p, o := newMockState()
a := s.addNewUnit(NewCard("base/archer"), Position{0, 0}, p)
oa := s.addNewUnit(NewCard("base/archer"), Position{1, 1}, o)
desc := newTargetDesc("enemy unit")
// Use a dummy pass action to pass the player to the target constraints
target := newTarget(s, desc, NewPassPriority(p))
options := target.Options()
if !utils.InterfaceSliceContains(options, oa) {
t.Fatal("enemy units does not contain opponent's archer")
}
if utils.InterfaceSliceContains(options, a) {
t.Fatal("enemy units contain own archer")
}
desc = newTargetDesc("allied unit")
// Use a dummy pass action to pass the player to the target constraints
target = newTarget(s, desc, NewPassPriority(p))
options = target.Options()
if utils.InterfaceSliceContains(options, oa) {
t.Fatal("'allied units' does contain opponent's archer")
}
if !utils.InterfaceSliceContains(options, a) {
t.Fatal("'allied units' does not contain own archer")
}
// Test for controlled units
oa.changeController(p)
desc = newTargetDesc("unit you control")
target = newTarget(s, desc, NewPassPriority(p))
options = target.Options()
if !utils.InterfaceSliceContains(options, oa) {
t.Fatal("'units you control' does not contain controlled archer owned by the oponent")
}
if !utils.InterfaceSliceContains(options, a) {
t.Fatal("'units you control' does not contain own archer")
}
}
func TestSpellConstraint(t *testing.T) {
s, _, p, _ := newMockState()
spellInDiscard := NewCard("magic/more!")
p.DiscardPile.AddCard(spellInDiscard)
p.Resource = 3
declaredSpell := NewPlayAction(p, NewCard("magic/ritual!"))
s.declareAction(declaredSpell)
unit := NewPlayAction(p, NewCard("base/archer"))
desc := newTargetDesc("spell")
target := newTarget(s, desc, NewPassPriority(p))
if err := target.AddSelection(declaredSpell); err == nil {
t.Fatal("could target declared spell")
}
if err := target.AddSelection(spellInDiscard); err != nil {
t.Fatal("failed to target spell:", err)
}
if err := target.AddSelection(unit); err == nil {
t.Fatal("could target unit play action:", err)
}
desc = newTargetDesc("declared spell")
target = newTarget(s, desc, NewPassPriority(p))
if !utils.InterfaceSliceContains(target.Options(), declaredSpell) {
t.Fatal("options does not contain the declared spell")
}
if err := target.AddSelection(declaredSpell); err != nil {
t.Fatal("failed to target spell:", err)
}
if err := target.AddSelection(spellInDiscard); err == nil {
t.Fatal("spell in discard pile is not declared:", err)
}
if err := target.AddSelection(unit); err == nil {
t.Fatal("could target unit play action:", err)
}
}
|