Playing cards: Difference between revisions

Add CLU
mNo edit summary
(Add CLU)
Line 1,660:
(if (keyword? pip) (name pip) pip)
(name suit)))))</lang>
 
=={{header|CLU}}==
<lang clu>% Represents one playing card.
card = cluster is make, parse, unparse, equal, all_cards
rep = struct[pip: int, suit: char]
own suits: string := "CHSD";
own pips: sequence[string] := sequence[string]$
["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
find_pip = proc (pip: string) returns (int) signals (bad_format)
for i: int in int$from_to(1, sequence[string]$size(pips)) do
if pips[i] = pip then return(i) end
end
signal bad_format
end find_pip
make = proc (pip: int, suit: char) returns (cvt) signals (bad_format)
if string$indexc(suit, suits) = 0
cor ~(pip >= 1 cand pip <= 13)
then signal bad_format
end
return(rep${pip: pip, suit: suit})
end make
parse = proc (s: string) returns (cvt) signals (bad_format)
size: int := string$size(s)
if size<2 cor size>3 then signal bad_format end
pip: string := string$substr(s, 1, size-1)
suit: char := string$rest(s, size-1)[1]
return(down(make(find_pip(pip), suit))) resignal bad_format
end parse
unparse = proc (c: cvt) returns (string)
return( pips[c.pip] || string$c2s(c.suit) )
end unparse
equal = proc (a, b: cvt) returns (bool)
return( a.pip = b.pip cand a.suit = b.suit )
end equal
% Yield all cards in the canonical order
all_cards = iter () yields (cvt)
for suit: char in string$chars(suits) do
for pip: int in int$from_to(1,sequence[string]$size(pips)) do
yield(down(make(pip, suit)))
end
end
end all_cards
end card
 
% Represents a deck
deck = cluster is new, shuffle, cards, deal, unparse
rep = array[card]
new = proc () returns (cvt)
d: rep := rep$new()
for c: card in card$all_cards() do rep$addh(d, c) end
return(d)
end new
shuffle = proc (d: cvt)
lo: int := rep$low(d)
hi: int := rep$high(d)
for i: int in int$from_to_by(hi, lo+1, -1) do
j: int := lo + random$next(i-lo)
c: card := d[i]
d[i] := d[j]
d[j] := c
end
end shuffle
cards = iter (d: cvt) yields (card)
for c: card in rep$elements(d) do yield(c) end
end cards
deal = proc (d: cvt) returns (card) signals (empty)
if rep$empty(d) then signal empty end
return(rep$reml(d))
end deal
unparse = proc (d: cvt) returns (string)
ss: stream := stream$create_output()
n: int := 0
for c: card in cards(up(d)) do
if n~=0 cand n//13=0 then stream$putc(ss, '\n')
elseif n~=0 then stream$putc(ss, ' ')
end
stream$puts(ss, card$unparse(c))
n := n+1
end
return(stream$get_contents(ss))
end unparse
end deck
 
start_up = proc ()
po: stream := stream$primary_output()
% seed the RNG
d_: date := now()
random$seed(d_.second + 60*(d_.minute + 60*d_.hour))
% make a new deck
d: deck := deck$new()
stream$putl(po, "New deck: ")
stream$putl(po, deck$unparse(d))
% shuffle the deck
deck$shuffle(d)
stream$putl(po, "\nShuffled deck: ")
stream$putl(po, deck$unparse(d))
% deal some cards
stream$puts(po, "\nDealing 10 cards:")
for i: int in int$from_to(1, 10) do
stream$puts(po, " " || card$unparse(deck$deal(d)))
end
% show remaining deck
stream$putl(po, "\n\nRemaining cards in deck:")
stream$putl(po, deck$unparse(d))
end start_up</lang>
{{out}}
<pre>New deck:
AC 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC
AH 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH
AS 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS
AD 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD
 
Shuffled deck:
QS 5D 5C 3H 8D 8C 2D 4H 3S QH 8H JS 9H
9C AH 4S 7S 8S 9S 2H AD QC 9D 5S 7C 10S
5H 4C KC 6S 4D 3C KS 6C JC 2C QD 7H AC
AS 10H 6H 3D KD 10D JD 7D JH KH 6D 2S 10C
 
Dealing 10 cards: QS 5D 5C 3H 8D 8C 2D 4H 3S QH
 
Remaining cards in deck:
8H JS 9H 9C AH 4S 7S 8S 9S 2H AD QC 9D
5S 7C 10S 5H 4C KC 6S 4D 3C KS 6C JC 2C
QD 7H AC AS 10H 6H 3D KD 10D JD 7D JH KH
6D 2S 10C</pre>
 
=={{header|COBOL}}==
2,099

edits