-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_test.go
1476 lines (1220 loc) · 44.3 KB
/
game_test.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package shed
import (
"fmt"
"math/rand"
"reflect"
"testing"
"time"
"github.com/minaorangina/shed/deck"
utils "github.com/minaorangina/shed/internal"
"github.com/minaorangina/shed/protocol"
)
func TestGameTurn(t *testing.T) {
gameWithRandomPlayers := func() (int, *shed) {
rand.Seed(time.Now().UnixNano())
randomNumberOfPlayers := rand.Intn(2) + 2
players := map[string]*PlayerCards{}
playerIDs := []string{}
for i := 0; i < randomNumberOfPlayers; i++ {
id := fmt.Sprintf("player-%d", i)
playerIDs = append(playerIDs, id)
players[id] = &PlayerCards{}
}
game := NewShed(ShedOpts{deck: deck.New(), playerCards: players, playerIDs: playerIDs})
return randomNumberOfPlayers, game
}
t.Run("turns loop back through all players", func(t *testing.T) {
numPlayers, game := gameWithRandomPlayers()
for i := 0; i < numPlayers; i++ {
game.turn()
}
utils.AssertEqual(t, game.currentTurnIdx, 0)
for i := 0; i < numPlayers+1; i++ {
game.turn()
}
utils.AssertEqual(t, game.currentTurnIdx, 1)
utils.AssertEqual(t, game.currentPlayerID, "player-1")
})
}
func TestGameStageZero(t *testing.T) {
t.Run("players asked to reorganise their hand", func(t *testing.T) {
// Given a new game
game := NewShed(ShedOpts{})
// When the game starts
err := game.Start([]string{"p1", "p2", "p3"})
utils.AssertNoError(t, err)
msgs, err := game.Next()
// then players receive an instruction to reorganise their cards
utils.AssertNoError(t, err)
utils.AssertTrue(t, len(msgs) == len(game.playerIDs))
for _, m := range msgs {
utils.AssertEqual(t, m.Command, protocol.Reorg)
}
// and the game is awaiting a response
utils.AssertEqual(t, game.awaitingResponse, protocol.Reorg)
})
t.Run("reorganised cards handled correctly", func(t *testing.T) {
// Given a new game
game := NewShed(ShedOpts{})
// When the game has started and Next is called
err := game.Start([]string{"p1", "p2", "p3"})
utils.AssertNoError(t, err)
_, err = game.Next()
utils.AssertNoError(t, err)
// Then the game enters the reorganisation stage
utils.AssertEqual(t, game.stage, preGame)
p2NewCards := somePlayerCards(3)
p2NewCards.Unseen = game.playerCards["p2"].Unseen
want := map[string]*PlayerCards{
"p1": game.playerCards["p1"],
"p2": p2NewCards,
"p3": game.playerCards["p3"],
}
// and the players send their response
msgs, err := game.ReceiveResponse([]InboundMessage{
{
PlayerID: "p1",
Command: protocol.Reorg,
Hand: game.playerCards["p1"].Hand,
Seen: game.playerCards["p1"].Seen,
},
{
PlayerID: "p2",
Command: protocol.Reorg,
Hand: p2NewCards.Hand,
Seen: p2NewCards.Seen,
},
{
PlayerID: "p3",
Command: protocol.Reorg,
Hand: game.playerCards["p3"].Hand,
Seen: game.playerCards["p3"].Seen,
},
})
// and the response is accepted
utils.AssertNoError(t, err)
utils.AssertDeepEqual(t, msgs, []OutboundMessage(nil))
// and players' cards are updated
for id, c := range game.playerCards {
utils.AssertDeepEqual(t, *c, *want[id])
}
})
}
func TestGameStageOne(t *testing.T) {
t.Run("stage 1: player has legal moves", func(t *testing.T) {
// Given a game in stage 1, with a low-value card on the pile
lowValueCard := deck.NewCard(deck.Four, deck.Hearts)
pile := []deck.Card{lowValueCard}
// And a player with higher-value cards in their hand
targetCard := deck.NewCard(deck.Nine, deck.Clubs)
hand := []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
targetCard,
deck.NewCard(deck.Six, deck.Diamonds),
}
pc := &PlayerCards{Hand: hand}
game := NewShed(ShedOpts{
stage: clearDeck,
deck: someDeck(4),
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
oldHand := game.playerCards[game.currentPlayerID].Hand
oldHandSize, oldPileSize, oldDeckSize := len(oldHand), len(game.pile), len(game.deck)
// When the game progresses, then players are informed of the current turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertNotNil(t, msgs)
utils.AssertEqual(t, len(msgs), len(game.playerIDs))
checkNextMessages(t, msgs, protocol.PlayHand, game)
moves := getMoves(msgs, game.currentPlayerID)
// And the game expects a response
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayHand)
// And when player response is received
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayHand,
Decision: []int{moves[1]}, // target card is the second one
}})
utils.AssertNoError(t, err)
newHand := game.playerCards[game.currentPlayerID].Hand
newHandSize, newPileSize, newDeckSize := len(newHand), len(game.pile), len(game.deck)
// Then the pile contains the selected card
utils.AssertTrue(t, newPileSize > oldPileSize)
utils.AssertTrue(t, containsCard(game.pile, targetCard))
// And the deck decreases in size
utils.AssertTrue(t, newDeckSize < oldDeckSize)
// And the size of the player's hand remains the same
utils.AssertTrue(t, newHandSize == oldHandSize)
// But the cards in the player's hand changed
utils.AssertEqual(t, reflect.DeepEqual(oldHand, newHand), false)
// And all cards are unique
utils.AssertTrue(t, cardsUnique(newHand)) // this fails sometimes
utils.AssertTrue(t, cardsUnique(game.pile))
// And the game produces messages to all players
// expecting a response only from the current player
utils.AssertEqual(t, len(msgs), len(game.playerIDs))
checkReceiveResponseMessages(t, msgs, protocol.ReplenishHand, game)
utils.AssertEqual(t, game.awaitingResponse, protocol.ReplenishHand)
// And when the current player acks and releases their turn
previousPlayerID := game.currentPlayerID
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.ReplenishHand,
}})
utils.AssertNoError(t, err)
// And the next player is up
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
})
t.Run("stage 1: player plays multiple cards from their hand", func(t *testing.T) {
// Given a game in stage 1
lowValueCard := deck.NewCard(deck.Four, deck.Hearts)
pile := []deck.Card{lowValueCard}
targetCards := []deck.Card{
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Nine, deck.Diamonds),
}
// And a player with two cards of the same value in their hand
pc := &PlayerCards{Hand: append(targetCards, deck.NewCard(deck.Eight, deck.Hearts))}
game := NewShed(ShedOpts{
stage: clearDeck,
deck: someDeck(4),
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
oldHand := game.playerCards[game.currentPlayerID].Hand
oldHandSize := len(oldHand)
oldPileSize := len(game.pile)
oldDeckSize := len(game.deck)
// When the player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertNotNil(t, msgs)
utils.AssertEqual(t, game.currentTurnIdx, 0)
utils.AssertTrue(t, game.currentPlayerID != "")
moves := msgs[0].Moves
utils.AssertTrue(t, len(moves) > 1)
// And chooses to play two of the same card
_, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayHand,
Decision: []int{0, 1},
}})
utils.AssertNoError(t, err)
newHand := game.playerCards[game.currentPlayerID].Hand
newHandSize := len(newHand)
newPileSize := len(game.pile)
newDeckSize := len(game.deck)
// Then the hand size remains the same, but the cards have changed
utils.AssertEqual(t, newHandSize, oldHandSize)
utils.AssertEqual(t, containsCard(newHand, targetCards...), false) // sometimes fails
// And the pile has two extra cards (from the hand)
utils.AssertTrue(t, newPileSize > oldPileSize)
utils.AssertTrue(t, containsCard(game.pile, targetCards...))
// And the deck has two fewer cards
utils.AssertTrue(t, newDeckSize == oldDeckSize-2)
})
t.Run("stage 1: player picks up pile", func(t *testing.T) {
// Given a game with a high-value card on the pile
highValueCard := deck.NewCard(deck.Ace, deck.Clubs) // Ace of Clubs
// and a player with low-value cards in their Hand
lowValueCards := []deck.Card{
deck.NewCard(deck.Four, deck.Hearts),
deck.NewCard(deck.Five, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
}
game := NewShed(ShedOpts{
stage: clearDeck,
deck: someDeck(4),
pile: []deck.Card{highValueCard},
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": &PlayerCards{Hand: deck.Deck(lowValueCards)},
"player-2": somePlayerCards(3),
},
})
oldHandSize := len(game.playerCards[game.currentPlayerID].Hand)
oldPileSize := len(game.pile)
oldDeckSize := len(game.deck)
// when a player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.SkipTurn)
newHandSize := len(game.playerCards[game.currentPlayerID].Hand)
newPileSize := len(game.pile)
newDeckSize := len(game.deck)
// then the current player's hand includes the cards from the pile
utils.AssertEqual(t, newHandSize, oldHandSize+oldPileSize)
// and the pile is now empty
utils.AssertEqual(t, newPileSize, 0)
// and the deck is unchanged
utils.AssertEqual(t, newDeckSize, oldDeckSize)
// then everyone is informed
utils.AssertNotNil(t, msgs)
utils.AssertEqual(t, len(msgs), len(game.playerIDs))
// and the current player's OutboundMessage has the expected content
utils.AssertTrue(t, msgs[0].ShouldRespond)
utils.AssertEqual(t, msgs[0].Command, protocol.SkipTurn)
// and the other players' OutboundMessages have the expected content
utils.AssertEqual(t, msgs[1].ShouldRespond, false)
utils.AssertEqual(t, msgs[1].Command, protocol.SkipTurn)
// and the current player's response is handled correctly
previousPlayerID := game.currentPlayerID
response, err := game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.SkipTurn,
}})
utils.AssertNoError(t, err)
utils.AssertDeepEqual(t, response, []OutboundMessage(nil))
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
// and the next player is up
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
})
t.Run("stage 1: not enough cards in deck", func(t *testing.T) {
// Given a game in stage 1 with one card left on the deck
lowValueCard := deck.NewCard(deck.Four, deck.Hearts)
targetCards := []deck.Card{
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Nine, deck.Diamonds),
}
// And a player with two cards of the same value in their hand
pc := &PlayerCards{Hand: append(targetCards, deck.NewCard(deck.Eight, deck.Hearts))}
game := NewShed(ShedOpts{
stage: clearDeck,
deck: someDeck(1),
pile: []deck.Card{lowValueCard},
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
oldHand := game.playerCards[game.currentPlayerID].Hand
oldHandSize, oldPileSize := len(oldHand), len(game.pile)
// When the player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertNotNil(t, msgs)
moves := msgs[0].Moves
utils.AssertTrue(t, len(moves) > 1)
// And chooses to play two cards of the same value
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayHand,
Decision: []int{0, 1},
}})
utils.AssertNoError(t, err)
newHand := game.playerCards[game.currentPlayerID].Hand
newHandSize := len(newHand)
newPileSize := len(game.pile)
newDeckSize := len(game.deck)
// Then the hand size is smaller, and the cards have changed
utils.AssertEqual(t, newHandSize, oldHandSize-1)
utils.AssertEqual(t, containsCard(newHand, targetCards...), false) // fails sometimes
// And the pile has two extra cards (from the hand)
utils.AssertEqual(t, newPileSize, oldPileSize+2)
utils.AssertTrue(t, containsCard(game.pile, targetCards...))
// And the deck is empty
utils.AssertEqual(t, newDeckSize, 0)
// And the game produces messages to all players
// expecting a response only from the current player
utils.AssertEqual(t, len(msgs), len(game.playerIDs))
checkReceiveResponseMessages(t, msgs, protocol.ReplenishHand, game)
utils.AssertEqual(t, game.awaitingResponse, protocol.ReplenishHand)
// And when the current player acks and releases their turn
previousPlayerID := game.currentPlayerID
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.ReplenishHand,
}})
utils.AssertNoError(t, err)
// And the game switches to stage 2
utils.AssertEqual(t, game.stage, clearCards)
// And the next player is up
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
})
}
func TestGameStageTwo(t *testing.T) {
t.Run("stage 2: hand gets smaller", func(t *testing.T) {
// Given a game in stage 2, with a low-value card on the pile
lowValueCard := deck.NewCard(deck.Six, deck.Hearts)
pile := []deck.Card{lowValueCard}
// And a player with higher-value cards in their hand
hand := []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
}
pc := &PlayerCards{Hand: hand}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
// When a player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertNotNil(t, msgs)
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayHand)
moves := getMoves(msgs, game.currentPlayerID)
oldHandSize := len(game.playerCards[game.currentPlayerID].Hand)
oldSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
oldUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
previousPlayerID := game.currentPlayerID
cardChoice := []int{moves[0]}
_, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayHand,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.EndOfTurn)
newHandSize := len(game.playerCards[game.currentPlayerID].Hand)
newSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
newUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
// Then their hand is smaller, but their remaining cards are unchanged
utils.AssertTrue(t, newHandSize < oldHandSize)
utils.AssertTrue(t, newSeenSize == oldSeenSize)
utils.AssertTrue(t, newUnseenSize == oldUnseenSize)
// And when the player releases their turn
_, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.EndOfTurn,
}})
utils.AssertNoError(t, err)
// Then the game is no longer expecting a response
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
// And it's the next player's turn
utils.AssertTrue(t, previousPlayerID != game.currentPlayerID)
})
t.Run("stage 2: player has legal moves and no hand cards", func(t *testing.T) {
// Given a game in stage 2, with a low-value card on the pile
lowValueCard := deck.NewCard(deck.Six, deck.Hearts)
pile := []deck.Card{lowValueCard}
// And a player with an empty hand and a full set of Seen cards
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
// When the player starts their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.PlaySeen)
// Then everyone is informed
checkNextMessages(t, msgs, protocol.PlaySeen, game)
moves := getMoves(msgs, game.currentPlayerID)
utils.AssertTrue(t, len(moves) > 0)
utils.AssertDeepEqual(t, moves, []int{0, 1, 2})
oldSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
oldUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
oldPileSize := len(game.pile)
// And when the player makes their choice
cardChoice := []int{moves[0]}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlaySeen,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
newHandSize := len(game.playerCards[game.currentPlayerID].Hand)
newSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
newUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
newPileSize := len(game.pile)
// Then they have one less Seen card
utils.AssertEqual(t, newSeenSize, oldSeenSize-1)
// but their remaining cards are unchanged
utils.AssertEqual(t, newHandSize, 0)
utils.AssertEqual(t, newUnseenSize, oldUnseenSize)
// And the pile increases
utils.AssertEqual(t, newPileSize, oldPileSize+1)
// And everyone is informed
for _, m := range msgs {
utils.AssertEqual(t, m.Command, protocol.EndOfTurn)
if m.PlayerID == game.currentPlayerID {
utils.AssertTrue(t, m.ShouldRespond)
} else {
utils.AssertEqual(t, m.ShouldRespond, false)
}
}
// And the game expects an ack from the player
utils.AssertEqual(t, game.awaitingResponse, protocol.EndOfTurn)
previousPlayerID := game.currentPlayerID
// And when the player sends an ack
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.EndOfTurn,
}})
utils.AssertNoError(t, err)
// Then it's the next player's turn
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
utils.AssertTrue(t, previousPlayerID != game.currentPlayerID)
})
t.Run("stage 2: player has no legal moves and no hand cards", func(t *testing.T) {
// Given a game in stage 2, with a high-value card on the pile
highValueCard := deck.NewCard(deck.Ace, deck.Hearts)
pile := []deck.Card{highValueCard}
// And a player with an empty hand and a full set of seen cards
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
oldHandSize := len(game.playerCards[game.currentPlayerID].Hand)
oldPileSize := len(game.pile)
oldSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
// When the player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
newHandSize := len(game.playerCards[game.currentPlayerID].Hand)
newPileSize := len(game.pile)
newSeenSize := len(game.playerCards[game.currentPlayerID].Seen)
// then the current player's hand includes the cards from the pile
utils.AssertEqual(t, newHandSize, oldHandSize+oldPileSize)
// and the pile is now empty
utils.AssertEqual(t, newPileSize, 0)
// and the seen cards are the same
utils.AssertEqual(t, newSeenSize, oldSeenSize)
// then everyone is informed
utils.AssertTrue(t, len(msgs) > 0)
utils.AssertEqual(t, len(msgs), len(game.playerIDs))
// and the current player's OutboundMessage has the expected content
utils.AssertTrue(t, msgs[0].ShouldRespond)
utils.AssertEqual(t, msgs[0].Command, protocol.SkipTurn)
// and the other players' OutboundMessages have the expected content
utils.AssertEqual(t, msgs[1].ShouldRespond, false)
utils.AssertEqual(t, msgs[1].Command, protocol.SkipTurn)
// and the current player's response is handled correctly
previousPlayerID := game.currentPlayerID
response, err := game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.SkipTurn,
}})
utils.AssertNoError(t, err)
utils.AssertDeepEqual(t, response, []OutboundMessage(nil))
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
// and the next player is up
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
})
t.Run("stage 2: player only unseen cards", func(t *testing.T) {
// Given a game in stage 2, with a low-value card on the pile
lowValueCard := deck.NewCard(deck.Six, deck.Hearts)
pile := []deck.Card{lowValueCard}
// And a player with only a full set of Unseen cards
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{},
Unseen: []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
// When the player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
// Then everyone is informed
checkNextMessages(t, msgs, protocol.PlayUnseen, game)
// Then the game selects all Unseen cards (legal moves or not)
moves := getMoves(msgs, game.currentPlayerID)
utils.AssertTrue(t, len(moves) > 0)
utils.AssertDeepEqual(t, moves, []int{0, 1, 2})
oldUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
previousPlayerID := game.currentPlayerID
// And when the player selects a legal move
cardChoice := []int{moves[0]}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayUnseen,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.UnseenSuccess)
newHandSize := len(game.playerCards[game.currentPlayerID].Hand)
newUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
// Then they have one less Unseen card, but their remaining cards are unchanged
utils.AssertEqual(t, newUnseenSize, oldUnseenSize-1)
utils.AssertEqual(t, newHandSize, 0)
// And everyone is informed of the end of turn
checkReceiveResponseMessages(t, msgs, protocol.UnseenSuccess, game)
// And the game expects an ack from the player
_, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.UnseenSuccess,
}})
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
// And it's the next player's turn
utils.AssertTrue(t, previousPlayerID != game.currentPlayerID)
})
t.Run("stage 2: player has no legal moves and only unseen cards", func(t *testing.T) {
// Given a game in stage 2
highValueCard := deck.NewCard(deck.Ace, deck.Spades)
pile := []deck.Card{highValueCard}
// And a player with only a full set of Unseen cards
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{},
Unseen: []deck.Card{
deck.NewCard(deck.Eight, deck.Hearts),
deck.NewCard(deck.Nine, deck.Clubs),
deck.NewCard(deck.Six, deck.Diamonds),
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
// When the player takes their turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
// Then everyone is informed
checkNextMessages(t, msgs, protocol.PlayUnseen, game)
// Then the game selects all Unseen cards (legal moves or not)
moves := getMoves(msgs, game.currentPlayerID)
utils.AssertTrue(t, len(moves) > 0)
utils.AssertDeepEqual(t, moves, []int{0, 1, 2})
oldUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
oldPileSize := len(game.pile)
// And when the player selects an illegal move
cardChoice := []int{moves[0]}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayUnseen,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
newHand := game.playerCards[game.currentPlayerID].Hand
newUnseenSize := len(game.playerCards[game.currentPlayerID].Unseen)
// Then the player picks up the pile which includes the chosen Unseen card
utils.AssertEqual(t, len(game.pile), 0)
utils.AssertDeepEqual(t, len(newHand), oldPileSize+1)
utils.AssertEqual(t, newUnseenSize, oldUnseenSize-1)
checkReceiveResponseMessages(t, msgs, protocol.UnseenFailure, game)
// And the game is expecting an ack
utils.AssertEqual(t, game.awaitingResponse, protocol.UnseenFailure)
// And when the player's ack is received
previousPlayerID := game.currentPlayerID
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.UnseenFailure,
}})
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
// Then it's the next player's turn
utils.AssertTrue(t, previousPlayerID != game.currentPlayerID)
})
t.Run("stage 2: player finishes with final Unseen card", func(t *testing.T) {
// Given a game in stage 2
lowValueCard := deck.NewCard(deck.Four, deck.Spades)
highValueCard := deck.NewCard(deck.Ace, deck.Spades)
pile := []deck.Card{lowValueCard}
// And a player with one remaining Unseen card
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{},
Unseen: []deck.Card{
highValueCard,
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2", "player-3"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
"player-3": somePlayerCards(3),
},
})
// When the player takes a legal turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
previousPlayerID := game.currentPlayerID
previousNumPlayers := len(game.activePlayers)
cardChoice := []int{0}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayUnseen,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
// Then the game informs everyone the player has finished
checkPlayerFinishedMessages(t, msgs, game)
// And the game is expecting a response
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayerFinished)
// And when the player acks
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayerFinished,
}})
utils.AssertNoError(t, err)
// Then there is one less active players
utils.AssertEqual(t, len(game.activePlayers), previousNumPlayers-1)
utils.AssertTrue(t, sliceContainsString(game.finishedPlayers, previousPlayerID))
// And it's the next player's turn
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
})
t.Run("stage 2: player finishes with final Hand card", func(t *testing.T) {
// Given a game in stage 2
lowValueCard := deck.NewCard(deck.Four, deck.Spades)
highValueCard := deck.NewCard(deck.Ace, deck.Spades)
pile := []deck.Card{lowValueCard}
// And a player with one remaining Hand card and no Unseen cards
pc := &PlayerCards{
Hand: []deck.Card{highValueCard},
Seen: []deck.Card{},
Unseen: []deck.Card{},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2", "player-3"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
"player-3": somePlayerCards(3),
},
})
// When the player takes a legal turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayHand)
previousPlayerID := game.currentPlayerID
previousNumPlayers := len(game.activePlayers)
cardChoice := []int{0}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayHand,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
// Then the game informs everyone the player has finished
checkPlayerFinishedMessages(t, msgs, game)
// And the game is expecting a response
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayerFinished)
// And when the player acks
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayerFinished,
}})
utils.AssertNoError(t, err)
// Then there is one less active players
utils.AssertEqual(t, len(game.activePlayers), previousNumPlayers-1)
utils.AssertTrue(t, sliceContainsString(game.finishedPlayers, previousPlayerID))
// And it's the next player's turn
utils.AssertTrue(t, game.currentPlayerID != previousPlayerID)
utils.AssertEqual(t, game.awaitingResponse, protocol.Null)
})
t.Run("stage 2: game ends when n-1 players have finished (Unseen card)", func(t *testing.T) {
// Given a game in stage 2
lowValueCard := deck.NewCard(deck.Four, deck.Spades)
highValueCard := deck.NewCard(deck.Ace, deck.Spades)
pile := []deck.Card{lowValueCard}
// And a player with one remaining Unseen card
pc := &PlayerCards{
Hand: []deck.Card{},
Seen: []deck.Card{},
Unseen: []deck.Card{
highValueCard,
},
}
game := NewShed(ShedOpts{
stage: clearCards,
deck: deck.Deck{},
pile: pile,
playerIDs: []string{"player-1", "player-2"},
currentPlayerID: "player-1",
playerCards: map[string]*PlayerCards{
"player-1": pc,
"player-2": somePlayerCards(3),
},
})
// When the player takes a legal turn
msgs, err := game.Next()
utils.AssertNoError(t, err)
cardChoice := []int{0}
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayUnseen,
Decision: cardChoice,
}})
utils.AssertNoError(t, err)
// Then the game informs everyone the player has finished
checkPlayerFinishedMessages(t, msgs, game)
// And the game is expecting a response
utils.AssertEqual(t, game.awaitingResponse, protocol.PlayerFinished)
// And when the player acks
msgs, err = game.ReceiveResponse([]InboundMessage{{
PlayerID: game.currentPlayerID,
Command: protocol.PlayerFinished,
}})
utils.AssertNoError(t, err)
// Then the game informs everyone the game is over
checkGameOverMessages(t, msgs, game)
// And the game is NOT expecting a response