38 lines
913 B
Go
38 lines
913 B
Go
|
|
package poker
|
||
|
|
|
||
|
|
import (
|
||
|
|
"math/rand"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Card represents a playing card as a two-character string.
|
||
|
|
// Ranks: 2-9, T, J, Q, K, A
|
||
|
|
// Suits: s (spades), h (hearts), d (diamonds), c (clubs)
|
||
|
|
// Examples: "As", "Kh", "Td", "2c"
|
||
|
|
|
||
|
|
var ranks = []string{"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}
|
||
|
|
var suits = []string{"s", "h", "d", "c"}
|
||
|
|
|
||
|
|
// NewDeck returns a freshly shuffled 52-card deck.
|
||
|
|
func NewDeck() []string {
|
||
|
|
deck := make([]string, 0, 52)
|
||
|
|
for _, r := range ranks {
|
||
|
|
for _, s := range suits {
|
||
|
|
deck = append(deck, r+s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
|
|
rng.Shuffle(len(deck), func(i, j int) {
|
||
|
|
deck[i], deck[j] = deck[j], deck[i]
|
||
|
|
})
|
||
|
|
return deck
|
||
|
|
}
|
||
|
|
|
||
|
|
// Deal removes and returns the top n cards from the deck.
|
||
|
|
func Deal(deck []string, n int) (cards []string, remaining []string) {
|
||
|
|
if n > len(deck) {
|
||
|
|
n = len(deck)
|
||
|
|
}
|
||
|
|
return deck[:n], deck[n:]
|
||
|
|
}
|