Skip to content

Commit 107eeb6

Browse files
Add test case, fix bug
We were allowed to require more wildcards than cards, erroneously returning a negative value for `more` instead of failing the match. This did not affect the actual test case that was added, but I noticed the discrepancy in the logs. It only affected one existing test case which was wrong lol.
1 parent 1953e3e commit 107eeb6

File tree

2 files changed

+90
-62
lines changed

2 files changed

+90
-62
lines changed

pkg/games/gin_solver.go

+3
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ func (gs *GinSolver) WcRun(hand []Card, cards []int) Interval {
442442
if !gs.MostlyWildGroups && (len(cards)-min < more) {
443443
more = len(cards) - min
444444
}
445+
if more < 0 {
446+
return none
447+
}
445448

446449
return Interval{min, more}
447450
}

pkg/games/gin_solver_test.go

+87-62
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package games
22

33
import (
4-
//"reflect"
5-
//"sort"
4+
"reflect"
5+
"sort"
66
"testing"
77
)
88

@@ -671,7 +671,7 @@ var GroupTestCases = []ValidGroupEntry{
671671
Card{0, NoneSuit, JokerRank},
672672
Card{0, SpadesSuit, TwoRank},
673673
},
674-
IsRun: true,
674+
IsRun: false,
675675
IsKind: false,
676676
},
677677
GroupEntry{
@@ -1005,6 +1005,29 @@ var HandTestCases = []ValidHandEntry{
10051005
},
10061006
},
10071007
},
1008+
ValidHandEntry{
1009+
Solver: setAceLow(true, threeThirteenSolver(QueenRank)),
1010+
Entries: []HandEntry{
1011+
HandEntry{
1012+
Hand: []Card{
1013+
Card{107, ClubsSuit, AceRank}, // 0
1014+
Card{3, DiamondsSuit, AceRank}, // 1
1015+
Card{62, SpadesSuit, ThreeRank}, // 2
1016+
Card{83, ClubsSuit, ThreeRank}, // 3
1017+
Card{55, SpadesSuit, FourRank}, // 4
1018+
Card{102, SpadesSuit, EightRank}, // 5
1019+
Card{67, HeartsSuit, KingRank}, // 6
1020+
1021+
Card{1, HeartsSuit, QueenRank}, // 7
1022+
Card{2, SpadesSuit, QueenRank}, // 8
1023+
Card{3, DiamondsSuit, QueenRank}, // 9
1024+
Card{4, ClubsSuit, QueenRank}, // 10
1025+
Card{0, FancySuit, JokerRank}, // 11
1026+
},
1027+
Score: 5,
1028+
},
1029+
},
1030+
},
10081031
}
10091032

10101033
func TestIsValidGroup(t *testing.T) {
@@ -1040,71 +1063,72 @@ func TestIsValidGroup(t *testing.T) {
10401063
}
10411064
*/
10421065
// base_groups [[17 12 11 10 16] [14 15 13]] hand [Card{40, DiamondsSuit, ThreeRank} Card{54, HeartsSuit, JackRank} Card{21, DiamondsSuit, TenRank} Card{12, SpadesSuit, JackRank} Card{38, DiamondsSuit, SixRank} Card{29, SpadesSuit, EightRank} Card{28, SpadesSuit, SixRank} Card{5, DiamondsSuit, NineRank} Card{27, SpadesSuit, TenRank} Card{34, ClubsSuit, JackRank} Card{32, ClubsSuit, TenRank} Card{45, FancySuit, JokerRank} Card{31, ClubsSuit, EightRank} Card{48, DiamondsSuit, KingRank} Card{36, ClubsSuit, KingRank} Card{10, HeartsSuit, KingRank} Card{43, ClubsSuit, SixRank} Card{35, ClubsSuit, NineRank}] groups [[35 31 45 32 43] [36 10 48]]
1043-
base_groups := [][]int{[]int{17, 12, 11, 10, 16}, []int{14, 15, 13}}
1044-
hand := []Card{
1045-
Card{40, DiamondsSuit, ThreeRank},
1046-
Card{54, HeartsSuit, JackRank},
1047-
Card{21, DiamondsSuit, TenRank},
1048-
Card{12, SpadesSuit, JackRank},
1049-
Card{38, DiamondsSuit, SixRank},
1050-
Card{29, SpadesSuit, EightRank},
1051-
Card{28, SpadesSuit, SixRank},
1052-
Card{5, DiamondsSuit, NineRank},
1053-
Card{27, SpadesSuit, TenRank},
1054-
Card{34, ClubsSuit, JackRank},
1066+
/*
1067+
base_groups := [][]int{[]int{17, 12, 11, 10, 16}, []int{14, 15, 13}}
1068+
hand := []Card{
1069+
Card{40, DiamondsSuit, ThreeRank},
1070+
Card{54, HeartsSuit, JackRank},
1071+
Card{21, DiamondsSuit, TenRank},
1072+
Card{12, SpadesSuit, JackRank},
1073+
Card{38, DiamondsSuit, SixRank},
1074+
Card{29, SpadesSuit, EightRank},
1075+
Card{28, SpadesSuit, SixRank},
1076+
Card{5, DiamondsSuit, NineRank},
1077+
Card{27, SpadesSuit, TenRank},
1078+
Card{34, ClubsSuit, JackRank},
10551079
1056-
Card{32, ClubsSuit, TenRank}, // 10
1057-
Card{45, FancySuit, JokerRank},
1058-
Card{31, ClubsSuit, EightRank},
1080+
Card{32, ClubsSuit, TenRank}, // 10
1081+
Card{45, FancySuit, JokerRank},
1082+
Card{31, ClubsSuit, EightRank},
10591083
1060-
Card{48, DiamondsSuit, KingRank},
1061-
Card{36, ClubsSuit, KingRank},
1062-
Card{10, HeartsSuit, KingRank},
1084+
Card{48, DiamondsSuit, KingRank},
1085+
Card{36, ClubsSuit, KingRank},
1086+
Card{10, HeartsSuit, KingRank},
10631087
1064-
Card{43, ClubsSuit, SixRank},
1065-
Card{35, ClubsSuit, NineRank},
1066-
}
1067-
t.Log("min score", defaultSolver.MinScoreBelowUsing(hand, 99, base_groups))
1068-
/*
1069-
return
1070-
for _, tc := range GroupTestCases {
1071-
for _, entry := range tc.Entries {
1072-
group := entry.Group
1073-
cards := make([]int, len(group))
1074-
for index := range group {
1075-
cards[index] = index
1076-
}
1077-
actual_group := (&tc.Solver).IsValidGroup(group, cards)
1078-
actual_run := (&tc.Solver).IsRun(group, cards)
1079-
actual_kind := (&tc.Solver).IsKind(group, cards)
1088+
Card{43, ClubsSuit, SixRank},
1089+
Card{35, ClubsSuit, NineRank},
1090+
}
1091+
t.Log("min score", defaultSolver.MinScoreBelowUsing(hand, 99, base_groups))
1092+
*/
10801093

1081-
if actual_group != (entry.IsRun || entry.IsKind) || actual_run != entry.IsRun || actual_kind != entry.IsKind {
1082-
t.Error("ERROR Expected:", entry.IsRun || entry.IsKind, entry.IsRun, entry.IsKind, "got:", actual_group, actual_run, actual_kind, "\nfor group\n", group, "\nand solver\n", tc.Solver)
1083-
}
1084-
actual_score := (&tc.Solver).MinScoreBelow(group, 100)
1085-
if (entry.IsRun || entry.IsKind) == (actual_score != 0) {
1086-
if len(group) > 10 {
1087-
continue
1088-
}
1089-
//m := (&tc.Solver).AllMatches(group, cards)
1090-
//if len(m) < 12 {
1091-
// t.Log("All matches", m)
1092-
//}
1093-
t.Log("Mostly", tc.Solver.MostlyWildGroups, "all", tc.Solver.AllWildGroups)
1094-
t.Error("ERROR Expected:", (entry.IsRun || entry.IsKind), "got:", actual_score, "\nfor group\n", group, "\nand solver\n", tc.Solver)
1094+
for _, tc := range GroupTestCases {
1095+
for _, entry := range tc.Entries {
1096+
group := entry.Group
1097+
cards := make([]int, len(group))
1098+
for index := range group {
1099+
cards[index] = index
1100+
}
1101+
actual_group := (&tc.Solver).IsValidGroup(group, cards)
1102+
actual_run := (&tc.Solver).IsRun(group, cards)
1103+
actual_kind := (&tc.Solver).IsKind(group, cards)
1104+
1105+
if actual_group != (entry.IsRun || entry.IsKind) || actual_run != entry.IsRun || actual_kind != entry.IsKind {
1106+
t.Error("ERROR Expected:", entry.IsRun || entry.IsKind, entry.IsRun, entry.IsKind, "got:", actual_group, actual_run, actual_kind, "\nfor group\n", group, "\nand solver\n", tc.Solver)
1107+
}
1108+
actual_score := (&tc.Solver).MinScoreBelow(group, 100)
1109+
if (entry.IsRun || entry.IsKind) == (actual_score != 0) {
1110+
if len(group) > 10 {
1111+
continue
10951112
}
1113+
//m := (&tc.Solver).AllMatches(group, cards)
1114+
//if len(m) < 12 {
1115+
// t.Log("All matches", m)
1116+
//}
1117+
t.Log("Mostly", tc.Solver.MostlyWildGroups, "all", tc.Solver.AllWildGroups)
1118+
t.Error("ERROR Expected:", (entry.IsRun || entry.IsKind), "got:", actual_score, "\nfor group\n", group, "\nand solver\n", tc.Solver)
10961119
}
10971120
}
1098-
for _, tc := range HandTestCases {
1099-
for _, entry := range tc.Entries {
1100-
group := entry.Hand
1101-
cards := make([]int, len(group))
1102-
regular := make([]int, 0, len(group))
1103-
for index, card := range group {
1104-
cards[index] = index
1105-
if !(&tc.Solver).IsWildCard(card) {
1106-
regular = append(regular, index)
1107-
}
1121+
}
1122+
1123+
for _, tc := range HandTestCases {
1124+
for _, entry := range tc.Entries {
1125+
group := entry.Hand
1126+
cards := make([]int, len(group))
1127+
regular := make([]int, 0, len(group))
1128+
for index, card := range group {
1129+
cards[index] = index
1130+
if !(&tc.Solver).IsWildCard(card) {
1131+
regular = append(regular, index)
11081132
}
11091133
wc := len(cards) - len(regular)
11101134

@@ -1167,5 +1191,6 @@ func TestIsValidGroup(t *testing.T) {
11671191
t.Error("ERROR Expected:", entry.Score, "got:", actual_score, "\nfor group\n", entry.Hand, "\nand solver\n", tc.Solver)
11681192
}
11691193
}
1170-
}*/
1194+
}
1195+
}
11711196
}

0 commit comments

Comments
 (0)