...

Source file src/code.rocketnine.space/tslocum/joker-cribbage/score_result.go

Documentation: code.rocketnine.space/tslocum/joker-cribbage

     1  package cribbage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.rocketnine.space/tslocum/joker"
     7  )
     8  
     9  // ScoreResult is a score from pegging or showing a hand.
    10  type ScoreResult struct {
    11  	Type   ScoreType
    12  	Cards  joker.Cards
    13  	Points int
    14  }
    15  
    16  func (r ScoreResult) String() string {
    17  	return fmt.Sprintf("%s for %d with %s", r.Type, r.Points, r.Cards)
    18  }
    19  
    20  // ScoreResults is a slice of scores from pegging or showing a hand.
    21  type ScoreResults []ScoreResult
    22  
    23  func (r ScoreResults) Len() int {
    24  	return len(r)
    25  }
    26  
    27  func (r ScoreResults) Swap(i, j int) {
    28  	r[i], r[j] = r[j], r[i]
    29  }
    30  
    31  func (r ScoreResults) Less(i, j int) bool {
    32  	if r[i].Type != r[j].Type {
    33  		return r[i].Type < r[j].Type
    34  	} else if r[i].Points != r[j].Points {
    35  		return r[i].Points < r[j].Points
    36  	} else if len(r[i].Cards) != len(r[j].Cards) {
    37  		return len(r[i].Cards) < len(r[j].Cards)
    38  	}
    39  
    40  	for k := len(r[i].Cards) - 1; k >= 0; k-- {
    41  		if r[i].Cards[k].Value() != r[j].Cards[k].Value() {
    42  			return r[i].Cards[k].Value() < r[j].Cards[k].Value()
    43  		}
    44  	}
    45  
    46  	return i < j
    47  }
    48  

View as plain text