RosettaCodeData/Task/Playing-cards/Icon/playing-cards.icon
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

53 lines
1.7 KiB
Text

procedure main(arglist)
cards := 2 # cards per hand
players := 5 # players to deal to
write("New deck : ", showcards(D := newcarddeck())) # create and show a new deck
write("Shuffled : ", showcards(D := shufflecards(D))) # shuffle it
H := list(players)
every H[1 to players] := [] # hands for each player
every ( c := 1 to cards ) & ( p := 1 to players ) do
put(H[p], dealcard(D)) # deal #players hands of #cards
every write("Player #",p := 1 to players,"'s hand : ",showcards(H[p]))
write("Remaining: ",showcards(D)) # show the rest of the deck
end
record card(suit,pip) #: datatype for a card suit x pip
procedure newcarddeck() #: return a new standard deck
local D
every put(D := [], card(suits(),pips()))
return D
end
procedure suits() #: generate suits
suspend !["H","S","D","C"]
end
procedure pips() #: generate pips
suspend !["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
end
procedure shufflecards(D) #: shuffle a list of cards
every !D :=: ?D # see INL#9
return D
end
procedure dealcard(D) #: deal a card (from the top)
return get(D)
end
procedure showcards(D) #: return a string of all cards in the given list (deck/hand/etc.)
local s
every (s := "") ||:= card2string(!D) || " "
return s
end
procedure card2string(x) #: return a string version of a card
return x.pip || x.suit
end