Playing cards: Difference between revisions

Go solution
m (→‎Icon and Unicon: header simplification)
(Go solution)
Line 668:
END PROGRAM</lang>
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 main
 
import (
"fmt"
"rand"
"time"
)
 
func main() {
rand.Seed(time.Nanoseconds())
d := newDeck()
fmt.Println("fresh deck")
fmt.Println(d)
 
d.shuffle()
fmt.Println("\nshuffled")
fmt.Println(d)
 
h := d.deal(5)
fmt.Println("\n5 cards delt")
fmt.Println(h)
 
fmt.Println("\npip, suit values of cards in hand:")
fmt.Println("pip suit")
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)
}
 
// card type. (deck type defined later)
// valid range is 0..51. this facilititates computations.
type card int
 
// zero-based pip/suit functions. useful for computations.
// (or maybe just reference for computations, since they are trivial)
func (c card) pip0() int {
return int(c % 13)
}
 
func (c card) suit0() int {
return int(c / 13)
}
 
func (c card) pipSuit0() (int, int) {
return int(c % 13), int(c / 13)
}
 
// one-base pip/suit functions. meaningful for output.
// ("pip" does really mean the identifying number of spots on the card)
func (c card) pip() int {
return int(c%13) + 1
}
 
func (c card) suit() int {
return int(c/13) + 1
}
 
func (c card) pipSuit() (int, int) {
return int(c%13) + 1, int(c/13) + 1
}
 
// string representation
const pipS = "A23456789TJQK"
const suitS = "CDHS"
 
func (c card) String() string {
px, sx := c.pipSuit0()
return pipS[px:px+1] + suitS[sx:sx+1]
}
// deck type
type deck []card
 
// "constructor" returns pointer to new deck object
func newDeck() deck {
d := make(deck, 52)
for i := range d {
d[i] = card(i)
}
return d
}
 
// string representation: 13 cards to a line, up to four lines.
// lines separated with newline character. empty deck is empty string.
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
}
 
// shuffle in place (although using extra memory)
func (d deck) shuffle() {
d2 := make(deck, len(d))
copy(d2, d)
for i, s := range rand.Perm(len(d)) {
d[i] = d2[s]
}
}
 
// return requested number of cards as new deck object.
// the deck being delt from (the method reciever) is resliced.
func (d *deck) deal(n int) deck {
if n > len(*d) {
return nil
}
r := make(deck, n)
copy(r, *d)
*d = (*d)[n:]
return r
}</lang>
Output:
<pre>
fresh deck
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS
 
shuffled
AH KD 6C KS KH TS 7C 2C 4H 9C 5H KC 3S
JH TH 5S QS JD 2D 7S 8H 8C 4D 9H QH TD
5C 3H 3C 8D AC TC JS 6S 9D 7D 6H QD 9S
8S 7H 4S 2H 4C QC 3D AD AS JC 5D 2S 6D
 
5 cards delt
AH KD 6C KS KH
 
pip, suit values of cards in hand:
pip suit
1 3
13 2
6 1
13 4
13 3
 
47 cards left in deck
TS 7C 2C 4H 9C 5H KC 3S JH TH 5S QS JD
2D 7S 8H 8C 4D 9H QH TD 5C 3H 3C 8D AC
TC JS 6S 9D 7D 6H QD 9S 8S 7H 4S 2H 4C
QC 3D AD AS JC 5D 2S 6D
</pre>
 
=={{header|Haskell}}==
1,707

edits