Remove dependency on permutation library

This commit is contained in:
Trevor Slocum 2020-01-16 06:36:49 -08:00
parent 2a9990d38b
commit 4651517ba9
6 changed files with 17 additions and 44 deletions

View File

@ -1,5 +1,6 @@
0.1.1:
- Rename library from cards-cribbage to joker-cribbage
- Remove dependency on permutation library
0.1.0:
- Initial release

View File

@ -10,3 +10,12 @@ func Value(c Card) int {
}
return v
}
// Sum returns the total cribbage value of the supplied cards.
func Sum(c Cards) int {
var v int
for _, card := range c {
v += Value(card)
}
return v
}

View File

@ -1,18 +0,0 @@
package cribbage
import . "git.sr.ht/~tslocum/joker"
// Sum returns the total cribbage value of the supplied cards.
func Sum(c Cards) int {
var v int
for _, card := range c {
v += Value(card)
}
return v
}
func compareCards(i, j interface{}) bool {
icard := i.(Card)
jcard := j.(Card)
return icard.Value() < jcard.Value()
}

5
go.mod
View File

@ -2,7 +2,4 @@ module git.sr.ht/~tslocum/joker-cribbage
go 1.13
require (
git.sr.ht/~tslocum/joker v0.1.2-0.20200115233238-4adab71872c0
github.com/fighterlyt/permutation v0.0.0-20170407093504-ac78aa5051ae
)
require git.sr.ht/~tslocum/joker v0.1.2-0.20200116142638-b0126631fff3

6
go.sum
View File

@ -1,4 +1,2 @@
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=
git.sr.ht/~tslocum/joker v0.1.2-0.20200116142638-b0126631fff3 h1:oAv5q5P8Zhg8KMF8DhI7cVwr3WS3WK2fJo4PG7gGdNI=
git.sr.ht/~tslocum/joker v0.1.2-0.20200116142638-b0126631fff3/go.mod h1:ILjUVYQbVWJOBnDmpzF2vivl/Nhl7of+l/u42g0K598=

View File

@ -1,11 +1,9 @@
package cribbage
import (
"log"
"sort"
. "git.sr.ht/~tslocum/joker"
"github.com/fighterlyt/permutation"
)
// ScoringType represents a set of scoring rules.
@ -82,30 +80,24 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
}
var scoreCards Cards
var permutations []Cards
if scoringType == Peg {
scoreCards = c.Reversed()
} else {
scoreCards = append(c.Copy(), starter)
sort.Sort(scoreCards)
permutations = scoreCards.Permutations()
}
// Score 15s
fifteenscore := 0
fifteenvalue := 0
if scoringType != Peg {
var allusedcards []Cards
var usedcards Cards
perm, err := permutation.NewPerm(scoreCards, compareCards)
if err != nil {
log.Panicf("failed to generate a permutation of cards %s", scoreCards)
}
SCORE15:
for permhand, err := perm.Next(); err == nil; permhand, err = perm.Next() {
for _, permhand := range permutations {
fifteenvalue = 0
permhand := permhand.(Cards)
usedcards = Cards{}
for _, card := range permhand {
@ -243,14 +235,8 @@ func Score(scoringType ScoringType, c Cards, starter Card) (int, ScoreResults) {
}
} else {
for runLength := 6; runLength > 3; runLength-- {
perm, err := permutation.NewPerm(scoreCards, compareCards)
if err != nil {
log.Panicf("failed to generate a permutation of cards %s", scoreCards)
}
SCOREHANDRUN:
for permhand, err := perm.Next(); err == nil; permhand, err = perm.Next() {
permhand := permhand.(Cards)
for _, permhand := range permutations {
runCards = Cards{}
runScore = 0