/
variants.go
266 lines (237 loc) · 8.3 KB
/
variants.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
package main
import (
"encoding/json"
"io/ioutil"
"path"
"strconv"
"strings"
)
var (
variants map[string]*Variant
variantIDMap map[int]string
variantNames []string
)
// VariantJSON is very similar to Variant,
// but the latter is comprised of some more complicated objects
type VariantJSON struct {
Name string `json:"name"`
ID int `json:"id"`
Suits []string `json:"suits"`
// ClueColors and ClueRanks are optional elements
// Thus, they must be pointers so that we can tell if the values were specified or not
ClueColors *[]string `json:"clueColors"`
ClueRanks *[]int `json:"clueRanks"`
ColorCluesTouchNothing bool `json:"colorCluesTouchNothing"`
RankCluesTouchNothing bool `json:"rankCluesTouchNothing"`
SpecialRank int `json:"specialRank"` // For e.g. Rainbow-Ones
SpecialAllClueColors bool `json:"specialAllClueColors"`
SpecialAllClueRanks bool `json:"specialAllClueRanks"`
SpecialNoClueColors bool `json:"specialNoClueColors"`
SpecialNoClueRanks bool `json:"specialNoClueRanks"`
SpecialDeceptive bool `json:"specialDeceptive"`
}
func variantsInit() {
// Import the JSON file
filePath := path.Join(dataPath, "variants.json")
var fileContents []byte
if v, err := ioutil.ReadFile(filePath); err != nil {
logger.Fatal("Failed to read the \"" + filePath + "\" file: " + err.Error())
return
} else {
fileContents = v
}
var variantsArray []VariantJSON
if err := json.Unmarshal(fileContents, &variantsArray); err != nil {
logger.Fatal("Failed to convert the variants file to JSON: " + err.Error())
return
}
// Convert the array to a map
variants = make(map[string]*Variant)
variantIDMap = make(map[int]string)
variantNames = make([]string, 0)
for _, variant := range variantsArray {
// Validate the name
if variant.Name == "" {
logger.Fatal("There is a variant with an empty name in the \"variants.json\" file.")
}
// Validate the ID
if variant.ID < 0 { // The first variant has an ID of 0
logger.Fatal("The \"" + variant.Name + "\" variant has an invalid ID.")
}
// Validate that all of the names are unique
if _, ok := variants[variant.Name]; ok {
logger.Fatal("There are two variants with the name of \"" + variant.Name + "\".")
return
}
// Validate that there is at least one suit
if len(variant.Suits) < 1 {
logger.Fatal("The variant of \"" + variant.Name + "\" does not have at least one suit.")
return
}
// Validate that all of the suits exist and convert suit strings to objects
variantSuits := make([]*Suit, 0)
for _, suitName := range variant.Suits {
if suit, ok := suits[suitName]; !ok {
logger.Fatal("The suit of \"" + suitName + "\" " +
"in variant \"" + variant.Name + "\" does not exist.")
return
} else {
variantSuits = append(variantSuits, suit)
}
}
// Derive the card ranks (the ranks that the cards of each suit will be)
// By default, assume ranks 1 through 5
variantRanks := []int{1, 2, 3, 4, 5}
if strings.HasPrefix(variant.Name, "Up or Down") {
// The "Up or Down" variants have START cards
// ("startCardRank" is defined in the "variantUpOrDown.go" file)
variantRanks = append(variantRanks, StartCardRank)
}
// Validate or derive the clue colors (the colors available to clue in this variant)
clueColors := variant.ClueColors
if clueColors == nil {
// The clue colors were not specified in the JSON, so derive them from the suits
derivedClueColors := make([]string, 0)
for _, suit := range variantSuits {
if suit.AllClueColors {
// If a suit is touched by all colors,
// then we don't want to add every single clue color to the variant clue list
continue
}
for _, color := range suit.ClueColors {
if !stringInSlice(color, derivedClueColors) {
derivedClueColors = append(derivedClueColors, color)
}
}
}
clueColors = &derivedClueColors
} else {
// The clue colors were specified in the JSON, so validate that they map to colors
for _, colorName := range *variant.ClueColors {
if _, ok := colors[colorName]; !ok {
logger.Fatal("The variant of \"" + variant.Name + "\" has a clue color of " +
"\"" + colorName + "\", but that color does not exist.")
return
}
}
}
// Validate or derive the clue ranks (the ranks available to clue in this variant)
clueRanks := variant.ClueRanks
if clueRanks == nil {
// The clue ranks were not specified in the JSON,
// so just assume that we can clue ranks 1 through 5
clueRanks = &[]int{1, 2, 3, 4, 5}
}
// The default value of "SpecialRank" is -1, not 0
specialRank := variant.SpecialRank
if specialRank == 0 {
specialRank = -1
}
// Convert the JSON variant into a variant object and store it in the map
variants[variant.Name] = &Variant{
Name: variant.Name,
ID: variant.ID,
Suits: variantSuits,
Ranks: variantRanks,
ClueColors: *clueColors,
ClueRanks: *clueRanks,
ColorCluesTouchNothing: variant.ColorCluesTouchNothing,
RankCluesTouchNothing: variant.RankCluesTouchNothing,
SpecialRank: specialRank,
SpecialAllClueColors: variant.SpecialAllClueColors,
SpecialAllClueRanks: variant.SpecialAllClueRanks,
SpecialNoClueColors: variant.SpecialNoClueColors,
SpecialNoClueRanks: variant.SpecialNoClueRanks,
SpecialDeceptive: variant.SpecialDeceptive,
MaxScore: len(variantSuits) * 5,
// (we assume that there are 5 points per stack)
}
// Validate that all of the ID's are unique
// And create a reverse mapping of ID to name
// (so that we can easily find the associated variant from a database entry)
if _, ok := variantIDMap[variant.ID]; ok {
logger.Fatal("There are two variants with the ID of " +
"\"" + strconv.Itoa(variant.ID) + "\".")
return
}
variantIDMap[variant.ID] = variant.Name
// Create an array with every variant name
variantNames = append(variantNames, variant.Name)
}
// Validate that there are no skipped ID numbers
// (commented out for now since we have deleted some variants in the last round of changes)
/*
for i := 0; i < len(variantNames); i++ {
if _, ok := variantIDMap[i]; !ok {
logger.Fatal("There is no variant with an ID of \"" + strconv.Itoa(i) + "\". " +
"(Variant IDs must be sequential.)")
return
}
}
*/
}
// variantIsCardTouched returns true if a clue will touch a particular suit
// For example, a yellow clue will not touch a green card in a normal game,
// but it will the "Dual-Color" variant
// This mirrors the function "touchesCard()" in "clues.ts"
func variantIsCardTouched(variantName string, clue Clue, card *Card) bool {
variant := variants[variantName]
suit := variant.Suits[card.SuitIndex]
if clue.Type == ClueTypeColor {
if variant.ColorCluesTouchNothing {
return false
}
if suit.AllClueColors {
return true
}
if suit.NoClueColors {
return false
}
if variant.SpecialRank == card.Rank {
if variant.SpecialAllClueColors {
return true
}
if variant.SpecialNoClueColors {
return false
}
}
clueColorName := variant.ClueColors[clue.Value]
if suit.Prism {
// The color that touches a prism card is contingent upon the card's rank
prismColorIndex := (card.Rank - 1) % len(variant.ClueColors)
if card.Rank == StartCardRank {
// "START" cards count as rank 0, so they are touched by the final color
prismColorIndex = len(variant.ClueColors) - 1
}
prismColorName := variant.ClueColors[prismColorIndex]
return clueColorName == prismColorName
}
return stringInSlice(clueColorName, suit.ClueColors)
}
if clue.Type == ClueTypeRank {
if variant.RankCluesTouchNothing {
return false
}
if variant.Suits[card.SuitIndex].AllClueRanks {
return true
}
if variant.Suits[card.SuitIndex].NoClueRanks {
return false
}
if variant.SpecialRank == card.Rank {
if variant.SpecialAllClueRanks {
return true
}
if variant.SpecialNoClueRanks {
return false
}
if variant.SpecialDeceptive {
// The rank that touches a deceptive card is contingent upon the card's suit
deceptiveRank := variant.ClueRanks[card.SuitIndex%len(variant.ClueRanks)]
return clue.Value == deceptiveRank
}
}
return clue.Value == card.Rank
}
return false
}