Rename library from cards-cribbage to joker-cribbage
This commit is contained in:
parent
b3d36a72d0
commit
2a9990d38b
|
@ -1,13 +1,13 @@
|
|||
arch: amd64
|
||||
environment:
|
||||
PROJECT_NAME: 'cards-cribbage'
|
||||
PROJECT_NAME: 'joker-cribbage'
|
||||
CGO_ENABLED: '1'
|
||||
GO111MODULE: 'on'
|
||||
image: freebsd/latest
|
||||
packages:
|
||||
- go
|
||||
sources:
|
||||
- https://git.sr.ht/~tslocum/cards-cribbage
|
||||
- https://git.sr.ht/~tslocum/joker-cribbage
|
||||
tasks:
|
||||
- test: |
|
||||
cd $PROJECT_NAME
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
arch: x86_64
|
||||
environment:
|
||||
PROJECT_NAME: 'cards-cribbage'
|
||||
PROJECT_NAME: 'joker-cribbage'
|
||||
CGO_ENABLED: '1'
|
||||
GO111MODULE: 'on'
|
||||
image: alpine/edge
|
||||
packages:
|
||||
- go
|
||||
sources:
|
||||
- https://git.sr.ht/~tslocum/cards-cribbage
|
||||
- https://git.sr.ht/~tslocum/joker-cribbage
|
||||
tasks:
|
||||
- test: |
|
||||
cd $PROJECT_NAME
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
0.1.1:
|
||||
- Rename library from cards-cribbage to joker-cribbage
|
||||
|
||||
0.1.0:
|
||||
- Initial release
|
||||
|
|
14
README.md
14
README.md
|
@ -1,17 +1,17 @@
|
|||
# cards-cribbage
|
||||
[![GoDoc](https://godoc.org/git.sr.ht/~tslocum/cards-cribbage?status.svg)](https://godoc.org/git.sr.ht/~tslocum/cards-cribbage)
|
||||
[![builds.sr.ht status](https://builds.sr.ht/~tslocum/cards-cribbage.svg)](https://builds.sr.ht/~tslocum/cards-cribbage)
|
||||
# joker-cribbage
|
||||
[![GoDoc](https://godoc.org/git.sr.ht/~tslocum/joker-cribbage?status.svg)](https://godoc.org/git.sr.ht/~tslocum/joker-cribbage)
|
||||
[![builds.sr.ht status](https://builds.sr.ht/~tslocum/joker-cribbage.svg)](https://builds.sr.ht/~tslocum/joker-cribbage)
|
||||
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
|
||||
|
||||
Cribbage scoring library
|
||||
Cribbage scoring library based on [joker](https://git.sr.ht/~tslocum/joker)
|
||||
|
||||
## Documentation
|
||||
|
||||
Documentation is available via [godoc](https://godoc.org):
|
||||
|
||||
- [cards](https://godoc.org/git.sr.ht/~tslocum/cards)
|
||||
- [cards-cribbage](https://godoc.org/git.sr.ht/~tslocum/cards-cribbage)
|
||||
- [joker](https://godoc.org/git.sr.ht/~tslocum/joker)
|
||||
- [joker-cribbage](https://godoc.org/git.sr.ht/~tslocum/joker-cribbage)
|
||||
|
||||
## Support
|
||||
|
||||
Please share issues/suggestions [here](https://todo.sr.ht/~tslocum/cards-cribbage).
|
||||
Please share issues/suggestions [here](https://todo.sr.ht/~tslocum/joker).
|
||||
|
|
4
card.go
4
card.go
|
@ -1,9 +1,9 @@
|
|||
package cribbage
|
||||
|
||||
import "git.sr.ht/~tslocum/cards"
|
||||
import . "git.sr.ht/~tslocum/joker"
|
||||
|
||||
// Value returns the cribbage value of a card.
|
||||
func Value(c cards.Card) int {
|
||||
func Value(c Card) int {
|
||||
v := int(c.Face)
|
||||
if v > 10 {
|
||||
v = 10
|
||||
|
|
8
cards.go
8
cards.go
|
@ -1,9 +1,9 @@
|
|||
package cribbage
|
||||
|
||||
import "git.sr.ht/~tslocum/cards"
|
||||
import . "git.sr.ht/~tslocum/joker"
|
||||
|
||||
// Sum returns the total cribbage value of the supplied cards.
|
||||
func Sum(c cards.Cards) int {
|
||||
func Sum(c Cards) int {
|
||||
var v int
|
||||
for _, card := range c {
|
||||
v += Value(card)
|
||||
|
@ -12,7 +12,7 @@ func Sum(c cards.Cards) int {
|
|||
}
|
||||
|
||||
func compareCards(i, j interface{}) bool {
|
||||
icard := i.(cards.Card)
|
||||
jcard := j.(cards.Card)
|
||||
icard := i.(Card)
|
||||
jcard := j.(Card)
|
||||
return icard.Value() < jcard.Value()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
Package cribbage provides cribbage scoring.
|
||||
|
||||
Cards
|
||||
|
||||
To retrieve the cribbage value of a card:
|
||||
|
||||
cribbageValue := cribbage.Value(card)
|
||||
|
||||
Scoring
|
||||
|
||||
To score a play, supply a discard pile:
|
||||
|
||||
pegPoints, pegResult := cribbage.Score(cribbage.Peg, discard, Card{})
|
||||
|
||||
To score a shown hand, supply a hand and a starter card:
|
||||
|
||||
handPoints, handResult := cribbage.Score(cribbage.ShownHand, hand, starter)
|
||||
*/
|
||||
package cribbage
|
4
go.mod
4
go.mod
|
@ -1,8 +1,8 @@
|
|||
module git.sr.ht/~tslocum/cards-cribbage
|
||||
module git.sr.ht/~tslocum/joker-cribbage
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
git.sr.ht/~tslocum/cards v0.1.1
|
||||
git.sr.ht/~tslocum/joker v0.1.2-0.20200115233238-4adab71872c0
|
||||
github.com/fighterlyt/permutation v0.0.0-20170407093504-ac78aa5051ae
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,4 +1,4 @@
|
|||
git.sr.ht/~tslocum/cards v0.1.1 h1:yrkj35pB/frGikUXjnVtsYLWTUE1l5dtY033xDkiaC0=
|
||||
git.sr.ht/~tslocum/cards v0.1.1/go.mod h1:ABrbSXnsABGTkuJbWZsI+oGtGTOMlAC+HZy3AysOe7c=
|
||||
git.sr.ht/~tslocum/joker v0.1.2-0.20200115233238-4adab71872c0 h1:CbO6nIO3f5R4J7PsQJXUM9CDfOLDbfSPu30cIy4PhVU=
|
||||
git.sr.ht/~tslocum/joker v0.1.2-0.20200115233238-4adab71872c0/go.mod h1:ILjUVYQbVWJOBnDmpzF2vivl/Nhl7of+l/u42g0K598=
|
||||
github.com/fighterlyt/permutation v0.0.0-20170407093504-ac78aa5051ae h1:wdS91f8H+bGgcjlx5G4LEUVXkmt/uz0VYkc6lZMIjD4=
|
||||
github.com/fighterlyt/permutation v0.0.0-20170407093504-ac78aa5051ae/go.mod h1:KqCsX+AbfYLoAjwmUkE6ocQHwto7ibjZvTY/c5QhgZg=
|
||||
|
|
45
score.go
45
score.go
|
@ -4,7 +4,7 @@ import (
|
|||
"log"
|
||||
"sort"
|
||||
|
||||
. "git.sr.ht/~tslocum/cards"
|
||||
. "git.sr.ht/~tslocum/joker"
|
||||
"github.com/fighterlyt/permutation"
|
||||
)
|
||||
|
||||
|
@ -83,9 +83,10 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
|
||||
var scoreCards Cards
|
||||
if scoringType == Peg {
|
||||
scoreCards = c.Reverse()
|
||||
scoreCards = c.Reversed()
|
||||
} else {
|
||||
scoreCards = append(c.Copy(), starter).Sort()
|
||||
scoreCards = append(c.Copy(), starter)
|
||||
sort.Sort(scoreCards)
|
||||
}
|
||||
|
||||
// Score 15s
|
||||
|
@ -126,7 +127,8 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
allusedcards = append(allusedcards, usedcards)
|
||||
fifteenscore++
|
||||
|
||||
results = append(results, ScoreResult{Type: Score15, Cards: usedcards.Sort(), Points: 2})
|
||||
sort.Sort(usedcards)
|
||||
results = append(results, ScoreResult{Type: Score15, Cards: usedcards, Points: 2})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,7 +138,7 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
}
|
||||
} else {
|
||||
if Sum(scoreCards) == 15 {
|
||||
results = append(results, ScoreResult{Type: Score15, Cards: scoreCards.Sort(), Points: 2})
|
||||
results = append(results, ScoreResult{Type: Score15, Cards: scoreCards.Sorted(), Points: 2})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,29 +168,32 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
} else if len(paircards) == 4 {
|
||||
pairmultiplier = 3
|
||||
}
|
||||
results = append(results, ScoreResult{Type: ScorePair, Cards: paircards.Sort(), Points: len(paircards) * pairmultiplier})
|
||||
sort.Sort(paircards)
|
||||
results = append(results, ScoreResult{Type: ScorePair, Cards: paircards, Points: len(paircards) * pairmultiplier})
|
||||
}
|
||||
|
||||
faces = append(faces, card.Face)
|
||||
}
|
||||
} else {
|
||||
if len(scoreCards) > 0 {
|
||||
var paircards Cards
|
||||
var pairCards Cards
|
||||
for _, compcard := range scoreCards[1:] {
|
||||
if compcard.Face != scoreCards[0].Face {
|
||||
break
|
||||
}
|
||||
|
||||
paircards = append(paircards, compcard)
|
||||
pairCards = append(pairCards, compcard)
|
||||
}
|
||||
pairmultiplier := 1
|
||||
if len(paircards) == 2 {
|
||||
if len(pairCards) == 2 {
|
||||
pairmultiplier = 2
|
||||
} else if len(paircards) == 3 {
|
||||
} else if len(pairCards) == 3 {
|
||||
pairmultiplier = 3
|
||||
}
|
||||
if paircards != nil {
|
||||
results = append(results, ScoreResult{Type: ScorePair, Cards: append(paircards, scoreCards[0]).Sort(), Points: (len(paircards) + 1) * pairmultiplier})
|
||||
if pairCards != nil {
|
||||
pairCards = append(pairCards, scoreCards[0])
|
||||
sort.Sort(pairCards)
|
||||
results = append(results, ScoreResult{Type: ScorePair, Cards: pairCards, Points: len(pairCards) * pairmultiplier})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -206,12 +211,12 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
// Examine the pile for a run by shortening the checked pile one card
|
||||
// after each iteration.
|
||||
SCOREPEGRUN:
|
||||
for complen := 0; complen < len(scoreCards); complen++ {
|
||||
compHand = scoreCards[0 : len(scoreCards)-complen]
|
||||
for complen := len(scoreCards); complen > 0; complen-- {
|
||||
compHand = scoreCards[0:complen].Sorted()
|
||||
compScore = 1
|
||||
runCards = nil
|
||||
|
||||
for i, compcard := range compHand.Sort() {
|
||||
for i, compcard := range compHand {
|
||||
if i > 0 {
|
||||
if int(compcard.Face) == (runValue + 1) {
|
||||
compScore++
|
||||
|
@ -227,7 +232,7 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
runScore = compScore
|
||||
|
||||
if runScore == len(scoreCards) {
|
||||
runCards = compHand.Sort()
|
||||
runCards = compHand
|
||||
break SCOREPEGRUN
|
||||
}
|
||||
}
|
||||
|
@ -262,7 +267,7 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
continue
|
||||
}
|
||||
|
||||
runCards = runCards.Sort()
|
||||
sort.Sort(runCards)
|
||||
for _, rc := range allRunCards {
|
||||
containsAll := true
|
||||
for _, runCard := range runCards {
|
||||
|
@ -298,7 +303,8 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
flushCards = append(flushCards, starter)
|
||||
}
|
||||
if suitvalue == 5 || (suitvalue == 4 && scoringType == ShowHand) {
|
||||
results = append(results, ScoreResult{Type: ScoreFlush, Cards: flushCards.Sort(), Points: suitvalue})
|
||||
sort.Sort(flushCards)
|
||||
results = append(results, ScoreResult{Type: ScoreFlush, Cards: flushCards, Points: suitvalue})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -314,7 +320,8 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
|
|||
|
||||
// Score 31
|
||||
if scoringType == Peg && Sum(scoreCards) == 31 {
|
||||
results = append(results, ScoreResult{Type: Score31, Cards: scoreCards.Sort(), Points: 2})
|
||||
sort.Sort(scoreCards)
|
||||
results = append(results, ScoreResult{Type: Score31, Cards: scoreCards, Points: 2})
|
||||
}
|
||||
|
||||
for _, r := range results {
|
||||
|
|
|
@ -3,7 +3,7 @@ package cribbage
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
. "git.sr.ht/~tslocum/cards"
|
||||
. "git.sr.ht/~tslocum/joker"
|
||||
)
|
||||
|
||||
// ScoreResult is a score from pegging or showing a hand.
|
||||
|
|
|
@ -3,7 +3,7 @@ package cribbage
|
|||
import (
|
||||
"testing"
|
||||
|
||||
. "git.sr.ht/~tslocum/cards"
|
||||
. "git.sr.ht/~tslocum/joker"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -134,7 +134,7 @@ var expectedShowScores = []expectedShowScore{
|
|||
}},
|
||||
}
|
||||
|
||||
func TestPegScoring(t *testing.T) {
|
||||
func TestScorePeg(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i, expected := range expectedPegScores {
|
||||
|
@ -200,6 +200,48 @@ func TestScoreShowCrib(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkScorePeg(b *testing.B) {
|
||||
var (
|
||||
score int
|
||||
results ScoreResults
|
||||
)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, expected := range expectedPegScores {
|
||||
score, results = Score(Peg, expected.Hand, Card{})
|
||||
}
|
||||
}
|
||||
_, _ = score, results
|
||||
}
|
||||
|
||||
func BenchmarkScoreShowHand(b *testing.B) {
|
||||
var (
|
||||
score int
|
||||
results ScoreResults
|
||||
)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, expected := range expectedShowScores {
|
||||
score, results = Score(ShowHand, expected.Hand, expected.Starter)
|
||||
}
|
||||
}
|
||||
_, _ = score, results
|
||||
}
|
||||
|
||||
func BenchmarkScoreShowCrib(b *testing.B) {
|
||||
var (
|
||||
score int
|
||||
results ScoreResults
|
||||
)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, expected := range expectedShowScores {
|
||||
score, results = Score(ShowCrib, expected.Hand, expected.Starter)
|
||||
}
|
||||
}
|
||||
_, _ = score, results
|
||||
}
|
||||
|
||||
func resultsEqual(a []ScoreResult, b []ScoreResult) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
|
|
Loading…
Reference in New Issue