-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMelds.go
More file actions
209 lines (190 loc) · 5.08 KB
/
Melds.go
File metadata and controls
209 lines (190 loc) · 5.08 KB
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
package main
import (
"reflect"
"sort"
"strconv"
)
// Protomeld - not quite a full meld, but container for candidate melds.
type Protomeld []Card
// Deadwood - cards that are not in a meld.
type Deadwood []Card
// Meld - Rummy standard melds. Must be at least three sequential cards of the
// same suit called a run (ex. 2H-3H-4H) or at least three cards of same rank called a set.
type Meld []Protomeld
// Melds - array of possible configuration of melds in a player's hand.
type Melds []Meld
// ContainsCard - checks and sees if the card is in the unmelded set.
func (d Deadwood) ContainsCard(card Card) bool {
for _, cardInDeadwood := range d {
if cardInDeadwood == card {
return true
}
}
return false
}
// CheckDeadwood - returns cards not in a meld configuration.
func (h *Hand) CheckDeadwood() (unmelded Deadwood) {
meldConfig := h.CheckMelds()
SEARCH_DEADWOOD:
for _, cardInHand := range *h {
for _, melds := range meldConfig {
for _, meld := range melds {
for _, cardInMeld := range meld {
if cardInHand == cardInMeld {
continue SEARCH_DEADWOOD
}
}
}
if !unmelded.ContainsCard(cardInHand) {
unmelded = append(unmelded, cardInHand)
}
}
}
return
}
// CheckMelds - gets all the possible melds that can be created with a hand.
func (h *Hand) CheckMelds() (melds Melds) {
seq := h.CheckMeldSeqFirst()
mult := h.CheckMeldMultFirst()
if reflect.DeepEqual(seq, mult) {
melds = append(melds, seq)
return
}
melds = append(melds, seq, mult)
return
}
// PrettyPrintMelds - pretty prints the melds and makes it readable.
func (m Melds) PrettyPrintMelds() (melds string) {
if reflect.DeepEqual(m, Melds{{}}) {
return "No melds in hand."
}
for index, i := range m {
melds += "Meld " + strconv.Itoa(index+1) + ": "
for _, j := range i {
for _, k := range j {
melds += k.rank + k.suit[:1] + " "
}
}
if index != len(m)-1 {
melds += "\n"
}
}
return
}
// SubsetOfMeld - checks if a meld being made is a subset of a previous meld
// made. Ex. 2C 3C 4C is a subset of 2C 3C 4C 5C 6C. This is a fixed length
// linear search.
func (m Protomeld) SubsetOfMeld(melds Meld) bool {
for _, ms := range melds {
for i := 0; i+len(m) <= len(ms); i++ {
subset := Protomeld{}
subset = ms[i : i+len(m)]
if reflect.DeepEqual(m, subset) {
return true
}
}
}
return false
}
// CheckMeldSeqFirst - checks the melds that can be made in the player's hand.
// This configuration checks for sequential melds first and does not store
// repeat cards.
func (h *Hand) CheckMeldSeqFirst() Meld {
melds := Meld{}
sort.Sort(ByValue(*h))
for i := 0; i < len(*h); i++ {
// Create a proto-meld. Since our hand is sorted by value,
// we can do a linear search and check by value to complete a meld.
meld := Protomeld{(*h)[i]}
for k, j := 1, i+1; j < len(*h); j++ {
// Search for the adjacent cards of ascending order.
if (*h)[i].value+k == (*h)[j].value {
if (*h)[i].suit == (*h)[j].suit {
meld = append(meld, (*h)[j])
k++
}
}
}
// If the length of the protomeld is less than 3, then it's not a meld.
if len(meld) >= 3 {
if !meld.SubsetOfMeld(melds) {
melds = append(melds, meld)
}
}
}
for i := 0; i < len(*h); i++ {
meld := Protomeld{(*h)[i]}
SEARCH:
for j := i + 1; j < len(*h); j++ {
// Search for the cards of the same value.
if (*h)[i].value == (*h)[j].value {
for _, m := range melds {
for _, n := range m {
if n == (*h)[j] {
continue SEARCH
}
}
}
meld = append(meld, (*h)[j])
}
}
// If the length of the protomeld is less than 3, then it's not a meld.
if len(meld) >= 3 {
if !meld.SubsetOfMeld(melds) {
melds = append(melds, meld)
}
}
}
return melds
}
// CheckMeldMultFirst - checks the melds that can be made in the player's hand.
// This configuration checks for card multiples melds first and does not store
// repeat cards.
func (h *Hand) CheckMeldMultFirst() Meld {
melds := Meld{}
sort.Sort(ByValue(*h))
for i := 0; i < len(*h); i++ {
meld := Protomeld{(*h)[i]}
for j := i + 1; j < len(*h); j++ {
// Search for the cards of the same value.
if (*h)[i].value == (*h)[j].value {
meld = append(meld, (*h)[j])
}
}
// If the length of the protomeld is less than 3, then it's not a meld.
if len(meld) >= 3 {
if !meld.SubsetOfMeld(melds) {
melds = append(melds, meld)
}
}
}
for i := 0; i < len(*h); i++ {
// Create a proto-meld. Since our hand is sorted by value,
// we can do a linear search and check by value to complete a meld.
meld := Protomeld{(*h)[i]}
SEARCH:
for k, j := 1, i+1; j < len(*h); j++ {
// Search for the adjacent cards of ascending order.
if (*h)[i].value+k == (*h)[j].value {
if (*h)[i].suit == (*h)[j].suit {
for _, m := range melds {
for _, n := range m {
if n == (*h)[j] {
continue SEARCH
}
}
}
meld = append(meld, (*h)[j])
k++
}
}
}
// If the length of the protomeld is less than 3, then it's not a meld.
if len(meld) >= 3 {
if !meld.SubsetOfMeld(melds) {
melds = append(melds, meld)
}
}
}
return melds
}