forked from jbowens/codenames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_test.go
212 lines (186 loc) · 4.69 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
package codenames
import (
"encoding/json"
"fmt"
"testing"
"github.com/jbowens/dictionary"
)
var testWords []string
func init() {
d, err := dictionary.Load("assets/original.txt")
if err != nil {
panic(err)
}
testWords = d.Words()
}
func BenchmarkGameMarshal(b *testing.B) {
b.StopTimer()
d, err := dictionary.Load("assets/original.txt")
if err != nil {
b.Fatal(err)
}
g := newGame("foo", GameState{
Seed: 1,
Round: 0,
Revealed: make([]bool, 25),
WordSet: d.Words(),
}, GameOptions{RandomWords: true})
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err = json.Marshal(g)
if err != nil {
b.Fatal(err)
}
}
}
func TestGameShuffle(t *testing.T) {
gamesWithoutRepeats := len(testWords)/25 - 1
initialState := randomState(testWords)
currState := initialState
m := map[string]int{}
for i := 0; i < gamesWithoutRepeats; i++ {
g := newGame("foo", currState, GameOptions{RandomWords: true})
for _, w := range g.Words {
if prevI, ok := m[w]; ok {
t.Errorf("Word %q appeared twice, once in game %d and once in game %d.", w, prevI, i)
}
m[w] = i
}
currState = nextGameState(currState)
}
}
func TestGameSetUp(t *testing.T) {
d, err := dictionary.Load("assets/original.txt")
if err != nil {
t.Fatal(err)
}
initG := newGame("foo", GameState{
Seed: 1,
Round: 0,
Revealed: make([]bool, 25),
WordSet: d.Words(),
}, GameOptions{})
if initG.Stage != Setup {
t.Errorf("Failed")
}
}
func TestRandomWordsSetup(t *testing.T) {
d, err := dictionary.Load("assets/original.txt")
if err != nil {
t.Fatal(err)
}
initG := newGame("foo", GameState{
Seed: 1,
Round: 0,
Revealed: make([]bool, 25),
WordSet: d.Words(),
}, GameOptions{RandomWords: true})
if initG.Stage != Setup {
t.Errorf("Failed")
}
if len(initG.Words) != 25 {
t.Errorf("Not enough words to play with")
}
}
func TestGetNextWord(t *testing.T) {
initG := newGame("foo", GameState{
Seed: 1,
Round: 0,
Revealed: make([]bool, 0),
WordSet: make([]string, 0),
}, GameOptions{})
initG.AddWord("bar")
initG.AddWord("foobar")
initG.GetNextWord(false)
if initG.CurrentWord == "" {
t.Errorf("Current Word not set")
}
initG.GetNextWord(true)
if len(initG.getAvailableWords()) != 1 {
t.Errorf("Correct word did not get taken out of pool")
}
initG.GetNextWord(true)
if initG.Stage != Explain {
t.Errorf("Stage not extended")
}
}
func TestPlayerRouting(t *testing.T) {
g := newGame("scoring", GameState{
Seed: 1,
Round: 0,
Revealed: make([]bool, 0),
WordSet: make([]string, 25),
}, GameOptions{RandomWords: true})
playerA := TeamPlayer{Team: 1, PlayerName: "A"}
player1 := TeamPlayer{Team: 0, PlayerName: "1"}
playerB := TeamPlayer{Team: 1, PlayerName: "B"}
player2 := TeamPlayer{Team: 0, PlayerName: "2"}
player3 := TeamPlayer{Team: 0, PlayerName: "3"}
g.AddPlayer(playerA)
g.AddPlayer(player1)
if g.RoutingOrder[0] != playerA {
t.Errorf("Wrong Routing Order")
}
if g.RoutingOrder[1] != player1 {
t.Errorf("Wrong Routing Order")
}
g.AddPlayer(playerB)
g.AddPlayer(player2)
if g.RoutingOrder[2] != playerB {
fmt.Println("Player: " + g.RoutingOrder[2].PlayerName)
t.Errorf("Wrong Routing Order")
}
g.AddPlayer(player3)
if g.RoutingOrder[5] != player3 {
t.Errorf("Wrong Routing Order")
}
if g.RoutingOrder[g.CurrentPlayer] != playerA {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer()
if g.RoutingOrder[g.CurrentPlayer] != player1 {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer()
if g.RoutingOrder[g.CurrentPlayer] != playerB {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer()
if g.RoutingOrder[g.CurrentPlayer] != player2 {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer()
if g.RoutingOrder[g.CurrentPlayer] != playerA {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer()
if g.RoutingOrder[g.CurrentPlayer] != player3 {
t.Errorf("Wrong player in rotation")
}
g.getNextPlayer() // B
g.getNextPlayer() // 1
g.getNextPlayer() // A
g.getNextPlayer() // 2
g.getNextPlayer() // B
g.getNextPlayer() // 3
g.getNextPlayer() // A
// Get next player properly loops over all players in the round
if g.RoutingOrder[g.CurrentPlayer] != playerA {
t.Errorf("Wrong player in rotation")
}
if g.StartingTeam != g.RoutingOrder[0].Team {
t.Errorf("Starting team not in sync with team rotation")
}
if g.TeamPlayers[3] != player2 {
t.Errorf("Player2 is in wrong place")
}
g.DeletePlayer("2")
if g.TeamPlayers[3] == player2 {
t.Errorf("Player not properly removed")
}
g.ChangePlayerTeam("A", 0)
// Diff memory allocation because re-creating the player
if g.TeamPlayers[len(g.TeamPlayers)-1].PlayerName != playerA.PlayerName {
t.Errorf("Player not re-added in last place")
}
}