forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
1.3 KiB
116 lines
1.3 KiB
/*
|
|
Package playing provides types for modeling a deck of conventional
|
|
playing cards.
|
|
*/
|
|
package playing
|
|
|
|
type Suit uint8
|
|
type Rank uint8
|
|
type Color bool
|
|
|
|
const (
|
|
Spades Suit = iota
|
|
Clubs
|
|
Hearts
|
|
Diamonds
|
|
UnknownSuit
|
|
)
|
|
|
|
const (
|
|
Ace Rank = iota
|
|
Two
|
|
Three
|
|
Four
|
|
Five
|
|
Six
|
|
Seven
|
|
Eight
|
|
Nine
|
|
Ten
|
|
Jack
|
|
Queen
|
|
King
|
|
UnknownRank
|
|
)
|
|
|
|
const (
|
|
Red Color = true
|
|
Black Color = false
|
|
)
|
|
|
|
type Card struct {
|
|
Suit
|
|
Rank
|
|
}
|
|
|
|
func Deck() []Card {
|
|
d := make([]Card, 0, 52)
|
|
for i := 0; i < 4; i++ {
|
|
for k := 0; k < 13; k++ {
|
|
d = append(d, Card{
|
|
Suit: Suit(i),
|
|
Rank: Rank(k),
|
|
})
|
|
}
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (r Rank) String() string {
|
|
switch r {
|
|
case Ace:
|
|
return "A"
|
|
case Two:
|
|
return "2"
|
|
case Three:
|
|
return "3"
|
|
case Four:
|
|
return "4"
|
|
case Five:
|
|
return "5"
|
|
case Six:
|
|
return "6"
|
|
case Seven:
|
|
return "7"
|
|
case Eight:
|
|
return "8"
|
|
case Nine:
|
|
return "9"
|
|
case Ten:
|
|
return "10"
|
|
case Jack:
|
|
return "J"
|
|
case Queen:
|
|
return "Q"
|
|
case King:
|
|
return "K"
|
|
default:
|
|
return "?"
|
|
}
|
|
}
|
|
|
|
func (s Suit) String() string {
|
|
switch s {
|
|
case Spades:
|
|
return "♠"
|
|
case Hearts:
|
|
return "♥"
|
|
case Diamonds:
|
|
return "♦"
|
|
case Clubs:
|
|
return "♣"
|
|
default:
|
|
return "?"
|
|
}
|
|
}
|
|
|
|
func (s Suit) Color() Color {
|
|
switch s {
|
|
case Spades, Clubs:
|
|
return Black
|
|
case Hearts, Diamonds:
|
|
return Red
|
|
default:
|
|
return Black
|
|
}
|
|
}
|
|
|