Browse Source

Rename Cards methods Sort to Sorted and Reverse to Reversed

master
Trevor Slocum 3 years ago
parent
commit
b0983c2e47
  1. 2
      README.md
  2. 1
      card.go
  3. 102
      cards.go
  4. 29
      cards_test.go
  5. 2
      deck.go
  6. 27
      doc.go

2
README.md

@ -7,6 +7,8 @@ Playing card library
## Extensions
The following libraries extend cards:
- [cards-cribbage](https://git.sr.ht/~tslocum/cards-cribbage)
## Documentation

1
card.go

@ -1,4 +1,3 @@
// Package cards provides playing cards.
package cards
import (

102
cards.go

@ -65,14 +65,14 @@ func (c Cards) Len() int {
return len(c)
}
func (c Cards) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c Cards) Less(i, j int) bool {
return c[i].Value() < c[j].Value()
}
func (c Cards) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Copy returns a copy of the supplied cards.
func (c Cards) Copy() Cards {
cc := make(Cards, len(c))
@ -80,59 +80,12 @@ func (c Cards) Copy() Cards {
return cc
}
// Sort returns the supplied cards in order.
func (c Cards) Sort() Cards {
cc := c.Copy()
sort.Sort(cc)
return cc
}
// Reverse returns the supplied cards in reverse order.
func (c Cards) Reverse() Cards {
l := len(c)
cc := make(Cards, l)
for i := 0; i < l; i++ {
cc[i] = c[l-i-1]
}
return cc
}
// Remove returns the supplied cards excluding the card at the specified index.
func (c Cards) Remove(i int) Cards {
cc := c.Copy()
return append(cc[:i], cc[i+1:]...)
}
// Low returns the lowest valued card.
func (c Cards) Low() Card {
if len(c) == 0 {
return Card{}
}
l := c[0]
for _, comp := range c[1:] {
if comp.Value() < l.Value() {
l = comp
}
}
return l
}
// High returns the highest valued card.
func (c Cards) High() Card {
if len(c) == 0 {
return Card{}
}
h := c[0]
for _, comp := range c[1:] {
if comp.Value() > h.Value() {
h = comp
}
}
return h
}
// Contains returns whether the supplied cards contain the specified card.
func (c Cards) Contains(card Card) bool {
for _, compcard := range c {
@ -170,3 +123,50 @@ func (c Cards) Equal(cards Cards) bool {
return true
}
// Sorted returns the supplied cards in order.
func (c Cards) Sorted() Cards {
cc := c.Copy()
sort.Sort(cc)
return cc
}
// Reversed returns the supplied cards in reverse order.
func (c Cards) Reversed() Cards {
l := len(c)
cc := make(Cards, l)
for i := 0; i < l; i++ {
cc[i] = c[l-i-1]
}
return cc
}
// Low returns the lowest valued card.
func (c Cards) Low() Card {
if len(c) == 0 {
return Card{}
}
l := c[0]
for _, comp := range c[1:] {
if comp.Value() < l.Value() {
l = comp
}
}
return l
}
// High returns the highest valued card.
func (c Cards) High() Card {
if len(c) == 0 {
return Card{}
}
h := c[0]
for _, comp := range c[1:] {
if comp.Value() > h.Value() {
h = comp
}
}
return h
}

29
cards_test.go

@ -2,14 +2,15 @@ package cards
import (
"reflect"
"sort"
"testing"
)
type cardsTestCase struct {
Cards Cards
Reversed Cards
RemovedFirst Cards
RemovedLast Cards
Reversed Cards
Sorted Cards
Low Card
High Card
@ -18,27 +19,27 @@ type cardsTestCase struct {
var cardsTestCases = []*cardsTestCase{
{
Cards: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
Reversed: Cards{Card{Face3, SuitSpades}, Card{Face2, SuitClubs}, Card{Face4, SuitDiamonds}, Card{FaceAce, SuitHearts}},
RemovedFirst: Cards{Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
RemovedLast: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}},
Reversed: Cards{Card{Face3, SuitSpades}, Card{Face2, SuitClubs}, Card{Face4, SuitDiamonds}, Card{FaceAce, SuitHearts}},
Sorted: Cards{Card{FaceAce, SuitHearts}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}, Card{Face4, SuitDiamonds}},
Low: Card{FaceAce, SuitHearts},
High: Card{Face4, SuitDiamonds},
},
{
Cards: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
Reversed: Cards{Card{Face4, SuitSpades}, Card{Face2, SuitClubs}},
RemovedFirst: Cards{Card{Face4, SuitSpades}},
RemovedLast: Cards{Card{Face2, SuitClubs}},
Reversed: Cards{Card{Face4, SuitSpades}, Card{Face2, SuitClubs}},
Sorted: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
Low: Card{Face2, SuitClubs},
High: Card{Face4, SuitSpades},
},
{
Cards: Cards{Card{FaceQueen, SuitHearts}},
Reversed: Cards{Card{FaceQueen, SuitHearts}},
RemovedFirst: Cards{},
RemovedLast: Cards{},
Reversed: Cards{Card{FaceQueen, SuitHearts}},
Sorted: Cards{Card{FaceQueen, SuitHearts}},
Low: Card{FaceQueen, SuitHearts},
High: Card{FaceQueen, SuitHearts},
@ -52,11 +53,6 @@ func TestCards(t *testing.T) {
t.Errorf("failed to copy cards: expected %s, got %s", c.Cards, copied)
}
reversed := c.Cards.Reverse()
if !reflect.DeepEqual(reversed, c.Reversed) {
t.Errorf("failed to reverse cards: expected %s, got %s", c.Reversed, reversed)
}
removedFirst := c.Cards.Remove(0)
if !reflect.DeepEqual(removedFirst, c.RemovedFirst) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedFirst, removedFirst)
@ -67,7 +63,12 @@ func TestCards(t *testing.T) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedLast, removedLast)
}
sorted := c.Cards.Sort()
reversed := c.Cards.Reversed()
if !reflect.DeepEqual(reversed, c.Reversed) {
t.Errorf("failed to reverse cards: expected %s, got %s", c.Reversed, reversed)
}
sorted := c.Cards.Sorted()
if !reflect.DeepEqual(sorted, c.Sorted) {
t.Errorf("failed to sort cards: expected %s, got %s", c.Sorted, sorted)
}
@ -83,3 +84,11 @@ func TestCards(t *testing.T) {
}
}
}
func BenchmarkCardsSort(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, c := range cardsTestCases {
sort.Sort(c.Cards)
}
}
}

2
deck.go

@ -13,7 +13,7 @@ type Deck struct {
}
// NewDeck initializes a deck of cards. A seed value of 0 is replaced with the
// current time in nanoseconds.
// current unix time in nanoseconds.
func NewDeck(c Cards, seed int64) *Deck {
if seed == 0 {
seed = time.Now().UnixNano()

27
doc.go

@ -0,0 +1,27 @@
/*
Package cards provides playing cards.
Cards
To initialize a card:
card := Card{FaceAce, SuitSpades}
To sort a set of cards:
hand := Cards{
Card{FaceAce, SuitSpades},
Card{Face3, SuitSpades},
Card{Face2, SuitSpades}
}
sort.Sort(hand)
Deck
To initialize a deck, call NewDeck with a seed for the random number generator
used when shuffling. A seed value of zero will be replaced with the
current unix time in nanoseconds.
deck := NewDeck(StandardCards, 0)
*/
package cards
Loading…
Cancel
Save