# Golf me a card dealer

Frank has been working IT his whole life. Each day he responds to tickets, organizes equipment, and reports to his boss. One day, near the closing time on friday, Frank is doing his final inventory and sorting come cables in the supply closet. Suddenly, the door closes and locks from the outside! Such audacity! Frank shakes the door to try and let anyone know that he's stuck in the closet. Blast, nobody comes to his aid. With no cellphone, and no internet connection, Frank settles in with his laptop. After countless games of solitaire, frank decides to make his own card game....

## Challenge:

In this case, you are frank (but not really). Your challenge is to create a dealer that can deal hands of cards, that could be dealt realistically. Your program needs to do 2 things: deal hands of cards according to an input, and take all the hands and reshuffle them. You must do this in the least ammount of bytes possible.

## Rules:

Possible cards include all classic numbers and faces (a,2,3,4,5,6,7,8,9,10,j,q,k) and all suits (c,s,h,d), and two jokers (0j).

If a card is dealt, it cannot be dealt again until the program is given the reshuffle command, or it runs out of cards.

valid inputs are 'd' to deal a hand of 6 cards, and 'r' to reshuffle. NOTE that you can only deal out 9 hands before you run out of cards. Your program can either:
Automatically reshuffle,
Or give a 'X' to indicate that the dealer is out of cards.

your program needs to be able to take multiple commands over its running span.

All possible hands need to be able to be dealt in every order. Though, your program does NOT have to be random.

## Examples:

We deal twice, the first hand yields the 2 of hearts, a 4 of spades, 9 of clubs, a joker, an ace of spades, and a 6 of diamonds. The second hand yields the 3 of hearts, the 10 of diamonds, the 10 of clubs, the king of clubs, the ace of diamonds, and the queen of spades. this is VALID because this is possible in real life.

d
2h4s9c0jas6d
d

We deal twice, the first hand yields a mass of 5 cards (indicated by the dashes) and a 6 of hearts. The second hand deals another mass of 5 cards (again, the dashes) and another 6 of hearts. This is INVALID because it cannot happen in real life.

d
-----6h
d
6h-----

we deal once, reset, and deal again. some of the cards from the first and the second deal match. this is VALID because cardstacks are renewed when shuffled.

d
123456
r

d
456789

## Scoring:

As with any golf, the lowest byte count wins! Good luck!

• how many jokers? – Rod Aug 18 '16 at 14:06
• @Rod 2, as standard. that way all 9 hands will have the same number of cards, as the deck will be 54 cards total. – user56309 Aug 18 '16 at 14:09
• Can programs take a list of commands [d,d,r,d,d...], and output each of the deals that way? – Nathan Merrill Aug 18 '16 at 14:18
• @NathanMerrill sure, so long as it CAN take multiple commands. – user56309 Aug 18 '16 at 14:19
• Can we distinguish between the jokers, e.g., 0j and 1j, so that every card is unique? – AdmBorkBork Aug 19 '16 at 12:42

# Pyth - 42 41 bytes

Will need to restructure this.

VcQ\rj<c9.S++M*+md"jqka"}2T"cshd"m"0j"2lN


Stops giving cards after 9 deals.

## Python 3, 184 192 179 bytes

d=lambda:[(v+s,'10'+s)[v=='1']for v in'123456789JQKA'for s in'SCHD']+['0J']*2
_=0;c=d()
while 1:
if input()<'r'*(len(c)>5):[print(c.pop())for i in'_'*6]
else:c=d();del c[_];_+=1


Edit: jokers Edit 2: switched from using random and shuffling the list to cycling through the list and deleting 1 index from it incrementally.

Try it

• The print in the list comprehension works only in Python 3, not in Python 2 - for me at last. Also you're missing the jokers. – Karl Napf Aug 19 '16 at 14:07

## 05AB1E, 56 bytes

Automatically reshuffles.

9L>"ajqk"Sì"cshd"â€J„0jD‚ì©UvXg>y'rQ~i®U}y'dQiX.r6ô¬,¦˜U


Explanation

Generate the list of cards:

9L>"ajqk"Sì                     # list of card values, excluding jokers
"cshd"â€J            # cartesian product with suits to generate all cards except jokers
©U    # store in register and X


The actual function:

v                             # for each in input
i  }                 # if
Xg>                          # we're out of cards
~                     # or
y'rQ                      # it's an explicit reshuffle
®U                  # get full list of cards from register and store in X
# then
y'dQi            # if it's a draw
X.r         # shuffle current list of cards
6ô¬,     # print a list of 6 cards
¦˜U  # save the rest of the cards in X


Try it online

## PHP, 342 bytes

Update of @hd-s version.

$c=explode(",","2,3,4,5,6,7,8,9,T,J,Q,K,A");$a=["h"=>$c,"d"=>$c,"s"=>$c,"c"=>$c,"0"=>["J","J"]];$b=$a;$f=0;foreach(explode(",",$argv[1])as$g){if($g=='d'&&$f<54){$f+=6;for($i=0;$i<6;$i++){$s=array_keys($a)[rand(0,4)];$l=rand(1,count($a[$s])-1);if(isset($a[$s][$l])){echo$a[$s][$l].$s;unset($a[$s][$l])}else{$i--}}echo PHP_EOL}else{$f=0;$a=$b}}


Please test before qualifying. PHP 5.3+

## Changes:

• All variables renamed to be 1 byte long
• The card array is just reset at the end
• All cards dealt check shortened
• Couple of spaces removed

Total bytes saved: 69

## PHP, 411 bytes

This will generate a random outcome for each play (each deal(d)) and then once it's used all the cards, it will reshuffle the deck, clearing the play space.

To run, input comma separated commands: php card_dealer_golf.php d,d,d,d,r

$c=explode(",","a,2,3,4,5,6,7,8,9,10,j,q,k");$a=["h"=>$c,"d"=>$c,"s"=>$c,"c"=>$c,"0"=>["J","J"]];$cd=0;foreach(explode(",",$argv[1]) as $cmd){if($cd>=54){$cmd="r";}if($cmd=='d'){$cd+=6;for($i=0;$i<6;$i++){$s=array_keys($a)[rand(0,4)];$l=rand(1,count($a[$s])-1);if(isset($a[$s][$l])){echo$a[$s][$l].$s;unset($a[$s][$l]);}else{$i--;}}echo PHP_EOL;}else{$cd=0;$a=["h"=>$c,"d"=>$c,"s"=>$c,"c"=>$c,"0"=>["J","J"]];}}


Output:

$php card_dealer_golf.php d,d,d,d,r 10djs5cjh2h3h 3cJ06d2d7d3s 9s6s7h4d6c4s 10c4c3d7c8h6h  Ungolfed version $cards = explode(",","a,2,3,4,5,6,7,8,9,10,j,q,k");
$a = [ "h" =>$cards,
"d" => $cards, "s" =>$cards,
"c" => $cards, "0" => ["J","J"] ];$cd=0;
foreach(explode(",",$argv[1]) as$cmd) {
if($cd>=54) {$cmd = "r";}
if($cmd=='d'){$cd+=6; //+6 cards dealt
for($i=0;$i<6;$i++){$suit = array_keys($a)[rand(0,4)]; //get random suit$l=rand(1,count($a[$suit])-1); //get random card from suit
if(isset($a[$suit][$l])){ //if the card is still in the deck echo$a[$suit][$l].$suit; //output it unset($a[$suit][$l]); //remove from deck
}else{
$i--; //card not in deck, decrease the cards dealt in this round to be regenered } } echo PHP_EOL; }else{ //reshuffle logic. reset deck and amount of cards dealth$cd=0;
$a = [ "h" =>$cards,
"d" => $cards, "s" =>$cards,
"c" => $cards, "0" => ["J","J"] ]; } }  ## Javascript (ES6), 189 bytes i=>{for(n=52,d=['0j','0j'];n;d[n+1]='123456789JQKA'[--n>>2]+(n<4?0:'')+'cdhs'[n&3]);i.forEach(c=>{c!='r'&&n-54||(d.sort((a,b)=>Math.random()-.5),n=0);c=='d'&&console.log(d.slice(n,n+=6))})}  Demo: let F = i=>{for(n=52,d=['0j','0j'];n;d[n+1]='123456789JQKA'[--n>>2]+(n<4?0:'')+'cdhs'[n&3]);i.forEach(c=>{c!='r'&&n-54||(d.sort((a,b)=>Math.random()-.5),n=0);c=='d'&&console.log(d.slice(n,n+=6))})} F([..."rddrdddddd"]); ## PowerShell v2+, 174 158 bytes $a=(2..10+[char[]]'ajqk'|%{$i=$_;[char[]]'cshd'|%{"$i$_"}})+'0j'+'1j';$x=,0 for(){switch(read-host){'d'{$a|?{$_-notin$x}|Random -c 6|%{$x+=$_;$_}}'r'{$x=,0}}}


Creates an array of the number and suit combinations, plus two jokers (note they're unique), stores that into $a. This forms our full deck. Sets array $x equal to [0]. This represents the cards that we've already dealt. Then enters an infinite for loop. Performs a read-host to get the d or r (other behavior is undefined).

For dealing, pipes $a a Where-Object to pull out the items that are -notin$x (i.e., the cards we haven't dealt yet). Those are piped to Get-Random with a -count of 6, which will randomly select 6 distinct elements. We store those newly-dealt cards into $x, and the card is placed $_ onto the pipeline, which will get flushed when the next read-host happens and output one-per-line thanks to the default behavior of Write-Output. If the user attempts to deal when $a =$x (i.e., all cards have been dealt), then the Get-Random will be selecting from nothing, and so nothing will be output.

For the reshuffling, we just set \$x back to ,0 as the original array.

Note you'll need to ctrl-c or otherwise force quit the program.

### Example execution

PS C:\Tools\Scripts\golfing> .\golf-card-dealer.ps1
d
8c
5h
as
ah
1j
ac
d
5d
2h
8s
6h
2c
qs
d
8h
6d
10c
10h
6s
4h
d
js
9h
4c
6c
qd
kd
d
3d
jc
8d
2s
3h
4d
d
kh
jd
3c
0j
jh
10d
d
9s
7s
ks
kc
qc
9d
d
5s
9c
7c
4s
10s
7d
d
7h
5c
3s
qh
2d
d
d
d
r
d
3c
qs
2s
kd
2h
qc
d
ks
2d
5s
qd
kc
js

PS C:\Tools\Scripts\golfing>

• so, If I'm understanding correctly, (d)ealing after the deck is out doesn't return any cards? That's valid, I'm just curious. – user56309 Aug 18 '16 at 14:45
• @tuskiomi Yep, that's right. – AdmBorkBork Aug 18 '16 at 14:52