diff --git a/utils/combination/validator_options.go b/utils/combination/validator_options.go index 3be1105..94a3920 100644 --- a/utils/combination/validator_options.go +++ b/utils/combination/validator_options.go @@ -197,6 +197,40 @@ func WithValidatorHandleGroupContinuousN[T Item, E generic.Ordered, Index generi // - m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型需要全部相同 // - getType: 用于获取组合中元素的类型,用于判断是否相同 func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + if len(items) != n+m { + return false // 总数量不符合 n + m + } + + typeCount := make(map[E]int) + for _, item := range items { + t := getType(item) + typeCount[t]++ + } + + if len(typeCount) != 2 { + return false + } + + foundN := false + foundM := false + for _, count := range typeCount { + if count == n { + foundN = true + } else if count == m { + foundM = true + } + } + + return foundN && foundM + }) +} + +// WithValidatorHandleNCarryIndependentM 校验组合成员是否匹配 N 携带独立的 M 的组合 +// - n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同 +// - m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型无需全部相同 +// - getType: 用于获取组合中元素的类型,用于判断是否相同 +func WithValidatorHandleNCarryIndependentM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { return WithValidatorHandle[T](func(items []T) bool { if len(items) != n+m { return false // 总数量不符合 n + m @@ -230,37 +264,3 @@ func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n, m int, getType fun return true // 符合 N 带 M 的条件 }) } - -// WithValidatorHandleNCarryIndependentM 校验组合成员是否匹配 N 携带独立的 M 的组合 -// - n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同 -// - m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型无需全部相同 -// - getType: 用于获取组合中元素的类型,用于判断是否相同 -func WithValidatorHandleNCarryIndependentM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { - return WithValidatorHandle[T](func(items []T) bool { - if len(items) != n+m { - return false // 总数量不符合 n + m - } - - typeCount := make(map[E]int) - for _, item := range items { - t := getType(item) - typeCount[t]++ - } - - if len(typeCount) != 2 { - return false - } - - foundN := false - foundM := false - for _, count := range typeCount { - if count == n { - foundN = true - } else if count == m { - foundM = true - } - } - - return foundN && foundM - }) -} diff --git a/utils/combination/validator_test.go b/utils/combination/validator_test.go new file mode 100644 index 0000000..f0a4ee9 --- /dev/null +++ b/utils/combination/validator_test.go @@ -0,0 +1,42 @@ +package combination_test + +import ( + "fmt" + "github.com/kercylan98/minotaur/utils/combination" + "github.com/kercylan98/minotaur/utils/super" + "testing" +) + +type Card struct { + Point string + Color string +} + +func TestValidator_Validate(t *testing.T) { + v := combination.NewValidator[*Card]( + combination.WithValidatorHandleContinuous[*Card, int](func(item *Card) int { + switch item.Point { + case "A": + return 1 + case "2", "3", "4", "5", "6", "7", "8", "9", "10": + return super.StringToInt(item.Point) + case "J": + return 11 + case "Q": + return 12 + case "K": + return 13 + } + return -1 + }), + combination.WithValidatorHandleLength[*Card](3), + ) + + cards := []*Card{ + {Point: "2", Color: "Spade"}, + {Point: "4", Color: "Heart"}, + {Point: "3", Color: "Diamond"}, + } + + fmt.Println(v.Validate(cards)) +}