96 lines
1.4 KiB
Go
96 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
)
|
|
|
|
type Suit int
|
|
|
|
func (s Suit) String() string {
|
|
switch s {
|
|
case 0:
|
|
return "♠"
|
|
case 1:
|
|
return "♥"
|
|
case 2:
|
|
return "♦"
|
|
case 3:
|
|
return "♣"
|
|
case 4:
|
|
return "★"
|
|
default:
|
|
panic("bad suit")
|
|
}
|
|
}
|
|
|
|
type Card int
|
|
|
|
func (c Card) Joker() bool {
|
|
return (c == 0)
|
|
}
|
|
|
|
func (c Card) Suit() Suit {
|
|
return Suit(int(c) % 5)
|
|
}
|
|
|
|
func (c Card) Face() int {
|
|
return int(c) / 5
|
|
}
|
|
|
|
func (c Card) String() string {
|
|
if c.Joker() {
|
|
return "Jok" // 🂿 U+1F0BF
|
|
}
|
|
|
|
if c == -1 {
|
|
return "INV" // Invalid
|
|
}
|
|
|
|
switch c.Face() {
|
|
case 11:
|
|
return fmt.Sprintf(" J%s", c.Suit().String())
|
|
|
|
case 12:
|
|
return fmt.Sprintf(" Q%s", c.Suit().String())
|
|
|
|
case 13:
|
|
return fmt.Sprintf(" K%s", c.Suit().String())
|
|
|
|
default:
|
|
return fmt.Sprintf("%2d%s", c.Face(), c.Suit().String())
|
|
}
|
|
|
|
}
|
|
|
|
func NewJoker() Card {
|
|
return Card(0)
|
|
}
|
|
|
|
func NewCardFrom(face int, suit Suit) Card {
|
|
return Card(face*5 + int(suit))
|
|
}
|
|
|
|
func Shuffle(c []Card) {
|
|
rand.Shuffle(len(c), func(i, j int) {
|
|
c[i], c[j] = c[j], c[i]
|
|
})
|
|
}
|
|
|
|
func Deck() []Card {
|
|
// The full game deck is two 58-card decks
|
|
// Each 58-card deck has 3 jokers plus {3..10 J Q K} in 5 suits.
|
|
ret := make([]Card, 0, 58*2)
|
|
for i := 0; i < 6; i++ {
|
|
ret = append(ret, NewJoker()) // Joker
|
|
}
|
|
|
|
for s := 0; s < 5; s++ {
|
|
for f := 3; f < 14; f++ {
|
|
ret = append(ret, NewCardFrom(f, Suit(s)), NewCardFrom(f, Suit(s)))
|
|
}
|
|
}
|
|
|
|
return ret
|
|
}
|