Browse Source

Remove NewCard initializer

Trevor Slocum 3 years ago
  1. 7
  2. 14
  3. 104
  4. 38
  5. 4
  6. 8


@ -16,11 +16,6 @@ type Card struct {
Suit CardSuit
// NewCard initializes a Card.
func NewCard(face CardFace, suit CardSuit) Card {
return Card{Face: face, Suit: suit}
// Value returns the numeric value of a card.
func (c Card) Value() int {
return (int(c.Face) * 100) + int(c.Suit)
@ -138,6 +133,6 @@ func (c *Card) UnmarshalJSON(b []byte) error {
return fmt.Errorf("invalid suit identifier %s", s)
*c = NewCard(cardFace, cardSuit)
*c = Card{cardFace, cardSuit}
return nil


@ -11,13 +11,13 @@ type cardTestCase struct {
var cardTestCases = []*cardTestCase{
{Card: NewCard(FaceAce, SuitSpades), Expected: `"AS"`},
{Card: NewCard(Face7, SuitDiamonds), Expected: `"7D"`},
{Card: NewCard(Face10, SuitHearts), Expected: `"10H"`},
{Card: NewCard(FaceQueen, SuitHearts), Expected: `"QH"`},
{Card: NewCard(FaceJoker, SuitClubs), Expected: `"!C"`},
{Card: NewCard(FaceJoker, SuitJoker), Expected: `"!!"`},
{Card: NewCard(FaceKing, SuitJoker), Expected: `"K!"`},
{Card: Card{FaceAce, SuitSpades}, Expected: `"AS"`},
{Card: Card{Face7, SuitDiamonds}, Expected: `"7D"`},
{Card: Card{Face10, SuitHearts}, Expected: `"10H"`},
{Card: Card{FaceQueen, SuitHearts}, Expected: `"QH"`},
{Card: Card{FaceJoker, SuitClubs}, Expected: `"!C"`},
{Card: Card{FaceJoker, SuitJoker}, Expected: `"!!"`},
{Card: Card{FaceKing, SuitJoker}, Expected: `"K!"`},
func TestMarshalCard(t *testing.T) {


@ -7,58 +7,58 @@ type Cards []Card
// StandardCards is a slice of standard cards.
var StandardCards = Cards{
NewCard(FaceAce, SuitHearts),
NewCard(Face2, SuitHearts),
NewCard(Face3, SuitHearts),
NewCard(Face4, SuitHearts),
NewCard(Face5, SuitHearts),
NewCard(Face6, SuitHearts),
NewCard(Face7, SuitHearts),
NewCard(Face8, SuitHearts),
NewCard(Face9, SuitHearts),
NewCard(Face10, SuitHearts),
NewCard(FaceJack, SuitHearts),
NewCard(FaceQueen, SuitHearts),
NewCard(FaceKing, SuitHearts),
NewCard(FaceAce, SuitDiamonds),
NewCard(Face2, SuitDiamonds),
NewCard(Face3, SuitDiamonds),
NewCard(Face4, SuitDiamonds),
NewCard(Face5, SuitDiamonds),
NewCard(Face6, SuitDiamonds),
NewCard(Face7, SuitDiamonds),
NewCard(Face8, SuitDiamonds),
NewCard(Face9, SuitDiamonds),
NewCard(Face10, SuitDiamonds),
NewCard(FaceJack, SuitDiamonds),
NewCard(FaceQueen, SuitDiamonds),
NewCard(FaceKing, SuitDiamonds),
NewCard(FaceAce, SuitClubs),
NewCard(Face2, SuitClubs),
NewCard(Face3, SuitClubs),
NewCard(Face4, SuitClubs),
NewCard(Face5, SuitClubs),
NewCard(Face6, SuitClubs),
NewCard(Face7, SuitClubs),
NewCard(Face8, SuitClubs),
NewCard(Face9, SuitClubs),
NewCard(Face10, SuitClubs),
NewCard(FaceJack, SuitClubs),
NewCard(FaceQueen, SuitClubs),
NewCard(FaceKing, SuitClubs),
NewCard(FaceAce, SuitSpades),
NewCard(Face2, SuitSpades),
NewCard(Face3, SuitSpades),
NewCard(Face4, SuitSpades),
NewCard(Face5, SuitSpades),
NewCard(Face6, SuitSpades),
NewCard(Face7, SuitSpades),
NewCard(Face8, SuitSpades),
NewCard(Face9, SuitSpades),
NewCard(Face10, SuitSpades),
NewCard(FaceJack, SuitSpades),
NewCard(FaceQueen, SuitSpades),
NewCard(FaceKing, SuitSpades),
Card{FaceAce, SuitHearts},
Card{Face2, SuitHearts},
Card{Face3, SuitHearts},
Card{Face4, SuitHearts},
Card{Face5, SuitHearts},
Card{Face6, SuitHearts},
Card{Face7, SuitHearts},
Card{Face8, SuitHearts},
Card{Face9, SuitHearts},
Card{Face10, SuitHearts},
Card{FaceJack, SuitHearts},
Card{FaceQueen, SuitHearts},
Card{FaceKing, SuitHearts},
Card{FaceAce, SuitDiamonds},
Card{Face2, SuitDiamonds},
Card{Face3, SuitDiamonds},
Card{Face4, SuitDiamonds},
Card{Face5, SuitDiamonds},
Card{Face6, SuitDiamonds},
Card{Face7, SuitDiamonds},
Card{Face8, SuitDiamonds},
Card{Face9, SuitDiamonds},
Card{Face10, SuitDiamonds},
Card{FaceJack, SuitDiamonds},
Card{FaceQueen, SuitDiamonds},
Card{FaceKing, SuitDiamonds},
Card{FaceAce, SuitClubs},
Card{Face2, SuitClubs},
Card{Face3, SuitClubs},
Card{Face4, SuitClubs},
Card{Face5, SuitClubs},
Card{Face6, SuitClubs},
Card{Face7, SuitClubs},
Card{Face8, SuitClubs},
Card{Face9, SuitClubs},
Card{Face10, SuitClubs},
Card{FaceJack, SuitClubs},
Card{FaceQueen, SuitClubs},
Card{FaceKing, SuitClubs},
Card{FaceAce, SuitSpades},
Card{Face2, SuitSpades},
Card{Face3, SuitSpades},
Card{Face4, SuitSpades},
Card{Face5, SuitSpades},
Card{Face6, SuitSpades},
Card{Face7, SuitSpades},
Card{Face8, SuitSpades},
Card{Face9, SuitSpades},
Card{Face10, SuitSpades},
Card{FaceJack, SuitSpades},
Card{FaceQueen, SuitSpades},
Card{FaceKing, SuitSpades},
func (c Cards) Len() int {


@ -17,31 +17,31 @@ type cardsTestCase struct {
var cardsTestCases = []*cardsTestCase{
Cards: Cards{NewCard(FaceAce, SuitHearts), NewCard(Face4, SuitDiamonds), NewCard(Face2, SuitClubs), NewCard(Face3, SuitSpades)},
Reversed: Cards{NewCard(Face3, SuitSpades), NewCard(Face2, SuitClubs), NewCard(Face4, SuitDiamonds), NewCard(FaceAce, SuitHearts)},
RemovedFirst: Cards{NewCard(Face4, SuitDiamonds), NewCard(Face2, SuitClubs), NewCard(Face3, SuitSpades)},
RemovedLast: Cards{NewCard(FaceAce, SuitHearts), NewCard(Face4, SuitDiamonds), NewCard(Face2, SuitClubs)},
Sorted: Cards{NewCard(FaceAce, SuitHearts), NewCard(Face2, SuitClubs), NewCard(Face3, SuitSpades), NewCard(Face4, SuitDiamonds)},
Low: NewCard(FaceAce, SuitHearts),
High: NewCard(Face4, SuitDiamonds),
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}},
Sorted: Cards{Card{FaceAce, SuitHearts}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}, Card{Face4, SuitDiamonds}},
Low: Card{FaceAce, SuitHearts},
High: Card{Face4, SuitDiamonds},
Cards: Cards{NewCard(Face2, SuitClubs), NewCard(Face4, SuitSpades)},
Reversed: Cards{NewCard(Face4, SuitSpades), NewCard(Face2, SuitClubs)},
RemovedFirst: Cards{NewCard(Face4, SuitSpades)},
RemovedLast: Cards{NewCard(Face2, SuitClubs)},
Sorted: Cards{NewCard(Face2, SuitClubs), NewCard(Face4, SuitSpades)},
Low: NewCard(Face2, SuitClubs),
High: NewCard(Face4, SuitSpades),
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}},
Sorted: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
Low: Card{Face2, SuitClubs},
High: Card{Face4, SuitSpades},
Cards: Cards{NewCard(FaceQueen, SuitHearts)},
Reversed: Cards{NewCard(FaceQueen, SuitHearts)},
Cards: Cards{Card{FaceQueen, SuitHearts}},
Reversed: Cards{Card{FaceQueen, SuitHearts}},
RemovedFirst: Cards{},
RemovedLast: Cards{},
Sorted: Cards{NewCard(FaceQueen, SuitHearts)},
Low: NewCard(FaceQueen, SuitHearts),
High: NewCard(FaceQueen, SuitHearts),
Sorted: Cards{Card{FaceQueen, SuitHearts}},
Low: Card{FaceQueen, SuitHearts},
High: Card{FaceQueen, SuitHearts},


@ -12,8 +12,8 @@ type Deck struct {
r *rand.Rand
// NewDeck initializes a deck of the supplied cards. A seed value of 0 is
// replaced with the current time in nanoseconds.
// NewDeck initializes a deck of cards. A seed value of 0 is replaced with the
// current time in nanoseconds.
func NewDeck(c Cards, seed int64) *Deck {
if seed == 0 {
seed = time.Now().UnixNano()


@ -17,12 +17,12 @@ func TestDeck(t *testing.T) {
for _, face := range StandardFaces {
found := 0
for _, c := range d.Cards {
if c.Equal(NewCard(face, suit)) {
if c.Equal(Card{face, suit}) {
if found != 1 {
t.Errorf("expected 1 %s, got %d", NewCard(face, suit), found)
t.Errorf("expected 1 %s, got %d", Card{face, suit}, found)
@ -51,12 +51,12 @@ func TestDeck(t *testing.T) {
for _, face := range StandardFaces {
found := 0
for _, c := range discard {
if c.Equal(NewCard(face, suit)) {
if c.Equal(Card{face, suit}) {
if found != 1 {
t.Errorf("failed to draw %d: discard: expected 1 %s, got %d", draw, NewCard(face, suit), found)
t.Errorf("failed to draw %d: discard: expected 1 %s, got %d", draw, Card{face, suit}, found)