-
Notifications
You must be signed in to change notification settings - Fork 2
/
game_test.go
168 lines (132 loc) · 4.05 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
package rung_test
import (
"testing"
"github.com/minhajuddinkhan/pattay"
"github.com/minhajuddinkhan/rung"
"github.com/minhajuddinkhan/rung/dataset"
"github.com/stretchr/testify/assert"
)
func TestGame_HasFourPlayers(t *testing.T) {
game := rung.NewGame()
assert.Equal(t, len(game.Players()), 4)
}
func TestGame_EachPlayerHasZeroCardsBeforeDistribution(t *testing.T) {
game := rung.NewGame()
players := game.Players()
for _, player := range players {
assert.Equal(t, len(player.CardsAtHand()), 0)
}
}
func TestGame_EachPlayerHasThirteenCardsAfterDistribution(t *testing.T) {
game := rung.NewGame()
err := game.DistributeCards()
assert.Nil(t, err)
players := game.Players()
for _, p := range players {
assert.Equal(t, len(p.CardsAtHand()), 13)
}
}
func TestGame_NoTwoPlayersHaveSameCard(t *testing.T) {
game := rung.NewGame()
err := game.DistributeCards()
assert.Nil(t, err)
players := game.Players()
secondPlayer := players[1]
cardWithfirstPlayer := players[0].CardsAtHand()[0]
playerOneHasAceOfSpade := false
playerTwoHasAceOfSpade := false
for _, card := range secondPlayer.CardsAtHand() {
if card.House() == cardWithfirstPlayer.House() && cardWithfirstPlayer.Number() == pattay.Ace {
playerTwoHasAceOfSpade = true
}
}
assert.NotEqual(t, playerOneHasAceOfSpade, playerTwoHasAceOfSpade)
}
func TestGame_FirstHandMustHaveFourCards(t *testing.T) {
game := rung.NewGame()
game.ShuffleDeck(20)
assert.Nil(t, game.DistributeCards())
players := game.Players()
for _, p := range players {
for i, c := range p.CardsAtHand() {
if c.House() == pattay.Club {
p.ThrowCard(i)
break
}
}
}
handOutCome, err := game.PlayHand(0, nil, nil)
assert.Nil(t, err)
assert.Equal(t, len(handOutCome.Cards()), 4)
}
func TestGame_FirstHandMustHaveTwoOfClubs(t *testing.T) {
game := rung.NewGame()
game.ShuffleDeck(1)
game.DistributeCards()
p1, i1 := dataset.PlayerWithTwoOfClubs(game)
others := dataset.PLayersWithoutTwoOfClubs(game)
assert.True(t, p1.HasHouse(pattay.Club))
assert.True(t, others[0].HasHouse(pattay.Club))
assert.True(t, others[1].HasHouse(pattay.Club))
assert.True(t, others[2].HasHouse(pattay.Club))
p1.ThrowCard(i1)
for _, px := range others {
for j, c := range px.CardsAtHand() {
if c.House() == pattay.Club {
px.ThrowCard(j)
}
}
}
hand, err := game.PlayHand(0, nil, nil)
assert.Nil(t, err)
assert.Equal(t, len(hand.Cards()), 4)
has, _ := hand.HasCard(pattay.NewCard(pattay.Club, pattay.Two))
assert.True(t, has)
}
func TestGame_ConsecutiveHeadsPlayerShouldWinHandsAtTable(t *testing.T) {
game := rung.NewGame()
game.ShuffleDeck(20)
game.DistributeCards()
trump := pattay.Spade
players := game.Players()
var biggestPlayer pattay.Player
var spades []pattay.Card
for _, x := range players {
spade, at, err := x.AnySpade()
assert.Nil(t, err)
spades = append(spades, spade)
if spade.Number() == pattay.GetBiggestCard(spades, pattay.Spade).Number() {
biggestPlayer = x
}
x.ThrowCard(at)
}
hand, err := game.PlayHand(1, &trump, biggestPlayer)
assert.Nil(t, err)
player, err := hand.Head()
assert.Nil(t, err)
assert.Equal(t, player.Name(), biggestPlayer.Name())
assert.Equal(t, 0, len(game.HandsOnGround()), "Hands on ground should be zero after player has won hand")
assert.Equal(t, 1, game.HandsWonBy(biggestPlayer), "Hands won be player should be 1")
}
func TestTwelvthHandShouldNotAddWinToAnyPlayer(t *testing.T) {
game := rung.NewGame()
game.ShuffleDeck(20)
game.DistributeCards()
trump := pattay.Spade
biggestPlayerCard, aceAt := dataset.PlayerWithAceOfSpade(game)
assert.NotEqual(t, -1, aceAt)
others := dataset.PlayersWithoutAceOfSpade(game)
biggestPlayerCard.ThrowCard(aceAt)
for _, px := range others {
for j, c := range px.CardsAtHand() {
if c.House() == pattay.Spade {
px.ThrowCard(j)
}
}
}
_, err := game.PlayHand(11, &trump, biggestPlayerCard)
assert.Equal(t, 0, game.HandsWonBy(biggestPlayerCard))
assert.Nil(t, err)
}
//TEST Todo: throw a card from player that it doens't have. expect error
//TEST todo: create invalid player in the game and call next