Cribbage scoring library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
998 B

package cribbage
import (
"fmt"
"code.rocketnine.space/tslocum/joker"
)
// ScoreResult is a score from pegging or showing a hand.
type ScoreResult struct {
Type ScoreType
Cards joker.Cards
Points int
}
func (r ScoreResult) String() string {
return fmt.Sprintf("%s for %d with %s", r.Type, r.Points, r.Cards)
}
// ScoreResults is a slice of scores from pegging or showing a hand.
type ScoreResults []ScoreResult
func (r ScoreResults) Len() int {
return len(r)
}
func (r ScoreResults) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r ScoreResults) Less(i, j int) bool {
if r[i].Type != r[j].Type {
return r[i].Type < r[j].Type
} else if r[i].Points != r[j].Points {
return r[i].Points < r[j].Points
} else if len(r[i].Cards) != len(r[j].Cards) {
return len(r[i].Cards) < len(r[j].Cards)
}
for k := len(r[i].Cards) - 1; k >= 0; k-- {
if r[i].Cards[k].Value() != r[j].Cards[k].Value() {
return r[i].Cards[k].Value() < r[j].Cards[k].Value()
}
}
return i < j
}