joker-cribbage/score_result.go

48 lines
983 B
Go

package cribbage
import (
"fmt"
. "git.sr.ht/~tslocum/cards"
)
// ScoreResult is a score from pegging or showing a hand.
type ScoreResult struct {
Type ScoreType
Cards 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
}