Playing cards: Difference between revisions

Added Easylang
(Added Easylang)
 
(17 intermediate revisions by 8 users not shown)
Line 362:
{{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}}==
Line 956 ⟶ 1,034:
 
=={{header|BASIC}}==
==={{header|QuickBASIC}}===
{{works with|QuickBASIC|QBX 7.1}}
 
Line 1,025 ⟶ 1,104:
END SUB</syntaxhighlight>
 
{{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}}==
Line 2,090 ⟶ 2,223:
(declare (ignore x y))
(zerop (random 2)))))</syntaxhighlight>
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
Line 2,508 ⟶ 2,640:
=={{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}}==
Line 3,507 ⟶ 3,695:
 
=={{header|jq}}==
In the following, dealing of cards is accomplished in the conventional manner,
Neither the C nor the Go implementation of jq currently has a built-in
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,
so this program assumes an invocation such as:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | $JQ -MRcnr -f playing-cards.jq
Line 3,611 ⟶ 3,802:
... leaving 32 cards
</pre>
 
 
=={{header|Julia}}==
Line 4,598 ⟶ 4,788:
 
use strict;
use warnings;
 
@Playing_Card_Deck::suits = qw
Line 4,613 ⟶ 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,622 ⟶ 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,636 ⟶ 4,827:
sub print_cards
# Prints out a description of every card in the deck, in order.
{print "$_->{pip} of $_->{suit}\n" foreach @{shift(@_)};}</syntaxhighlight>
Some examples of use:
<syntaxhighlight lang="perl">my $deck = new Playing_Card_Deck->new;
$deck->shuffle;
my %card = $deck->deal;
Line 7,425 ⟶ 7,616:
 
=={{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}}
 
Line 7,876 ⟶ 8,173:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var FACES = "23456789TJQKA"
Line 7,973 ⟶ 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}}==
2,046

edits