Playing cards: Difference between revisions

→‎{{header|Go}}: Redo the Go solution as a reusable package with a slightly different API.
m (→‎{{header|Go}}: Repair corruption)
(→‎{{header|Go}}: Redo the Go solution as a reusable package with a slightly different API.)
Line 1,389:
This creates a new deck, shuffles it, deals five cards to hand, prints the cards in hand and then prints the cards remaining in the deck.
=={{header|Go}}==
<lang go>package maincards
 
import (
"math/rand"
"fmt"
"math/rand"
"time"
)
 
// A Suit represents one of the four standard suites.
func main() {
type Suit uint8
rand.Seed(time.Now().UnixNano())
d := newDeck()
fmt.Println("fresh deck")
fmt.Println(d)
 
// The four standard suites.
d.shuffle()
const (
fmt.Println("\nshuffled")
Spade Suit = 3
fmt.Println(d)
Heart Suit = 2
Diamond Suit = 1
Club Suit = 0
)
 
func (s Suit) String() string {
h := d.deal(5)
const suites = "CDHS" // or "♣♢♡♠"
fmt.Println("\n5 cards delt")
return suites[s : s+1]
fmt.Println(h)
}
 
// Rank is the rank or pip value of a card from Ace==1 to King==13.
type Rank uint8
 
// The ranks from Ace to King.
const (
Ace Rank = 1
Two Rank = 2
Three Rank = 3
Four Rank = 4
Five Rank = 5
Six Rank = 6
Seven Rank = 7
Eight Rank = 8
Nine Rank = 9
Ten Rank = 10
Jack Rank = 11
Queen Rank = 12
King Rank = 13
)
 
func (r Rank) String() string {
fmt.Println("\npip, suit values of cards in hand:")
const ranks = "A23456789TJQK"
fmt.Println("pip suit")
return ranks[r-1 : r]
for _, c := range h {
fmt.Printf("%3d %3d\n", c.pip(), c.suit())
}
fmt.Print("\n", len(d), " cards left in deck\n")
fmt.Println(d)
}
 
// A Card represets a specific playing card.
// card type. (deck type defined later)
// It's an encoded representation of Rank and Suit
// valid range is 0..51. this facilititates computations.
// with a valid range of [0,51].
type card int
type Card uint8
 
// NewCard returns the Card representation for the specified rank and suit.
// zero-based pip/suit functions. useful for computations.
func NewCard(r Rank, s Suit) Card {
// (or maybe just reference for computations, since they are trivial)
return Card(13*uint8(s) + uint8(r-1))
func (c card) pip0() int {
return int(c % 13)
}
 
// RankSuit returns the rank and suit of the card.
func (c card) suit0() int {
func (c Card) RankSuit() (Rank, Suit) {
return int(c / 13)
return Rank(c%13 + 1), Suit(c / 13)
}
 
// Rank returns the rank of the card.
func (c card) pipSuit0() (int, int) {
func return int(c % 13Card), intRank(c) /Rank 13){
return Rank(c%13 + 1)
}
 
// Suit returns the suit of the card.
// one-base pip/suit functions. meaningful for output.
func (c Card) Suit() Suit {
// ("pip" does really mean the identifying number of spots on the card)
return Suit(c / 13)
func (c card) pip() int {
return int(c%13) + 1
}
 
func (c cardCard) suitString() intstring {
return int(c/13.Rank().String() + 1c.Suit().String()
}
 
// A Deck represents a set of zero or more cards in a specific order.
func (c card) pipSuit() (int, int) {
type Deck []Card
return int(c%13) + 1, int(c/13) + 1
 
// NewDeck returns a regular 52 deck of cards in A-K order.
func NewDeck() Deck {
d := make(Deck, 52)
for i := range d {
d[i] = Card(i)
}
return d
}
 
// String returns a string representation of the cards in the deck with
// a newline ('\n') separating the cards into groups of thirteen.
const pipS = "A23456789TJQK"
func (d Deck) String() string {
const suitS = "CDHS"
s := ""
for i, c := range d {
switch {
case i == 0: // do nothing
case i%13 == 0:
s += "\n"
default:
s += " "
}
s += c.String()
}
return s
}
 
// Shuffle randomises the order of the cards in the deck.
func (c card) String() string {
func (d Deck) Shuffle() {
px, sx := c.pipSuit0()
for i := range d {
return pipS[px:px+1] + suitS[sx:sx+1]
j := rand.Intn(i + 1)
d[i], d[j] = d[j], d[i]
}
}
 
// Contains returns true if the specified card is withing the deck.
// deck type
func (d Deck) Contains(tc Card) bool {
type deck []card
for _, c := range d {
if c == tc {
return true
}
}
return false
}
 
// AddDeck adds the specified deck(s) to this one at the end/bottom.
// "constructor" returns pointer to new deck object
func newDeck(d *Deck) deckAddDeck(decks ...Deck) {
for _, do := make(deck,range decks 52){
*d = append(*d, o...)
for i := range d {
}
d[i] = card(i)
}
return d
}
 
// AddCard adds the specified card to this deck at the end/bottom.
// string representation: 13 cards to a line, up to four lines.
func (d *Deck) AddCard(c Card) {
// lines separated with newline character. empty deck is empty string.
*d = append(*d, c)
func (d deck) String() string {
if len(d) == 0 {
return ""
}
r := d[0].String()
for i, c := range d[1:] {
if i%13 == 12 {
r += "\n"
} else {
r += " "
}
r += c.String()
}
return r
}
 
// Draw removes the selected number of cards from the top of the deck,
// shuffle in place (although using extra memory)
// returning them as a new deck.
func (d deck) shuffle() {
func (d *Deck) Draw(n int) Deck {
d2 := make(deck, len(d))
old := *d
copy(d2, d)
*d = old[n:]
for i, s := range rand.Perm(len(d)) {
return old[:n:n]
d[i] = d2[s]
}
}
 
// DrawCard draws a single card off the top of the deck,
// return requested number of cards as new deck object.
// theremoving deck being deltit from (the method reciever) is resliceddeck.
// It returns false if there are no cards in the deck.
func (d *deck) deal(n int) deck {
func (d *Deck) DrawCard() (Card, bool) {
if n > len(*d) {
if len(*d) == 0 {
return nil
return 0, false
}
}
r := make(deck, n)
old copy(r,:= *d)
*d = (*d)old[n1:]
return old[0], true
return r
}
 
// Deal deals out cards from the deck one at a time to multiple players.
// The initial hands (decks) of each player are provided as arguments and the
// modified hands are returned. The initial hands can be empty or nil.
// E.g. Deal(7, nil, nil, nil) deals out seven cards to three players
// each starting with no cards.
// If there are insufficient cards in the deck the hands are partially dealt and
// the boolean return is set to false (true otherwise).
func (d *Deck) Deal(cards int, hands ...Deck) ([]Deck, bool) {
for i := 0; i < cards; i++ {
for j := range hands {
if len(*d) == 0 {
return hands, false
}
hands[j] = append(hands[j], (*d)[0])
*d = (*d)[1:]
}
}
return hands, true
}</lang>
Example use:
<lang go>package main
 
import (
"fmt"
"whatever/path/was/used/cards"
)
 
func main() {
// We do not call rand.Seed() in order for the results to be repeatable.
//rand.Seed(time.Now().UnixNano())
d := cards.NewDeck()
fmt.Println("fresh deck")
fmt.Println(d)
 
d.Shuffle()
fmt.Println("\nshuffled")
fmt.Println(d)
 
h := d.Draw(5)
fmt.Println("\n5 cards drawn")
fmt.Println(h)
 
fmt.Println("\nrank, suit values of cards in drawn:")
fmt.Println("Card Rank Suit")
for _, c := range h {
fmt.Printf("%v : %v=%2[2]d %v=%2[3]d\n", c, c.Rank(), c.Suit())
}
 
ans := h.Contains(cards.NewCard(cards.Queen, cards.Spade))
fmt.Println("Drawn cards include the Queen of Spades?", ans)
ans = h.Contains(cards.NewCard(cards.Jack, cards.Spade))
fmt.Println("Drawn cards include the Jack of Spades?", ans)
 
p, _ := d.Deal(7, nil, nil)
fmt.Println("\nDealing 7 cards to two players")
fmt.Println("Player1:", p[0])
fmt.Println("Player2:", p[1])
 
fmt.Println("\n", len(d), " cards left in deck")
fmt.Println(d)
 
d.AddDeck(h, p[0], p[0])
fmt.Println("\nReturning the cards to the deck")
fmt.Println(d)
}</lang>
Output:
Line 1,520 ⟶ 1,618:
 
shuffled
AH9D KD5C 6C3C KSJS KH9S TS7H 7C7D 2C7S 4HTH 9CQH 5H3S KC6D 3STC
JHTS TH7C 5S QSAH JD8D 2DQS 7S4C 8H2H 8C3H 4DQD 9H3D QHJH TDQC
5CAS 3HKH 3C6S 8DKS AC9C TCKC JS4S 6S4D 9D6H 7D8C 6H9H QD6C 9S8H
2C 8S 7HJD 4SAC 2H2S 4CTD QCKD 3D4H ADJC AS2D JCAD 5D 2S 6D5H
 
5 cards drawn
9D 5C 3C JS 9S
 
rank, suit values of cards in drawn:
Card Rank Suit
9D : 9= 9 D= 1
5C : 5= 5 C= 0
3C : 3= 3 C= 0
JS : J=11 S= 3
9S : 9= 9 S= 3
Drawn cards include the Queen of Spades? false
Drawn cards include the Jack of Spades? true
 
5Dealing 7 cards deltto two players
Player1: 7H 7S QH 6D TS 5S 8D
AH KD 6C KS KH
Player2: 7D TH 3S TC 7C AH QS
 
pip, 33 suit valuescards of cardsleft in hand:deck
4C 2H 3H QD 3D JH QC AS KH 6S KS 9C KC
pip suit
4S 4D 6H 8C 9H 6C 8H 2C 8S JD AC 2S TD
1 3
13KD 4H JC 2D AD 25D 5H
6 1
13 4
13 3
 
47Returning the cards leftto inthe deck
TS4C 7C2H 2C3H 4HQD 9C3D 5HJH KCQC 3SAS JHKH TH6S 5SKS QS9C JDKC
2D4S 7S4D 8H6H 8C 4D 9H QH6C TD8H 5C2C 3H8S 3C 8DJD AC 2S TD
TCKD JS4H 6SJC 9D2D 7DAD 6H5D QD5H 9S9D 8S5C 7H3C 4SJS 2H9S 4C7H
QC7S 3DQH AD6D ASTS JC5S 5D8D 2S7H 7S QH 6D TS 5S 8D
</pre>
 
Anonymous user