-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.go
788 lines (767 loc) · 21.9 KB
/
state.go
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package go_carcassonne
import (
"fmt"
"math/rand"
"sort"
"strings"
bg "github.com/quibbble/go-boardgame"
"github.com/quibbble/go-boardgame/pkg/bgerr"
)
// state holds all necessary game objects and high level game logic
type state struct {
turn string
teams []string
winners []string
playTiles map[string]*tile // teams to the tiles to place onto the board at the start of any given turn
lastPlacedTiles map[string]*tile // the tiles that were last placed by each team
board *board
boardTokens []*token // a list of tokens currently on the board
tokens map[string]int // number of tokens each team can play
scores map[string]int // points of each team
deck *deck
}
func newState(teams []string, random *rand.Rand) *state {
tokens := make(map[string]int)
scores := make(map[string]int)
playTiles := make(map[string]*tile)
lastPlacedTiles := make(map[string]*tile)
for _, team := range teams {
tokens[team] = 7
scores[team] = 0
}
deck := newDeck(random)
for _, team := range teams {
tile, _ := deck.Draw()
playTiles[team] = tile
lastPlacedTiles[team] = nil
}
return &state{
turn: teams[0],
teams: teams,
winners: make([]string, 0),
playTiles: playTiles,
lastPlacedTiles: lastPlacedTiles,
board: newBoard(),
boardTokens: make([]*token, 0),
tokens: tokens,
scores: scores,
deck: deck,
}
}
func (s *state) RotateTileRight(team string) error {
if s.playTiles[team] == nil {
return &bgerr.Error{
Err: fmt.Errorf("cannot rotate tile"),
Status: bgerr.StatusInvalidAction,
}
}
s.playTiles[team].RotateRight()
return nil
}
func (s *state) RotateTileLeft(team string) error {
if s.playTiles[team] == nil {
return &bgerr.Error{
Err: fmt.Errorf("cannot rotate tile"),
Status: bgerr.StatusInvalidAction,
}
}
s.playTiles[team].RotateLeft()
return nil
}
func (s *state) PlaceTile(team string, tile *tile, x, y int) error {
if team != s.turn {
return &bgerr.Error{
Err: fmt.Errorf("%s cannot play on %s turn", team, s.turn),
Status: bgerr.StatusWrongTurn,
}
}
if s.playTiles[team] == nil || !tile.equals(s.playTiles[team]) {
return &bgerr.Error{
Err: fmt.Errorf("%s cannot place tile %+v", team, tile),
Status: bgerr.StatusInvalidActionDetails,
}
}
if err := s.board.Place(tile, x, y); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
s.lastPlacedTiles[team] = tile
s.playTiles[team] = nil
// if there are no tokens to place or cannot place token anywhere skip place token action here
if s.tokens[s.turn] == 0 || len(s.targets()) <= 1 {
if err := s.PlaceToken(s.turn, true, 0, 0, "", ""); err != nil {
return err
}
}
return nil
}
func (s *state) PlaceToken(team string, pass bool, x, y int, typ, side string) error {
if len(s.winners) > 0 {
return &bgerr.Error{
Err: fmt.Errorf("%s game already completed", key),
Status: bgerr.StatusGameOver,
}
}
if team != s.turn {
return &bgerr.Error{
Err: fmt.Errorf("currently %s's turn", s.turn),
Status: bgerr.StatusWrongTurn,
}
}
if s.playTiles[team] != nil {
return &bgerr.Error{
Err: fmt.Errorf("cannot place token"),
Status: bgerr.StatusInvalidAction,
}
}
// try placing token
if !pass {
if s.lastPlacedTiles[team].X != x || s.lastPlacedTiles[team].Y != y {
return &bgerr.Error{
Err: fmt.Errorf("cannot place token on tile at %d,%d", s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y),
Status: bgerr.StatusInvalidAction,
}
}
if !contains(TokenTypes, typ) {
return &bgerr.Error{
Err: fmt.Errorf("invalid token type %s", typ),
Status: bgerr.StatusInvalidActionDetails,
}
}
if (typ == Thief || typ == Knight) && !contains(Sides, side) {
return &bgerr.Error{
Err: fmt.Errorf("invalid side %s with token %s", side, typ),
Status: bgerr.StatusInvalidActionDetails,
}
} else if typ == Farmer && !contains(FarmSides, side) {
return &bgerr.Error{
Err: fmt.Errorf("invalid farm side %s with token %s", side, typ),
Status: bgerr.StatusInvalidActionDetails,
}
} else if typ == Monk && s.lastPlacedTiles[team].Center != Cloister {
return &bgerr.Error{
Err: fmt.Errorf("cannot place %s on tile that does not contain %s", Monk, Cloister),
Status: bgerr.StatusInvalidAction,
}
}
if s.tokens[team] <= 0 {
return &bgerr.Error{
Err: fmt.Errorf("not enough tokens to place for team %s", team),
Status: bgerr.StatusInvalidAction,
}
}
// check to ensure token does not connect to pre-existing tokens in given structure
switch typ {
case Thief:
road, err := s.board.generateRoad(x, y, side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
tokens := tokensInStructure(s.boardTokens, road)
if len(tokens) > 0 {
return &bgerr.Error{
Err: fmt.Errorf("cannot place token on road that is already claimed"),
Status: bgerr.StatusInvalidAction,
}
}
case Knight:
city, err := s.board.generateCity(x, y, side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
tokens := tokensInStructure(s.boardTokens, city)
if len(tokens) > 0 {
return &bgerr.Error{
Err: fmt.Errorf("cannot place token on city that is already claimed"),
Status: bgerr.StatusInvalidAction,
}
}
case Farmer:
farm, err := s.board.generateFarm(x, y, side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
tokens := tokensInStructure(s.boardTokens, farm)
if len(tokens) > 0 {
return &bgerr.Error{
Err: fmt.Errorf("cannot place token on farm that is already claimed"),
Status: bgerr.StatusInvalidAction,
}
}
}
// add the token
s.tokens[team]--
token := newToken(x, y, team, typ, side)
s.boardTokens = append(s.boardTokens, token)
}
// score completed cities
citySides := make([]string, 0)
for _, side := range Sides {
if s.lastPlacedTiles[team].Sides[side] == City {
citySides = append(citySides, side)
}
}
if len(citySides) > 0 {
if s.lastPlacedTiles[team].ConnectedCitySides {
citySides = citySides[:1]
}
for _, citySide := range citySides {
city, err := s.board.generateCity(s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y, citySide)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
if city.complete {
// add to completed list in board
s.board.completeCities = append(s.board.completeCities, city)
// check if token inside city
inside := tokensInStructure(s.boardTokens, city)
if len(inside) > 0 {
// score and add points
points, err := scoreCity(city)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
winners := pointsWinners(inside)
for _, winner := range winners {
s.scores[winner] += points
}
// remove inside from board and add back to tokens pile
for _, token := range inside {
s.tokens[token.Team]++
}
s.boardTokens = removeTokens(s.boardTokens, inside...)
// set color of completed
for _, n := range city.nodes {
for _, side := range n.sides {
n.tile.Teams[side] = winners
}
}
}
}
}
}
// score completed roads
roadSides := make([]string, 0)
for _, side := range Sides {
if s.lastPlacedTiles[team].Sides[side] == Road {
roadSides = append(roadSides, side)
}
}
if len(roadSides) > 0 {
if len(roadSides) <= 2 {
roadSides = roadSides[:1]
}
for _, roadSide := range roadSides {
road, err := s.board.generateRoad(s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y, roadSide)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
if road.complete {
inside := tokensInStructure(s.boardTokens, road)
if len(inside) > 0 {
// score and add points
points, err := scoreRoad(road)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
winners := pointsWinners(inside)
for _, winner := range winners {
s.scores[winner] += points
}
// remove inside from board and add back to tokens pile
for _, token := range inside {
s.tokens[token.Team]++
}
s.boardTokens = removeTokens(s.boardTokens, inside...)
// add to completed list in board
s.board.completeRoads = append(s.board.completeRoads, road)
// set color of completed
for _, n := range road.nodes {
for _, side := range n.sides {
n.tile.Teams[side] = winners
}
}
}
}
}
}
// score completed cloister
cloisterLocationsToCheck := [][]int{
{s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y},
{s.lastPlacedTiles[team].X + 1, s.lastPlacedTiles[team].Y},
{s.lastPlacedTiles[team].X - 1, s.lastPlacedTiles[team].Y},
{s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y + 1},
{s.lastPlacedTiles[team].X, s.lastPlacedTiles[team].Y - 1},
{s.lastPlacedTiles[team].X + 1, s.lastPlacedTiles[team].Y + 1},
{s.lastPlacedTiles[team].X + 1, s.lastPlacedTiles[team].Y - 1},
{s.lastPlacedTiles[team].X - 1, s.lastPlacedTiles[team].Y + 1},
{s.lastPlacedTiles[team].X - 1, s.lastPlacedTiles[team].Y - 1}}
for _, location := range cloisterLocationsToCheck {
tile := s.board.tile(location[0], location[1])
if tile != nil && tile.Center == Cloister {
count, err := s.board.tilesSurroundingCloister(location[0], location[1])
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
if count == 8 {
for _, token := range s.boardTokens {
if token.Type == Monk && token.X == location[0] && token.Y == location[1] {
// add to score
s.scores[token.Team] += count + 1
// remove inside from board and add back to tokens pile
s.tokens[token.Team]++
s.boardTokens = removeTokens(s.boardTokens, token)
// set color of completed
tile.CenterTeam = token.Team
break
}
}
}
}
}
// draw tile for player
if !s.deck.Empty() {
tile, _ := s.deck.Draw()
s.playTiles[s.turn] = tile
}
tilesInHands := 0
for _, tile := range s.playTiles {
if tile != nil {
tilesInHands++
}
}
if tilesInHands > 0 {
// next turn
for idx, team := range s.teams {
if team == s.turn {
s.turn = s.teams[(idx+1)%len(s.teams)]
break
}
}
// edge case where play tile isn't playable so re-draw
if !s.board.playable(s.playTiles[s.turn]) {
if !s.deck.Empty() {
tried := []*tile{s.playTiles[s.turn]}
retryLimit := s.deck.Size()
for i := 0; i < retryLimit; i++ {
tile, _ := s.deck.Draw()
if s.board.playable(tile) {
s.playTiles[s.turn] = tile
s.deck.Add(tried...)
tried = nil
break
} else {
tried = append(tried, tile)
}
}
if tried != nil {
// edge case where no tile in the deck is playable so end the game instead
if err := s.score(); err != nil {
return err
}
}
} else {
// edge case where tiles still remain but cannot be played so end the game instead
if err := s.score(); err != nil {
return err
}
}
}
} else {
// all tiles have been played so score
if err := s.score(); err != nil {
return err
}
}
return nil
}
func (s *state) score() error {
// score incomplete roads, cities, and cloister and score farms
for len(s.boardTokens) > 0 {
token := s.boardTokens[0]
switch token.Type {
case Knight:
city, err := s.board.generateCity(token.X, token.Y, token.Side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
// score and add points
points, err := scoreCity(city)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
inside := tokensInStructure(s.boardTokens, city)
winners := pointsWinners(inside)
for _, winner := range winners {
s.scores[winner] += points
}
// remove inside from board and add back to tokens pile
for _, token := range inside {
s.tokens[token.Team]++
}
s.boardTokens = removeTokens(s.boardTokens, inside...)
// set color of incomplete
for _, n := range city.nodes {
for _, side := range n.sides {
n.tile.Teams[side] = winners
}
}
case Thief:
road, err := s.board.generateRoad(token.X, token.Y, token.Side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
// score and add points
points, err := scoreRoad(road)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
inside := tokensInStructure(s.boardTokens, road)
winners := pointsWinners(inside)
for _, winner := range winners {
s.scores[winner] += points
}
// remove inside from board and add back to tokens pile
for _, token := range inside {
s.tokens[token.Team]++
}
s.boardTokens = removeTokens(s.boardTokens, inside...)
// set color of incomplete
for _, n := range road.nodes {
for _, side := range n.sides {
n.tile.Teams[side] = winners
}
}
case Monk:
tile := s.board.tile(token.X, token.Y)
if tile != nil && tile.Center == Cloister {
count, err := s.board.tilesSurroundingCloister(token.X, token.Y)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
s.scores[token.Team] += count + 1
// remove inside from board and add back to tokens pile
s.tokens[token.Team]++
s.boardTokens = removeTokens(s.boardTokens, token)
// set color of incomplete
tile.CenterTeam = token.Team
}
case Farmer:
farm, err := s.board.generateFarm(token.X, token.Y, token.Side)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
// score and add points
points, err := scoreFarm(farm, s.board.completeCities)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
inside := tokensInStructure(s.boardTokens, farm)
winners := pointsWinners(inside)
for _, winner := range winners {
s.scores[winner] += points
}
// remove inside from board and add back to tokens pile
for _, token := range inside {
s.tokens[token.Team]++
}
s.boardTokens = removeTokens(s.boardTokens, inside...)
// set color of farmland
for _, n := range farm.nodes {
// get number of city sides
numCities := 0
for _, section := range n.tile.Sides {
if section == City {
numCities++
}
}
// edge case where two adjacent disconnected city sections leads to uncolored farmland between them
if !n.tile.ConnectedCitySides && numCities == 2 {
for _, farmSide := range FarmSides {
n.tile.FarmTeams[farmSide] = winners
}
} else {
// otherwise, do normal coloring
for _, farmSide := range n.sides {
n.tile.FarmTeams[farmSide] = winners
}
}
}
}
}
// winner is team with the highest score
max := 0
winners := make([]string, 0)
for p, score := range s.scores {
if score > max {
max = score
winners = []string{p}
} else if score == max {
winners = append(winners, p)
}
}
s.winners = winners
return nil
}
func (s *state) SetWinners(winners []string) error {
for _, winner := range winners {
if !contains(s.teams, winner) {
return &bgerr.Error{
Err: fmt.Errorf("winner not in teams"),
Status: bgerr.StatusInvalidActionDetails,
}
}
}
s.winners = winners
return nil
}
func (s *state) targets() []*bg.BoardGameAction {
targets := make([]*bg.BoardGameAction, 0)
if s.playTiles[s.turn] != nil {
// add rotating tile as valid targets
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionRotateTileLeft,
}, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionRotateTileRight,
})
// find all valid places to play tile
emptySpaces := s.board.getEmptySpaces()
for _, emptySpace := range emptySpaces {
valid := true
for _, side := range Sides {
if emptySpace.adjacent[side] != nil && emptySpace.adjacent[side].Sides[AcrossSide[side]] != s.playTiles[s.turn].Sides[side] {
valid = false
}
}
if valid {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceTile,
MoreDetails: PlaceTileActionDetails{
X: emptySpace.X,
Y: emptySpace.Y,
},
})
}
}
} else {
// find all valid places to play token
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
Pass: true,
},
})
if s.lastPlacedTiles[s.turn].Center == Cloister {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Monk,
},
})
}
for _, side := range Sides {
switch s.lastPlacedTiles[s.turn].Sides[side] {
case Road:
// check if road is already claimed
road, _ := s.board.generateRoad(s.lastPlacedTiles[s.turn].X, s.lastPlacedTiles[s.turn].Y, side)
if len(tokensInStructure(s.boardTokens, road)) == 0 {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Thief,
Side: side,
},
})
}
// check if farmland A is claimed
farm, _ := s.board.generateFarm(s.lastPlacedTiles[s.turn].X, s.lastPlacedTiles[s.turn].Y, sideToFarmSide(side, FarmNotchA))
if len(tokensInStructure(s.boardTokens, farm)) == 0 {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Farmer,
Side: sideToFarmSide(side, FarmNotchA),
},
})
}
// check if farmland B is claimed
farm, _ = s.board.generateFarm(s.lastPlacedTiles[s.turn].X, s.lastPlacedTiles[s.turn].Y, sideToFarmSide(side, FarmNotchB))
if len(tokensInStructure(s.boardTokens, farm)) == 0 {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Farmer,
Side: sideToFarmSide(side, FarmNotchB),
},
})
}
case City:
// check if city is already claimed
city, _ := s.board.generateCity(s.lastPlacedTiles[s.turn].X, s.lastPlacedTiles[s.turn].Y, side)
if len(tokensInStructure(s.boardTokens, city)) == 0 {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Knight,
Side: side,
},
})
}
case Farm:
// check if farmland is claimed
farm, _ := s.board.generateFarm(s.lastPlacedTiles[s.turn].X, s.lastPlacedTiles[s.turn].Y, sideToFarmSide(side, FarmNotchA))
if len(tokensInStructure(s.boardTokens, farm)) == 0 {
targets = append(targets, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Farmer,
Side: sideToFarmSide(side, FarmNotchA),
},
}, &bg.BoardGameAction{
Team: s.turn,
ActionType: ActionPlaceToken,
MoreDetails: PlaceTokenActionDetails{
X: s.lastPlacedTiles[s.turn].X,
Y: s.lastPlacedTiles[s.turn].Y,
Type: Farmer,
Side: sideToFarmSide(side, FarmNotchB),
},
})
}
}
}
}
return targets
}
func (s *state) message() string {
message := fmt.Sprintf("%s must place a tile", s.turn)
if s.playTiles[s.turn] == nil {
message = fmt.Sprintf("%s must place a token", s.turn)
}
if len(s.winners) > 0 {
message = fmt.Sprintf("%s tie", strings.Join(s.winners, ", "))
if len(s.winners) == 1 {
message = fmt.Sprintf("%s wins", s.winners[0])
}
}
return message
}
// get the tokens that fall in the structure
func tokensInStructure(tokens []*token, structure *structure) []*token {
tokensInside := make([]*token, 0)
for _, token := range tokens {
for _, n := range structure.nodes {
// check if token type matches section type and token on node
if StructureTypeToTokenType[structure.typ] == token.Type &&
n.tile.X == token.X && n.tile.Y == token.Y && contains(n.sides, token.Side) {
tokensInside = append(tokensInside, token)
}
}
}
return tokensInside
}
// create a new list that has removed toRemove from original
func removeTokens(original []*token, toRemove ...*token) []*token {
newTokens := make([]*token, 0)
for _, token := range original {
found := false
for _, rm := range toRemove {
if token.X == rm.X && token.Y == rm.Y && token.Side == rm.Side &&
token.Type == rm.Type && token.Team == rm.Team {
found = true
}
}
if !found {
newTokens = append(newTokens, token)
}
}
return newTokens
}
// given a list of tokens, get the teams(s) with the most tokens
func pointsWinners(tokens []*token) []string {
max := 0
winners := make([]string, 0)
tally := make(map[string]int)
for _, token := range tokens {
tally[token.Team]++
}
for team, count := range tally {
if count > max {
winners = []string{team}
max = count
} else if count == max {
winners = append(winners, team)
}
}
sort.Strings(winners)
return winners
}