Playing cards: Difference between revisions

Added Easylang
mNo edit summary
(Added Easylang)
 
(27 intermediate revisions by 13 users not shown)
Line 24:
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun remove-nth (i xs)
(if (or (zp i) (endp (rest xs)))
(rest xs)
Line 76:
(defun draw-from-deck (deck)
(mv (first deck) (rest deck)))
</syntaxhighlight>
</lang>
 
Example (first 4 cards of a shuffled deck):
Line 88:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
DEFINE MAXDECKSIZE="52"
 
Line 243:
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Playing_cards.png Screenshot from Atari 8-bit computer]
Line 284:
* [http://www.csail.mit.edu/timeline/timeline.php?query=event&id=93 LOC was essential; Algol 68's REF was a mistake.]
 
<langsyntaxhighlight lang="algol68">MODE CARD = STRUCT(STRING pip, suit); # instance attributes #
 
# class members & attributes #
Line 358:
(init OF class deck)(deck);
(shuffle OF class deck)(deck);
print (((repr OF class deck)(deck), new line))</langsyntaxhighlight>
 
{{Out|Example output}}
<pre>((King OF Clubs), (6 OF Hearts), (7 OF Diamonds), (Ace OF Hearts), (9 OF Spades), (10 OF Clubs), (Ace OF Spades), (8 OF Clubs), (4 OF Spades), (8 OF Hearts), (Jack OF Hearts), (3 OF Clubs), (7 OF Hearts), (10 OF Hearts), (Jack OF Clubs), (Ace OF Clubs), (King OF Spades), (9 OF Clubs), (7 OF Spades), (5 OF Spades), (7 OF Clubs), (Queen OF Clubs), (9 OF Diamonds), (2 OF Spades), (6 OF Diamonds), (Ace OF Diamonds), (Queen OF Diamonds), (5 OF Hearts), (4 OF Clubs), (5 OF Clubs), (4 OF Hearts), (3 OF Diamonds), (4 OF Diamonds), (3 OF Hearts), (King OF Diamonds), (2 OF Clubs), (Jack OF Spades), (2 OF Diamonds), (5 OF Diamonds), (Queen OF Spades), (10 OF Diamonds), (King OF Hearts), (Jack OF Diamonds), (Queen OF Hearts), (8 OF Spades), (2 OF Hearts), (8 OF Diamonds), (10 OF Spades), (9 OF Hearts), (6 OF Clubs), (3 OF Spades), (6 OF Spades))</pre>
 
=={{header|APL}}==
 
<syntaxhighlight lang="apl">
deck←,'23456789TJQKA'∘.,'CDSH'
shuffle←{⍵[?⍨⍴⍵]}
</syntaxhighlight>
 
{{out}}
<pre>
deck
2C 2D 2S 2H 3C 3D 3S 3H 4C ..
shuffle deck
7H 3D 2H 9D KS 7C JH TC JS ..
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">ranks: 1..13
suits: [`♣`, `♦`, `♥`, `♠`]
 
define :card [rank, suit][
init: [
ensure ->
and? -> contains? ranks this\rank
-> contains? suits this\suit
]
 
print: [
R: ø
case [this\rank=]
when? [1] -> R: `A`
when? [11]-> R: `J`
when? [12]-> R: `Q`
when? [13]-> R: `K`
else -> R: this\rank
~{|R||this\suit|}
]
]
 
define :deck [][
init: [
this\cards: []
loop ranks 'rank ->
loop suits 'suit ->
this\cards: this\cards ++ to :card @[rank, suit]
]
]
 
shuffleUp: function [this :deck][
this\cards: shuffle this\cards
]
 
deal: function [this :deck, cnt :integer][
if cnt > size this\cards ->
panic "Not enough cards in deck"
 
cards: []
do.times: cnt [
dealt: sample this\cards
'cards ++ dealt
this\cards: remove this\cards dealt
]
return cards
]
 
; create a new deck
Deck: to :deck []
 
; and shuffle it
shuffleUp Deck
 
; deal 5 cards
print deal Deck 5</syntaxhighlight>
 
{{out}}
 
<pre>A♥ 6♣ 7♣ Q♦ K♥</pre>
 
=={{header|ATS}}==
<syntaxhighlight lang="ats">
<lang ATS>
(* ****** ****** *)
//
Line 805 ⟶ 883:
 
(* ****** ****** *)
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">suits := ["♠", "♦", "♥", "♣"]
values := [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
Gui, font, s14
Line 891 ⟶ 969:
GuiControl,, Deck, % deck
return
;-----------------------------------------------</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
<lang AutoIt>
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
Line 946 ⟶ 1,024:
Print()
#EndRegion ;#### USAGE ####
</syntaxhighlight>
</lang>
{{Out|Example output}}
<pre>
Line 956 ⟶ 1,034:
 
=={{header|BASIC}}==
==={{header|QuickBASIC}}===
{{works with|QuickBASIC|QBX 7.1}}
 
Most BASICs aren't object-oriented (or anything even resembling such) and can't do a deck of cards as a single cohesive unit -- but we can fake it.
 
<langsyntaxhighlight lang="qbasic">DECLARE SUB setInitialValues (deck() AS STRING * 2)
DECLARE SUB shuffle (deck() AS STRING * 2)
DECLARE SUB showDeck (deck() AS STRING * 2)
Line 1,023 ⟶ 1,102:
deck(L0) = shuffled(L0)
NEXT
END SUB</langsyntaxhighlight>
 
{{out}}
Sample output:
<pre>
Dealt: 7D
Dealt: 6D
Line 1,034 ⟶ 1,115:
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH 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
</pre>
 
=={{header|BASIC256}}==
{{trans|Run BASIC}}
<syntaxhighlight lang="vb">suite$ = "CDHS" #Club, Diamond, Heart, Spade
card$ = "A23456789TJQK" #Cards Ace to King
card = 0
 
dim n(55) #make ordered deck
for i = 1 to 52 # of 52 cards
n[i] = i
next i
 
for i = 1 to 52 * 3 #shuffle deck 3 times
i1 = int(rand * 52) + 1
i2 = int(rand * 52) + 1
h2 = n[i1]
n[i1] = n[i2]
n[i2] = h2
next i
 
for hand = 1 to 4 #4 hands
for deal = 1 to 13 #deal each 13 cards
card += 1 #next card in deck
s = (n[card] mod 4) + 1 #determine suite
c = (n[card] mod 13) + 1 #determine card
print mid(card$,c,1);mid(suite$,s,1);" "; #show the card
next deal
print
next hand
end
 
function word$(sr$, wn, delim$)
j = wn
if j = 0 then j += 1
res$ = "" : s$ = sr$ : d$ = delim$
if d$ = "" then d$ = " "
sd = length(d$) : sl = length(s$)
while true
n = instr(s$,d$) : j -= 1
if j = 0 then
if n=0 then res$=s$ else res$=mid(s$,1,n-1)
return res$
end if
if n = 0 then return res$
if n = sl - sd then res$ = "" : return res$
sl2 = sl-n : s$ = mid(s$,n+1,sl2) : sl = sl2
end while
return res$
end function</syntaxhighlight>
{{out}}
<pre>Same as Run BASIC entry.</pre>
 
=={{header|Batch File}}==
The data structure is a simple string with 3 characters per card, one char for the suit, a char for the pip (T) means ten and a last char not printed that would allow to sort a hand.
<langsyntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
 
Line 1,135 ⟶ 1,268:
set arr=!arr:@@@=%temp2%!
set "%~1=!arr!"
exit /b</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,175 ⟶ 1,308:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM Deck{ncards%, card&(51)}, Suit$(3), Rank$(12)
Suit$() = "Clubs", "Diamonds", "Hearts", "Spades"
Rank$() = "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", \
Line 1,221 ⟶ 1,354:
REM Return the name of a card:
DEF FNcardname(card&)
= Rank$(card& >> 2) + " of " + Suit$(card& AND 3)</langsyntaxhighlight>
Output:
<pre>Creating a new deck...
Line 1,241 ⟶ 1,374:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
Line 1,334 ⟶ 1,467:
//free(d);
return 0;
}</langsyntaxhighlight>output
<pre>
New deck, 52 cards: ♠A ♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K ♥A ♥2 ...
Line 1,348 ⟶ 1,481:
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 1,409 ⟶ 1,542:
 
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}</langsyntaxhighlight>
 
=={{header|C++}}==
=== Text version ===
<langsyntaxhighlight lang="cpp">#include <deque>
#include <algorithm>
#include <ostream>
Line 1,488 ⟶ 1,621:
return os;
}
}</langsyntaxhighlight>
 
main.cpp:
<langsyntaxhighlight lang="cpp">#include <iostream>
 
int main(int argc, const char** args) {
Line 1,499 ⟶ 1,632:
 
return 0;
}</langsyntaxhighlight>
 
=== Unicode version ===
{{Works with|Unicode|6 and above}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 1,534 ⟶ 1,667:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>🂢🃞🃓🃔🂧🃙🂵🂪🃇🃎🂩🃚🃕
Line 1,542 ⟶ 1,675:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">import com.vasileff.ceylon.random.api { ... }
 
"""Run the example code for Rosetta Code ["Playing cards" task] (http://rosettacode.org/wiki/Playing_cards)."""
Line 1,622 ⟶ 1,755:
}
}
}</langsyntaxhighlight>
 
Output:
Line 1,648 ⟶ 1,781:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(def suits [:club :diamond :heart :spade])
(def pips [:ace 2 3 4 5 6 7 8 9 10 :jack :queen :king])
 
Line 1,659 ⟶ 1,792:
(println (format "%s of %ss"
(if (keyword? pip) (name pip) pip)
(name suit)))))</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight 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</syntaxhighlight>
{{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}}==
{{works with|GnuCOBOL}}
<langsyntaxhighlight COBOLlang="cobol"> identification division.
program-id. playing-cards.
 
Line 1,840 ⟶ 2,113:
 
end program playing-cards.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,898 ⟶ 2,171:
=={{header|CoffeeScript}}==
 
<langsyntaxhighlight lang="coffeescript">#translated from JavaScript example
class Card
constructor: (@pip, @suit) ->
Line 1,922 ⟶ 2,195:
@cards[i] = @cards.splice(randomCard, 1, card)[0]
deal: -> @cards.shift()</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 1,928 ⟶ 2,201:
A card is a cons of a suit and a pip. A deck is a list of cards, and so dealing is simply popping off of a deck. Shuffling is naïve, just a sort with a random predicate. Printing is built in.
 
<langsyntaxhighlight lang="lisp">(defconstant +suits+
'(club diamond heart spade)
"Card suits are the symbols club, diamond, heart, and spade.")
Line 1,949 ⟶ 2,222:
(sort list #'(lambda (x y)
(declare (ignore x y))
(zerop (random 2)))))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
std.range, std.random;
 
Line 1,982 ⟶ 2,254:
while (!d.empty)
d.dealCard.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>Deck:
Line 2,092 ⟶ 2,364:
 
===More Refined Version===
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.algorithm, std.string, std.range;
 
struct Card {
Line 2,202 ⟶ 2,474:
g.sortDeck.showDeck;
}
}</langsyntaxhighlight>
{{out}}
<pre>Host
Line 2,240 ⟶ 2,512:
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">program Cards;
 
{$APPTYPE CONSOLE}
Line 2,364 ⟶ 2,636:
 
Readln;
end.</langsyntaxhighlight>
 
=={{header|E}}==
See [[Playing Cards/E]]
 
=={{header|EasyLang}}==
<syntaxhighlight>
global deck[] top .
proc new . .
deck[] = [ ]
for i to 52
deck[] &= i
.
top = 52
.
suit$[] = [ "♠" "♦" "♥" "♣" ]
val$[] = [ 2 3 4 5 6 7 8 9 10 "J" "Q" "K" "A" ]
func$ name card .
return suit$[card mod1 4] & val$[card div1 4]
.
proc show . .
for i to top
write name deck[i] & " "
.
print ""
print ""
.
proc shuffle . .
for i = 52 downto 2
r = randint i
swap deck[i] deck[r]
.
top = 52
.
func deal .
top -= 1
return deck[top + 1]
.
new
show
shuffle
show
for i to 10
write name deal & " "
.
print ""
print ""
show
</syntaxhighlight>
 
{{out}}
<pre>
♠2 ♦2 ♥2 ♣2 ♠3 ♦3 ♥3 ♣3 ♠4 ♦4 ♥4 ♣4 ♠5 ♦5 ♥5 ♣5 ♠6 ♦6 ♥6 ♣6 ♠7 ♦7 ♥7 ♣7 ♠8 ♦8 ♥8 ♣8 ♠9 ♦9 ♥9 ♣9 ♠10 ♦10 ♥10 ♣10 ♠J ♦J ♥J ♣J ♠Q ♦Q ♥Q ♣Q ♠K ♦K ♥K ♣K ♠A ♦A ♥A ♣A
 
♦2 ♣2 ♦6 ♠10 ♦5 ♥3 ♣4 ♦7 ♣9 ♥2 ♣7 ♣K ♦K ♠Q ♠2 ♦Q ♥7 ♥8 ♣8 ♥A ♠3 ♥10 ♥Q ♣10 ♠K ♠5 ♦8 ♠9 ♠4 ♣5 ♣J ♥5 ♠J ♠7 ♦4 ♦3 ♦10 ♥6 ♣Q ♥4 ♠6 ♣3 ♥K ♦J ♠A ♠8 ♥J ♥9 ♣A ♦9 ♣6 ♦A
 
♦A ♣6 ♦9 ♣A ♥9 ♥J ♠8 ♠A ♦J ♥K
 
♦2 ♣2 ♦6 ♠10 ♦5 ♥3 ♣4 ♦7 ♣9 ♥2 ♣7 ♣K ♦K ♠Q ♠2 ♦Q ♥7 ♥8 ♣8 ♥A ♠3 ♥10 ♥Q ♣10 ♠K ♠5 ♦8 ♠9 ♠4 ♣5 ♣J ♥5 ♠J ♠7 ♦4 ♦3 ♦10 ♥6 ♣Q ♥4 ♠6 ♣3
</pre>
 
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Card do
defstruct pip: nil, suit: nil
end
Line 2,411 ⟶ 2,739:
end
 
Playing_cards.task</langsyntaxhighlight>
 
{{out}}
Line 2,425 ⟶ 2,753:
 
This module is used by [[Go_Fish]].
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( playing_cards ).
 
Line 2,464 ⟶ 2,792:
 
suites() -> ["Clubs", "Hearts", "Spades", "Diamonds"].
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,478 ⟶ 2,806:
Since our data structure is both a sequence and a stack, we can leverage the existing <code>randomize</code> word for shuffling and <code>pop</code> word for dealing.
 
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel math qw random sequences
vectors ;
IN: rosetta-code.playing-cards
Line 2,496 ⟶ 2,824:
randomize ! shuffle the deck
dup pop drop ! deal from the deck (and discard)
print-deck ! print the deck</langsyntaxhighlight>
{{out}}
<pre>
Line 2,506 ⟶ 2,834:
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">
enum class Suit { clubs, diamonds, hearts, spades }
enum class Pips { ace, two, three, four, five,
Line 2,573 ⟶ 2,901:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">require random.fs \ RANDOM ( n -- 0..n-1 ) is called CHOOSE in other Forths
 
create pips s" A23456789TJQK" mem,
Line 2,608 ⟶ 2,936:
new-deck shuffle .deck
5 .hand
cards-left . \ 47</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">MODULE Cards
 
IMPLICIT NONE
Line 2,677 ⟶ 3,005:
END SUBROUTINE Print_deck
 
END MODULE Cards</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="fortran">PROGRAM Playing_Cards
USE Cards
Line 2,689 ⟶ 3,017:
CALL Print_deck
END PROGRAM</langsyntaxhighlight>
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|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define NSWAPS 100
 
type card
Line 2,751 ⟶ 3,079:
next j
shuffle_deck(deck()) 'shuffle the remaining cards
print_deck(deck()) 'display the last ten cards</langsyntaxhighlight>
{{out}}<pre>Dealing a card: King of Diamonds
Ace of Spades
Line 2,765 ⟶ 3,093:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package cards
 
import (
Line 2,938 ⟶ 3,266:
}
return hands, true
}</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,984 ⟶ 3,312:
fmt.Println("\nReturning the cards to the deck")
fmt.Println(d)
}</langsyntaxhighlight>
Output:
<pre>
Line 3,029 ⟶ 3,357:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">import groovy.transform.TupleConstructor
 
enum Pip {
Line 3,065 ⟶ 3,393:
 
String toString() { cards.isEmpty() ? "Empty Deck" : "Deck $cards" }
}</langsyntaxhighlight>
Test Code
<langsyntaxhighlight lang="groovy">Deck deck = new Deck()
deck.shuffle()
(0..<5).each { println deck.deal() }</langsyntaxhighlight>
Output:
<pre>TWO of DIAMONDS
Line 3,080 ⟶ 3,408:
 
Straightforward implementation with explicit names for pips and suits. A deck is just a list of cards. Dealing consists of splitting off cards from the beginning of the list by the usual pattern matching (not shown). Printing is automatic. Purely functional shuffling is a bit tricky, so here we just do the naive quadratic version. This also works for other than full decks.
<langsyntaxhighlight lang="haskell">import System.Random
 
data Pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
Line 3,106 ⟶ 3,434:
shuffle' g [] _ ys = ys
shuffle' g (x:xs) n ys = shuffle' g' xs (n+1) (insertAt k x ys) where
(k,g') = randomR (0,n) g</langsyntaxhighlight>
 
=={{header|Hy}}==
Line 3,113 ⟶ 3,441:
A simple program that deals out two five card hands.
 
<langsyntaxhighlight Hylang="hy">(import [random [shuffle]])
(setv pips (.split "2 3 4 5 6 7 8 9 10 J Q K A"))
(setv suits (.split "♥ ♦ ♣ ♠"))
Line 3,141 ⟶ 3,469:
(print "\nThe first hand delt was:" (.join " " (map str first_hand)))
(print "\nThe second hand delt was:" (.join " " (map str second_hand)))
(print "\nThe remaining cards in the deck are...\n" (.join " " (map str deck)))))</langsyntaxhighlight>
 
Sample Output:
Line 3,153 ⟶ 3,481:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
 
cards := 2 # cards per hand
Line 3,205 ⟶ 3,533:
procedure card2string(x) #: return a string version of a card
return x.pip || x.suit
end</langsyntaxhighlight>
Sample output:
<pre>New deck : 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
Line 3,218 ⟶ 3,546:
=={{header|J}}==
'''Solution:'''<br>
<langsyntaxhighlight lang="j">NB. playingcards.ijs
NB. Defines a Rosetta Code playing cards class
NB. Multiple decks may be used, one for each instance of this class.
Line 3,259 ⟶ 3,587:
)
 
newDeck_z_=: conew&'rcpc'</langsyntaxhighlight>
 
'''Example use:'''
<langsyntaxhighlight lang="j"> load '~user/playingcards.ijs'
coinsert 'rcpc' NB. insert rcpc class in the path of current locale
pc=: newDeck ''
Line 3,284 ⟶ 3,612:
42 2
destroy__pc ''
1</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
 
<langsyntaxhighlight lang="java">public enum Pip { Two, Three, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King, Ace }</langsyntaxhighlight>
<langsyntaxhighlight lang="java">public enum Suit { Diamonds, Spades, Hearts, Clubs }</langsyntaxhighlight>
 
The card:
<langsyntaxhighlight lang="java">public class Card {
private final Suit suit;
private final Pip value;
Line 3,306 ⟶ 3,634:
return value + " of " + suit;
}
}</langsyntaxhighlight>
The deck:
<langsyntaxhighlight lang="java">import java.util.Collections;
import java.util.LinkedList;
 
Line 3,331 ⟶ 3,659:
return deck.toString();
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">function Card(pip, suit) {
this.pip = pip;
this.suit = suit;
Line 3,364 ⟶ 3,692:
return this.deck.shift();
};
}</langsyntaxhighlight>
 
=={{header|jq}}==
In the following, dealing of cards is accomplished in the conventional manner,
that is in a round-robin fashion whereby in each round, each player is dealt one card from the top of the deck in turn.
 
Since neither the C nor the Go implementation of jq currently has a built-in
PRNG,
this program assumes an invocation such as:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | $JQ -MRcnr -f playing-cards.jq
</pre>
where $JQ can be either jq or gojq. If gojq is used, the def of
`_nwise` given below should be uncommented.
 
<syntaxhighlight lang=jq>
# Uncomment for gojq:
# def _nwise($n):
# def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
# nw;
 
# Output: a prn in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def knuthShuffle:
length as $n
| if $n <= 1 then .
else {i: $n, a: .}
| until(.i == 0;
.i += -1
| (.i + 1 | prn) as $j
| .a[.i] as $t
| .a[.i] = .a[$j]
| .a[$j] = $t)
| .a
end;
 
def Pip: ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
 
def Suit: ["♦", "♣", "♥", "♠"];
 
def Card(pip; suit): pip + suit;
 
def Deck: Card(Pip[]; Suit[]);
 
# Deal one round of the cards in .deck to the players
# represented by .hands,
# but if there are not enough cards, do nothing except add .error to the input
# Input {deck, hands}
def deal:
(.hands | length) as $nplayers
| if $nplayers > (.deck|length) then .error = "not enough cards"
else reduce range(0;$nplayers) as $i (.; .hands[$i] += [.deck[$i]])
| .deck |= .[$nplayers:]
 
# Deal $n cards to $nplayers players if there are enough cards in the input deck
# Input: a deck
# Output: {deck, hands}
def deal($n; $nplayers):
if $n * $nplayers > length then "deal/2: not enough cards" | error
else {deck: ., hands: [range(0; $nplayers)|[]]}
| until( .hands[0]|length == $n; deal)
end ;
 
# display an entire deck or else just the cards
def display:
if length == 52 then _nwise(13) | join(" ")
else join(" ")
end;
 
def task:
Deck
| "After creation, the deck consists of:", display, "",
(knuthShuffle
| "After shuffling:", display, "",
( deal(5; 4)
| "After dealing 5 cards each to 4 players, the hands are:",
(.hands[] | display), "", "... leaving \(.deck|length) cards") ) ;
 
task
</syntaxhighlight>
{{output}}
An example.
<pre>
After creation, the deck consists of:
A♦ 2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦
A♣ 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣
A♥ 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥
A♠ 2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠
 
After shuffling:
9♦ 7♦ J♥ 7♥ 4♥ 3♠ 4♣ 2♦ 2♠ 6♦ Q♠ 8♦ A♣
8♥ 4♦ J♣ 5♠ 3♣ Q♣ 3♥ 7♠ 9♥ 8♣ 10♠ 2♥ A♠
Q♦ 5♦ 10♦ J♠ K♥ 5♣ A♦ 10♥ 10♣ A♥ 2♣ 9♣ 7♣
K♣ 6♠ K♦ 9♠ J♦ 5♥ Q♥ 4♠ K♠ 8♠ 6♣ 6♥ 3♦
 
After dealing 5 cards each to 4 players, the hands are:
9♦ 4♥ 2♠ A♣ 5♠
7♦ 3♠ 6♦ 8♥ 3♣
J♥ 4♣ Q♠ 4♦ Q♣
7♥ 2♦ 8♦ J♣ 3♥
 
... leaving 32 cards
</pre>
 
=={{header|Julia}}==
Line 3,371 ⟶ 3,808:
A deck consists of an array of integers and a <tt>DeckDesign</tt> type, which defines the meanings of the cards. This is a somewhat simplified implementation. While strictly speaking, a deck should be an array of cards, it is sufficient here to use integers. Indeed, cards and hands are just decks with smaller numbers of cards.
 
<syntaxhighlight lang="julia">
<lang Julia>
type DeckDesign{T<:Integer,U<:String}
rlen::T
Line 3,397 ⟶ 3,834:
Deck(collect(1:des.rlen*des.slen), des)
end
</syntaxhighlight>
</lang>
 
'''Define a Few of the Standard Methods'''
Line 3,403 ⟶ 3,840:
Many of these definitions are simply passed through to the card array component of the deck. But note that <tt>size</tt> returns parameters appropriate to a complete deck. This behavior is helpful when assigning meaning to any list of cards.
<syntaxhighlight lang="julia">
<lang Julia>
Base.isempty(d::Deck) = isempty(d.cards)
Base.empty!(d::Deck) = empty!(d.cards)
Line 3,418 ⟶ 3,855:
join(r.*s, " ")
end
</syntaxhighlight>
</lang>
 
'''Define Some Special Methods'''
Line 3,424 ⟶ 3,861:
<tt>deal!</tt> is the only deck specific method required to complete the essentials for this task.
 
<syntaxhighlight lang="julia">
<lang Julia>
function deal!{T<:Integer}(d::Deck, hlen::T)
if hlen < length(d)
Line 3,450 ⟶ 3,887:
chop(s)
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
d = fresh(pokerlayout())
println("A new poker deck:")
Line 3,474 ⟶ 3,911:
println("And now the deck contains:")
println(pretty(d))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,506 ⟶ 3,943:
 
Create the deck.
<langsyntaxhighlight Klang="k"> v:"A23456789TJQK" / values
s:"SHCD" / suites
 
/ create a new deck
newdeck:{deck::,/s,'\:v}
newdeck();</langsyntaxhighlight>
 
Show the deck.
<langsyntaxhighlight Klang="k"> show:{`0:$,/-3$$deck}
 
show()
SA S2 S3 S4 S5 S6 S7 S8 S9 ST SJ SQ SK HA H2 H3 H4 H5 H6 H7 H8 H9 HT HJ HQ HK CA C2 C3 C4 C5 C6 C7 C8 C9 CT CJ CQ CK DA D2 D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK </langsyntaxhighlight>
 
Shuffle the deck.
<langsyntaxhighlight Klang="k"> shuffle:{deck::(-#deck)?deck}
 
shuffle();show()
S8 CA D5 D2 SJ D6 DJ H7 S4 S9 SQ SK S5 D8 C4 HT DA H3 S6 S2 DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8 </langsyntaxhighlight>
 
Deal: Get the N top cards and remove them from the deck.
Deal 5 cards.
<langsyntaxhighlight Klang="k"> deal1:{|((#deck)-x)_|deck}
deal:{c:deal1[x];deck::(deck _dvl c);c}
 
Line 3,538 ⟶ 3,975:
 
#deck / 5 cards are removed
47</langsyntaxhighlight>
 
Deal 3 more hands.
<langsyntaxhighlight Klang="k"> {deal@5}'!3
(("D6"
"DJ"
Line 3,556 ⟶ 3,993:
"H3"
"S6"
"S2"))</langsyntaxhighlight>
 
We now have 32 cards left.
<langsyntaxhighlight Klang="k"> #deck
32
show()
DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8</langsyntaxhighlight>
 
=={{header|Kotlin}}==
=== procedural style ===
{{Works with|Kotlin|1.3.50}}
<langsyntaxhighlight lang="scala">const val FACES = "23456789TJQKA"
const val SUITS = "shdc"
 
Line 3,600 ⟶ 4,037:
println("\nThe 10 cards dealt from the bottom of the deck are:")
printDeck(dealtBottom)
}</langsyntaxhighlight>
Sample output:
{{out}}
Line 3,624 ⟶ 4,061:
=== object-oriented ===
{{Works with|Kotlin|1.4.10}}
<langsyntaxhighlight lang="scala">class Deck : ArrayList<String> {
constructor() { FACES.forEach { face -> SUITS.forEach { add("$face$it") } } }
constructor(c: Collection<String>) { addAll(c) }
Line 3,655 ⟶ 4,092:
println("\nThe 10 cards dealt from the bottom of the deck are:")
deck.dealBottom(10).print()
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb"> Dim deckCards(52)
Dim holdCards(1, 1)
Print "The Sorted Deck"
Line 3,749 ⟶ 4,186:
pipLabel$ = "Ace Deuce Three Four Five Six Seven Eight Nine Ten Jack Queen King"
pip$ = Word$(pipLabel$, faceValue)
End Function</langsyntaxhighlight>
 
=={{header|Logo}}==
{{works with|UCB Logo}}
<langsyntaxhighlight lang="logo">make "suits {Diamonds Hearts Clubs Spades}
make "pips {Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King}
 
Line 3,785 ⟶ 4,222:
new.deck
shuffle.deck
repeat 5 [deal.card]</langsyntaxhighlight>
 
=={{header|Lua}}==
===Version 1===
<syntaxhighlight lang="lua">
<lang Lua>
suits = {"Clubs", "Diamonds", "Hearts", "Spades"}
faces = {2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"}
Line 3,854 ⟶ 4,291:
print(d - 4 .. "")
print(-b .. "")
</syntaxhighlight>
</lang>
 
===Version 2===
<langsyntaxhighlight Lualang="lua">local tPlayers = {} -- cards of players
local tBoard = {} -- cards in a board
local nPlayers = 5 -- number of players
Line 3,915 ⟶ 4,352:
 
print('ALL CARDS IN THE DECK\n', table.concat(tDeck, ' '), '\n')
</syntaxhighlight>
</lang>
 
Output:
<langsyntaxhighlight Lualang="lua">FRESH DECK
2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac
 
Line 3,938 ⟶ 4,375:
ALL CARDS IN THE DECK
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h 9s 6c Qc 2c Jd Ac Th 4s Qs Ad Qh 4c 3s As Kc
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
We can use one or more packs. When we need a card and deck has no card then a new pack inserted (automatic drop some random cards)
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module PlayCards {
Font "Arial" ' Ensure characters exist for Suits
Line 4,051 ⟶ 4,488:
}
PlayCards
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`randSeed',141592653)dnl
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
Line 4,093 ⟶ 4,530:
deal deal(`b')
deal deal(`b')
show(`b')</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">MakeDeck[] := Tuples[{{"Ace ", 2, 3 , 4 , 5, 6 , 7 , 8 , 9 , 10, "Jack" , "Queen", "King"}, {♦ , ♣, ♥ , ♠}}]
DeckShuffle[deck_] := RandomSample[deck, Length@deck]
DealFromDeck[] := (Print@First@deck; deck = deck[[2 ;; All]];)</langsyntaxhighlight>
Example usage:
<pre>deck = DeckShuffle@MakeDeck[]; Print[deck]
Line 4,123 ⟶ 4,560:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
pips = ["Ace","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King"]
Line 4,169 ⟶ 4,606:
print
print deck.len + " cards left in deck:"
display deck</langsyntaxhighlight>
{{out}}
<pre>Deck created. Cards in Deck: 52
Line 4,192 ⟶ 4,629:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import random, strutils
 
type
Line 4,282 ⟶ 4,719:
hands[0].add deck.draw()
echo "Player 1 hand: ", hands[0]
echo "Remaining cards: ", deck</langsyntaxhighlight>
 
{{out}}
Line 4,305 ⟶ 4,742:
=={{header|OCaml}}==
Straightforward implementation with algebraic types for the pips and suits, and lists of their values. A deck is an array of cards.
<langsyntaxhighlight lang="ocaml">type pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
Jack | Queen | King | Ace
let pips = [Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
Line 4,319 ⟶ 4,756:
deck.(i) <- deck.(j);
deck.(j) <- temp
done</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Uses a global variable v to pass around the remaining cards in the deck. If used inside a function this would be a good case for a dynamically-scoped variable (<code>local</code>) rather than the typically-preferred lexical scoping of <code>my</code>.
<langsyntaxhighlight lang="parigp">name(n)=Str(["A",2,3,4,5,6,7,8,9,10,"J","Q","K"][(n+3)>>2],["h","d","s","c"][n%4+1]);
newdeck()={
v=vector(52,i,i);
Line 4,342 ⟶ 4,779:
);
v
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 4,348 ⟶ 4,785:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">package Playing_Card_Deck;
 
use strict;
use warnings;
 
@Playing_Card_Deck::suits = qw
Line 4,366 ⟶ 4,804:
{my $invocant = shift;
my $class = ref($invocant) || $invocant;
my @cards = ();
foreach my $suit (@Playing_Card_Deck::suits)
{foreach my $pip (@Playing_Card_Deck::pips)
Line 4,375 ⟶ 4,813:
# Removes the top card of the given deck and returns it as a hash
# with the keys "suit" and "pip".
{return %{ shift( @{shift(@_)} ) };}
 
sub shuffle
Line 4,389 ⟶ 4,827:
sub print_cards
# Prints out a description of every card in the deck, in order.
{print "$_->{pip} of $_->{suit}\n" foreach @{shift(@_)};}</langsyntaxhighlight>
Some examples of use:
<langsyntaxhighlight lang="perl">my $deck = new Playing_Card_Deck->new;
$deck->shuffle;
my %card = $deck->deal;
print uc("$card{pip} OF $card{suit}\n");
$deck->print_cards;</langsyntaxhighlight>
This creates a new deck, shuffles it, removes the top card, prints out that card's name in all caps, and then prints the rest of the deck.
 
Line 4,401 ⟶ 4,839:
Includes both ascii/console and unicode/gui displays
{{libheader|Phix/pGUI}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Playing_cards.exw</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">nhands</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ncards</span><span style="color: #0000FF;">)</span>
Line 4,479 ⟶ 4,917:
<span style="color: #000000;">console_show</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">gui_show</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
(console)
Line 4,503 ⟶ 4,941:
{{works with|PHP|5.3+}}
Implementation:
<langsyntaxhighlight lang="php">class Card
{
// if unable to save as UTF-8, use other, non-UTF-8, symbols here
Line 4,778 ⟶ 5,216:
return $result[ 0 ];
}
}</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang="php">// if viewing in a browser, lets output a text/plain header with utf-8 charset
header( 'Content-Type: text/plain; charset=utf-8' );
 
Line 4,837 ⟶ 5,275:
 
// show the remaining cards in the deck
echo PHP_EOL . count( $deck ) . ' cards remaining in the deck: ' . PHP_EOL . $deck . PHP_EOL;</langsyntaxhighlight>
 
This will output something like:
Line 4,859 ⟶ 5,297:
24 cards remaining in the deck:
♥A ♥6 ♦2 ♦5 ♥J ♣T ♦3 ♠5 ♠A ♣8 ♥8 ♥7 ♥K ♦7 ♥T ♦6 ♦T ♣6 ♣3 ♣A ♦4 ♥4 ♠6 ♣9</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
% Create and print the deck
deck(Deck),
print_deck(Deck),
nl,
% Shuffle the deck
print_deck(shuffle(Deck)),
nl,
 
% Deal 3 cards
Deck := deal(Deck,Card1),
Deck := deal(Deck,Card2),
Deck := deal(Deck,Card3),
 
println(deal1=Card1),
println(deal2=Card2),
println(deal3=Card3),
% The remaining deck
print_deck(Deck),
nl,
 
% Deal 5 cards
Deck := deal(Deck,Card4),
Deck := deal(Deck,Card5),
Deck := deal(Deck,Card6),
Deck := deal(Deck,Card7),
Deck := deal(Deck,Card8),
 
println(cards4_to_8=[Card4,Card5,Card6,Card7,Card8]),
nl,
 
% Deal 5 cards
Deck := deal_n(Deck,5,FiveCards),
println(fiveCards=FiveCards),
print_deck(Deck),
nl,
 
% And deal some more cards
% This chaining works since deal/1 returns the remaining deck
Deck := Deck.deal(Card9).deal(Card10).deal(Card11).deal(Card12),
println("4_more_cards"=[Card9,Card10,Card11,Card12]),
print_deck(Deck),
 
nl.
 
% suits(Suits) => Suits = ["♠","♥","♦","♣"].
suits(Suits) => Suits = ["C","H","S","D"].
values(Values) => Values = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"].
 
% Create a (sorted) deck.
deck(Deck) =>
suits(Suits),
values(Values),
Deck =[S++V :V in Values, S in Suits].sort().
 
% Shuffle a deck
shuffle(Deck) = Deck2 =>
Deck2 = Deck,
Len = Deck2.length,
foreach(I in 1..Len)
R2 = random(1,Len),
Deck2 := swap(Deck2,I,R2)
end.
 
% Swap position I <=> J in list L
swap(L,I,J) = L2, list(L) =>
L2 = L,
T = L2[I],
L2[I] := L2[J],
L2[J] := T.
 
 
% The first card is returned as the out parameter Card.
% The deck is returned as the function value.
deal(Deck, Card) = Deck.tail() => Card = Deck.first().
 
% Deal N cards
deal_n(Deck, N, Cards) = [Deck[I] : I in Len+1..Deck.length] =>
Len = min(N,Deck.length),
Cards = [Deck[I] : I in 1..Len].
 
% Print deck
print_deck(Deck) =>
println("Deck:"),
foreach({Card,I} in zip(Deck,1..Deck.len))
printf("%w ", Card),
if I mod 10 == 0 then
nl
end
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>Deck:
C2 C3 C4 C5 C6 C7 C8 C9 CA CJ
CK CQ CT D2 D3 D4 D5 D6 D7 D8
D9 DA DJ DK DQ DT H2 H3 H4 H5
H6 H7 H8 H9 HA HJ HK HQ HT S2
S3 S4 S5 S6 S7 S8 S9 SA SJ SK
SQ ST
 
Deck:
SA C2 D7 C7 C4 S8 HT D9 DA H6
D6 S6 H2 C5 H8 ST SJ HQ S9 DQ
D5 C6 CJ H4 S5 HK CK HJ H5 D8
H9 C8 D4 CQ H3 H7 SK S4 DK SQ
CA S7 D2 C9 DJ HA DT S2 C3 CT
D3 S3
 
deal1 = SA
deal2 = C2
deal3 = D7
Deck:
C7 C4 S8 HT D9 DA H6 D6 S6 H2
C5 H8 ST SJ HQ S9 DQ D5 C6 CJ
H4 S5 HK CK HJ H5 D8 H9 C8 D4
CQ H3 H7 SK S4 DK SQ CA S7 D2
C9 DJ HA DT S2 C3 CT D3 S3
 
cards4_to_8 = [C7,C4,S8,HT,D9]
 
fiveCards = [DA,H6,D6,S6,H2]
Deck:
C5 H8 ST SJ HQ S9 DQ D5 C6 CJ
H4 S5 HK CK HJ H5 D8 H9 C8 D4
CQ H3 H7 SK S4 DK SQ CA S7 D2
C9 DJ HA DT S2 C3 CT D3 S3
 
4_more_cards = [C5,H8,ST,SJ]
Deck:
HQ S9 DQ D5 C6 CJ H4 S5 HK CK
HJ H5 D8 H9 C8 D4 CQ H3 H7 SK
S4 DK SQ CA S7 D2 C9 DJ HA DT
S2 C3 CT D3 S3 </pre>
 
 
=={{header|PicoLisp}}==
{{trans|Common Lisp}}
<langsyntaxhighlight PicoLisplang="picolisp">(de *Suits
Club Diamond Heart Spade )
 
Line 4,874 ⟶ 5,450:
 
(de shuffle (Lst)
(by '(NIL (rand)) sort Lst) )</langsyntaxhighlight>
 
=={{header|PowerShell}}==
This implementation was designed as a pre-requisite to other cards games. For this reason, this has the ability to have more than one persistent deck, more than one persistent hand, and shuffle the ''same deck'' over and over again or after some cards have been dealt out. There is also a full help file accessed by typing
''Get-Help Any-Function -full''
<syntaxhighlight lang="powershell">
<lang PowerShell>
<#
.Synopsis
Line 5,359 ⟶ 5,936:
}
}
</syntaxhighlight>
</lang>
Results of using ''Draw-Deck'' without creating a hand or deck beforehand.
Note: Due to length I've shortened some of the results, however when run it returns everything:
Line 5,466 ⟶ 6,043:
{{works with|SWI Prolog|4.8.0}}
 
<langsyntaxhighlight Prologlang="prolog">/** <module> Cards
 
A card is represented by the term "card(Pip, Suit)".
Line 5,504 ⟶ 6,081:
print_card(card(Pip, Suit)) :-
format('~a of ~a~n', [Pip, Suit]).
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
This approach keeps track of the cards in an abbrieviated form but allows them to be expanded to a more wordy form when they are dealt or shown.
<langsyntaxhighlight PureBasiclang="purebasic">#MaxCards = 52 ;Max Cards in a deck
Structure card
pip.s
Line 5,641 ⟶ 6,218:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output:
<pre>Dealt: Queen of Hearts
Line 5,656 ⟶ 6,233:
=={{header|Python}}==
===Python 2.x, standalone===
<langsyntaxhighlight lang="python">import random
 
class Card(object):
Line 5,681 ⟶ 6,258:
def deal(self):
self.shuffle() # Can't tell what is next from self.deck
return self.deck.pop(0)</langsyntaxhighlight>
 
===Python 3: extending [[Poker hand analyser#Python]]===
Assume the code from [[Poker hand analyser#Python]] is in a file pokerhand.py and importable.
<langsyntaxhighlight lang="python">from pokerhand import Card, suit, face
from itertools import product
from random import randrange
Line 5,709 ⟶ 6,286:
print(deck.deal(), end=' ')
print()
print('\nThe remaining cards are a', deck)</langsyntaxhighlight>
 
{{out}}
Line 5,800 ⟶ 6,377:
A listing of the code accompanying the chapter, which does the above and more:
 
<langsyntaxhighlight Quackerylang="quackery"> [ /mod if 1+ ] is /up ( n n --> n )
 
[ [] 52 times [ i^ join ] ] is newpack ( --> [ )
Line 5,938 ⟶ 6,515:
again ] drop join ] is riffle ( [ --> [ )
 
[ times riffle ] is riffles ( [ n --> [ )</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">pips <- c("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")
suit <- c("Clubs", "Diamonds", "Hearts", "Spades")
# Create a deck
Line 5,965 ⟶ 6,542:
deck <- deal(deck)
# While no-one is looking, sneakily deal a card from the bottom of the pack
deck <- deal(deck, FALSE)</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight Racketlang="racket">#lang racket
 
;; suits:
Line 5,990 ⟶ 6,567:
(set-box! deck (shuffle (unbox deck))))
 
;; deal a card from tAthe 2 3 4 5 6 7 8 9 10 J Q K>;deck:
enum Suit he deck:
(define (deck-deal deck)
(begin0 (first (unbox deck))
Line 6,002 ⟶ 6,578:
(deck-deal my-deck)
(deck-deal my-deck)
(length (unbox my-deck))</langsyntaxhighlight>
 
{{out}}
Line 6,015 ⟶ 6,591:
(formerly Perl 6)
{{Works with|rakudo|2016.08}}
<syntaxhighlight lang="raku" perl6line>enum Pip <A 2 3 4 5 6 7 8 9 10 J Q K>;
enum Suit <♦ ♣ ♥ ♠>;
Line 6,044 ⟶ 6,620:
 
$d.shuffle;
say "Deck, re-shuffled: ", $d;</langsyntaxhighlight>
{{out}}
<pre>Deck: 3♦ J♦ 4♥ 7♠ 7♣ 7♥ 9♣ K♥ 6♠ 2♦ 3♠ Q♥ 8♥ 2♥ J♥ 5♥ 8♦ 8♣ 6♦ 7♦ 5♦ 2♣ 4♦ 8♠ 9♥ 4♣ 3♥ K♠ 2♠ 5♣ Q♣ Q♦ K♦ 4♠ 9♦ Q♠ 5♠ 6♥ J♣ J♠ K♣ 9♠ 3♣ 6♣
Line 6,051 ⟶ 6,627:
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">
<lang Rebol>
Red [Title: "Playing Cards"]
 
pip: ["a" "2" "3" "4" "5" "6" "7" "8" "9" "10" "j" "q" "k"]
suit: ["♣" "♦" "♥" "♠"]
 
Line 6,065 ⟶ 6,641:
shuffle: function [deck [block!]] [deck: random deck]
 
deal: function [other-deck [block!] deck [block!]] [card:unless takeempty? deck return[append/only cardother-deck take deck]]
 
contents: function [deck [block!]] [
Line 6,079 ⟶ 6,655:
prin "^/^/remaining: "
contents deck
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 6,094 ⟶ 6,670:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 1) Build ordered Card deck
* 2) Create shuffled stack
Line 6,143 ⟶ 6,719:
Say 'Left on shuffled stack:'
Say ' 'subword(ss,1,26) /* and what's left on stack */
Say ' 'subword(ss,27,6)</langsyntaxhighlight>
Output:
<pre>
Line 6,163 ⟶ 6,739:
===version 2===
A check is made to see if ASCII characters can be used to display the suits &nbsp; (if using an ASCII machine).
<langsyntaxhighlight lang="rexx">/*REXX pgm shows a method to build/shuffle/deal 5 cards (using a 52─card deck)──►4 hands*/
box = build(); say ' box of cards:' box /*a brand new standard box of 52 cards.*/
deck= mix(); say 'shuffled deck:' deck /*obtain a randomly shuffled deck. */
Line 6,194 ⟶ 6,770:
@= @ word(_, ?) /*shuffled deck, 1 card at a time.*/
_= delword(_, ?, 1) /*elide just─chosen card from deck*/
end /*mixer*/; return @</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default values of four hands holding five cards:}}
<pre>
Line 6,216 ⟶ 6,792:
The Cards game, Screen shot : http://ring-lang.sourceforge.net/doc/_images/ringqt_shot48.jpg
 
<langsyntaxhighlight lang="ring">
Load "guilib.ring"
 
Line 6,462 ⟶ 7,038:
oTest = new qTest
oTest.qsleep(nTime)
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 6,468 ⟶ 7,044:
 
{{works with|Ruby|2.0.0+}}
<langsyntaxhighlight lang="ruby">class Card
# class constants
SUITS = %i[ Clubs Hearts Spades Diamonds ]
Line 6,518 ⟶ 7,094:
hand = deck.deal(5)
puts hand.join(", ")
puts hand.sort.join(", ")</langsyntaxhighlight>
 
{{out}}
Line 6,526 ⟶ 7,102:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">suite$ = "C,D,H,S" ' Club, Diamond, Heart, Spade
card$ = "A,2,3,4,5,6,7,8,9,T,J,Q,K" ' Cards Ace to King
 
Line 6,550 ⟶ 7,126:
next deal
print
next hand</langsyntaxhighlight>
<pre>TD 7S JD 7C 3H AS 6D QD KH 5H 2C QH 8C
8H 5S 7D 2D 2H 4D KS JS 7H QC KC 9S TH
Line 6,558 ⟶ 7,134:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">extern crate rand;
 
use std::fmt;
Line 6,620 ⟶ 7,196:
println!("{}", deck.deal().unwrap());
}
}</langsyntaxhighlight>
'''Sample output: 5 random cards'''
<pre>Jack of Diamonds
Line 6,629 ⟶ 7,205:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.annotation.tailrec
import scala.util.Random
 
Line 6,663 ⟶ 7,239:
}
override def toString():String="Deck: " + (cards mkString ", ")
}</langsyntaxhighlight>
Usage:
<langsyntaxhighlight lang="scala">val deck=new Deck()
val deckShuffled:Deck=deck.shuffle
 
Line 6,679 ⟶ 7,255:
val (cards, rest3) =deckShuffled deal 6
println(cards)
println(rest3)</langsyntaxhighlight>
Output:
<pre>Deck: Four of Diamonds, Two of Diamonds, Queen of Diamonds, Jack of Diamonds, Nine of Diamonds, Five of Diamonds, Seven of Diamonds, Ten of Diamonds, Six of Diamonds, Ace of Diamonds, King of Diamonds, Three of Diamonds, Eight of Diamonds, Ten of Spades, Seven of Spades, Eight of Spades, King of Spades, Ace of Spades, Three of Spades, Queen of Spades, Jack of Spades, Six of Spades, Five of Spades, Nine of Spades, Four of Spades, Two of Spades, Seven of Hearts, Queen of Hearts, Two of Hearts, Ten of Hearts, Eight of Hearts, Jack of Hearts, Four of Hearts, Nine of Hearts, Six of Hearts, King of Hearts, Five of Hearts, Three of Hearts, Ace of Hearts, King of Clubs, Jack of Clubs, Queen of Clubs, Six of Clubs, Three of Clubs, Two of Clubs, Eight of Clubs, Seven of Clubs, Ace of Clubs, Four of Clubs, Nine of Clubs, Five of Clubs, Ten of Clubs
Line 6,694 ⟶ 7,270:
The procedure <code>shuffle</code> requires an appropriate procedure <code>random</code> to be
defined. Some Scheme implementations provide this as an extension.
<langsyntaxhighlight lang="scheme">(define ranks
(quote (ace 2 3 4 5 6 7 8 9 10 jack queen king)))
 
Line 6,721 ⟶ 7,297:
(syntax-rules ()
((deal! deck hand)
(begin (set! hand (cons (car deck) hand)) (set! deck (cdr deck))))))</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="scheme">(define deck
(shuffle new-deck))
 
Line 6,736 ⟶ 7,312:
 
(display hand)
(newline)</langsyntaxhighlight>
Sample output:
<pre>((jack . hearts) (5 . clubs) (9 . hearts) (7 . clubs) (6 . spades))</pre>
Line 6,744 ⟶ 7,320:
 
DeckOfCards:
<langsyntaxhighlight lang="sensetalk">properties
cards: []
end properties
Line 6,769 ⟶ 7,345:
to handle asText
return my cards joined by return
end asText</langsyntaxhighlight>
Run:
<langsyntaxhighlight lang="sensetalk">put a new DeckOfCards into deck1
get deck1.shuffle
get deck1.deal
put deck1</langsyntaxhighlight>
Output:
<pre>New deck created, number of cards in deck: 52
Line 6,834 ⟶ 7,410:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">define Pip = <A 2 3 4 5 6 7 8 9 10 J Q K>;
define Suit = <♦ ♣ ♥ ♠>;
 
Line 6,864 ⟶ 7,440:
 
d.shuffle;
say "Deck, shuffled: #{d}";</langsyntaxhighlight>
{{out}}
<pre>
Line 6,875 ⟶ 7,451:
===Version 1===
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">Object subclass: #Card
instanceVariableNames: 'thePip theSuit'
classVariableNames: 'pips suits'
Line 6,977 ⟶ 7,553:
 
"create a deck, shuffle it, remove the first card and display it"
Deck new shuffle deal displayNl.</langsyntaxhighlight>
 
'''Note''': there's something odd with the class method for getting pips and suits; it's because I've not understood how to initialize class variables to certain values without the need to explicitly call a method to do so.
Line 6,983 ⟶ 7,559:
===Version 2===
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">Object subclass: Deck [
 
| cards |
Line 7,019 ⟶ 7,595:
'2c' '3c' '4c' '5c' '6c' '7c' '8c' '9c' 'Tc' 'Jc' 'Qc' 'Kc' 'Ac'
) deepCopy]
]</langsyntaxhighlight>
 
The Card class should obviously be more intricate to fulfil the needs of a CardGame class. :-)
 
Use example:
<langsyntaxhighlight Smalltalklang="smalltalk">st> myDeck := Deck of: Card
a Deck
st> myDeck displayNl
Line 7,037 ⟶ 7,613:
5
st> myHand
OrderedCollection ('Ad' 'Jd' 'Ks' 'Th' '6d' )</langsyntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift|5}}
<syntaxhighlight lang="swift">
struct Card: CustomStringConvertible
{
enum Suit: String, CaseIterable, CustomStringConvertible
{
case clubs = "♣️"
case diamonds = "♦️"
case hearts = "♥️"
case spades = "♠️"
 
var description: String { rawValue }
}
 
let suit: Suit
let value: Int
 
var description: String
{
let valueAsString: String
switch value
{
case 1:
valueAsString = "A"
case 11:
valueAsString = "J"
case 12:
valueAsString = "Q"
case 13:
valueAsString = "K"
default:
valueAsString = "\(value)"
}
return valueAsString + suit.description
}
}
 
struct Deck: CustomStringConvertible
{
var cards: [Card] = []
 
init()
{
for suit in Card.Suit.allCases
{
for faceValue in 1 ... 13
{
cards.append(Card(suit: suit, value: faceValue))
}
}
}
 
var description: String
{
String(cards.map{ $0.description }.joined(separator: ", "))
}
 
mutating func shuffle()
{
cards.shuffle()
}
 
mutating func dealCard() -> Card?
{
guard !cards.isEmpty else { return nil }
return cards.removeLast()
}
}
 
var deck = Deck()
print("New deck:")
print(deck)
deck.shuffle()
print("Shuffled deck:")
print(deck)
 
var hands: [[Card]] = [[], [], [], []]
 
var handIndex = 0
 
while let card = deck.dealCard()
{
hands[handIndex].append(card)
handIndex = (handIndex + 1) % hands.count
}
 
print ("Hands:")
print(hands.map({ $0.description }).joined(separator: "\n"))
print("Remaining deck (should be empty):")
print(deck)
</syntaxhighlight>
{{out}}
<pre>
New deck:
A♣️, 2♣️, 3♣️, 4♣️, 5♣️, 6♣️, 7♣️, 8♣️, 9♣️, 10♣️, J♣️, Q♣️, K♣️, A♦️, 2♦️, 3♦️, 4♦️, 5♦️, 6♦️, 7♦️, 8♦️, 9♦️, 10♦️, J♦️, Q♦️, K♦️, A♥️, 2♥️, 3♥️, 4♥️, 5♥️, 6♥️, 7♥️, 8♥️, 9♥️, 10♥️, J♥️, Q♥️, K♥️, A♠️, 2♠️, 3♠️, 4♠️, 5♠️, 6♠️, 7♠️, 8♠️, 9♠️, 10♠️, J♠️, Q♠️, K♠️
Shuffled deck:
2♣️, 2♠️, 8♠️, K♣️, 7♥️, Q♠️, 3♥️, 5♦️, 7♣️, 6♥️, J♥️, 10♣️, 6♠️, 8♥️, A♦️, 6♣️, 10♠️, 9♠️, 2♥️, 7♠️, 3♠️, 6♦️, A♥️, 5♥️, 9♣️, 5♣️, A♣️, 3♣️, 3♦️, 9♥️, Q♥️, 4♣️, 8♣️, K♦️, 10♥️, 4♦️, J♦️, 7♦️, 8♦️, J♣️, Q♦️, 5♠️, 2♦️, 4♥️, 10♦️, 9♦️, K♥️, 4♠️, J♠️, K♠️, Q♣️, A♠️
Hands:
[A♠️, 4♠️, 4♥️, J♣️, 4♦️, 4♣️, 3♣️, 5♥️, 7♠️, 6♣️, 10♣️, 5♦️, K♣️]
[Q♣️, K♥️, 2♦️, 8♦️, 10♥️, Q♥️, A♣️, A♥️, 2♥️, A♦️, J♥️, 3♥️, 8♠️]
[K♠️, 9♦️, 5♠️, 7♦️, K♦️, 9♥️, 5♣️, 6♦️, 9♠️, 8♥️, 6♥️, Q♠️, 2♠️]
[J♠️, 10♦️, Q♦️, J♦️, 8♣️, 3♦️, 9♣️, 3♠️, 10♠️, 6♠️, 7♣️, 7♥️, 2♣️]
Remaining deck (should be empty):
 
</pre>
 
{{works with|Swift 2.0}}
 
Enter this into a playground to see results
<langsyntaxhighlight lang="swift">
import Foundation
 
Line 7,228 ⟶ 7,910:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
namespace eval playing_cards {
Line 7,296 ⟶ 7,978:
playing_cards::print $hand -sort true
puts "\nthe deck:"
playing_cards::print_deck</langsyntaxhighlight>
 
=={{header|VBScript}}==
=====Implementation=====
<syntaxhighlight lang="vb">
<lang vb>
class playingcard
dim suit
Line 7,367 ⟶ 8,049:
end sub
end class
</syntaxhighlight>
</lang>
 
=====Invocation=====
<syntaxhighlight lang="vb">
<lang vb>
dim pack
set pack = new carddeck
Line 7,389 ⟶ 8,071:
end if
 
</syntaxhighlight>
</lang>
 
=====Output=====
Line 7,405 ⟶ 8,087:
Each users hand is another edit buffer.
There is no need for any routines to display the deck or a hand, since each of them is displayed in an edit window.
<langsyntaxhighlight lang="vedit">// Playing Cards, main program
 
Call("CREATE_DECK")
Line 7,487 ⟶ 8,169:
Buf_Switch(#11) // players hand
Reg_ins(9) // insert the cards here
Return</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
 
var FACES = "23456789TJQKA"
Line 7,524 ⟶ 8,206:
var dealtBottom = dealBottomDeck.call(deck, 10)
System.print("\nThe 10 cards dealt from the bottom of the deck are:")
printDeck.call(dealtBottom)</langsyntaxhighlight>
 
{{out}}
Line 7,548 ⟶ 8,230:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">char Deck(52); \card structure (low 2 bits = suit)
def Players = 4; \number of players to deal to
char Hand(Players, 52); \each player's hand
Line 7,579 ⟶ 8,261:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 7,588 ⟶ 8,270:
QD KS 7S QH 6S JD QC 6D KD AC 7D AS AD
</pre>
 
=={{header|Yabasic}}==
{{trans|Run BASIC}}
<syntaxhighlight lang="vb">suite$ = "CDHS" //Club, Diamond, Heart, Spade
card$ = "A23456789TJQK" //Cards Ace to King
 
dim n(55) //make ordered deck
for i = 1 to 52 // of 52 cards
n(i) = i
next i
 
for i = 1 to 52 * 3 //shuffle deck 3 times
i1 = int(ran(52)) + 1
i2 = int(ran(52)) + 1
h2 = n(i1)
n(i1) = n(i2)
n(i2) = h2
next i
 
for hand = 1 to 4 //4 hands
for deal = 1 to 13 //deal each 13 cards
card = card + 1 //next card in deck
s = mod(n(card), 4) + 1 //determine suite
c = mod(n(card), 13) + 1 //determine card
print mid$(card$,c,1),mid$(suite$,s,1)," "; //show the card
next deal
print
next hand
end</syntaxhighlight>
{{out}}
<pre>Same as Run BASIC entry.</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">const Diamonds=1, Spades=3, Clubs=0, Hearts=2, Ace=1; // informational
var suits=T(0x1F0D1,0x1F0C1,0x1F0B1,0x1F0A1); //unicode 🃑,🃁,🂱,🂡
 
Line 7,614 ⟶ 8,327:
fcn deal(cards=5){ deck.pop(0,cards); }
fcn toString{ deck.pump(String,"toString"); }
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">d:=Deck();
d.println(d.deck.len());
d.deal().println();
d.println();</langsyntaxhighlight>
{{out}}
<pre>
Line 7,628 ⟶ 8,341:
=={{header|ZX Spectrum Basic}}==
The deck is just a basic index from 1 to 52, with 1-13 representing the A-K of hearts, 14-26 the A-K of spades, etc. This makes it easier to sort a hand later, if so desired.
<langsyntaxhighlight lang="zxbasic">10 GOSUB 5000
20 CLS
30 PRINT "Options:"
Line 7,749 ⟶ 8,462:
7290 DATA BIN 00010000: REM 16
7300 DATA BIN 01111100: REM 124
7310 DATA BIN 00000000: REM 0</langsyntaxhighlight>
{{out}}
Using the HSDC mod from line 5020 to make it easier here. H and D would appear in colour.
1,964

edits