Card shuffles: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
[[Category:Games]]
 
{{draft task|Games}}{{clarified-review}}
 
Line 39 ⟶ 38:
* [[Poker hand_analyser]]
* [[Go Fish]]
 
=={{header|APL}}==
 
If we generate a deck by
 
<syntaxhighlight lang=APL"apl">
deck ← ⊂[1](52⍴'A23456789TJQK'),[0.5](13⍴'S'),(13⍴'H'),(13⍴'D'),(13⍴'C')
</syntaxhighlight>
Line 50 ⟶ 48:
Then a generated deck looks like
 
<syntaxhighlight lang=APL"apl">
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 AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
</syntaxhighlight>
Line 56 ⟶ 54:
Sorting a deck merely requires generating 52 unique random nunmbers from 1 to 52.
 
<syntaxhighlight lang="text">
deck[52?52]
JD 8C TH 8D KH QH 6S AH 4D JS 5S AD 6H 3H 3D 5C 9C 7C 7S 4C JC 3S KD 9H 3C 4H 2D TD KS TS 7D JH 9D 8H 6D 7H 2H 4S QC AC KC 9S AS QS TC 2C 8S 5D 2S 6C 5H QD
</syntaxhighlight>
 
=={{header|C}}==
{{trans|Modula-2}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 251 ⟶ 248:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 17, 18, 19, 14, 15, 16, 10, 11, 12, 13, 9, 8, 7, 5, 6, 1, 2, 3, 4]</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 383 ⟶ 379:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 17, 18, 19, 15, 16, 11, 12, 13, 14, 10, 7, 8, 9, 5, 6, 2, 3, 4, 1]</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <time.h>
#include <algorithm>
Line 529 ⟶ 524:
14 4 17 3 12 5 19 6 20 2 16 11 8 15 7 13 10 18 9 1
</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang=D"d">import std.container.array;
import std.random;
import std.range;
Line 650 ⟶ 644:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[10, 17, 19, 13, 5, 12, 1, 2, 14, 4, 9, 16, 7, 3, 15, 20, 8, 11, 6, 18]</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import (
Line 767 ⟶ 760:
[15 16 10 4 7 2 6 18 14 13 17 1 12 3 11 8 19 20 5 9]
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class CardShuffles {
private static final Random rand = new Random()
 
Line 888 ⟶ 880:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[18, 17, 20, 5, 8, 11, 2, 4, 3, 7, 19, 10, 15, 14, 12, 13, 16, 9, 1, 6]</pre>
 
=={{header|J}}==
{{eff note|J|({~ ?~@#)}}
 
<syntaxhighlight lang=J"j">NB. overhand cut
overhand=: (\: [: +/\ %@%:@# > # ?@# 0:)@]^:[
 
Line 904 ⟶ 895:
Here are some examples of the underlying selection mechanism in action for a deck of 10 cards:
 
<syntaxhighlight lang=J"j"> ([: +/\ %@%:@# > # ?@# 0:) i.10
0 0 0 0 0 0 0 0 1 1
([: +/\ %@%:@# > # ?@# 0:) i.10
Line 919 ⟶ 910:
Task examples:
 
<syntaxhighlight lang=J"j"> 1 riffle i.20
0 1 2 3 4 5 6 7 8 13 14 9 15 16 17 10 18 11 12 19
10 riffle i.20
Line 927 ⟶ 918:
10 overhand i.20
15 11 2 4 5 12 16 10 17 19 9 8 6 13 3 18 7 1 0 14</syntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<syntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
Line 1,052 ⟶ 1,042:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[18, 12, 13, 14, 2, 3, 15, 5, 9, 19, 7, 11, 1, 6, 4, 20, 16, 17, 10, 8]</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">function riffleshuffle!(list::Vector, flips::Integer)
len = length(list)
# pre-allocate the left and right part for efficiency
Line 1,143 ⟶ 1,132:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
-> [3, 11, 18, 14, 2, 12, 13, 4, 10, 19, 8, 16, 20, 5, 1, 6, 9, 15, 17, 7]</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.51
 
import java.util.Random
Line 1,227 ⟶ 1,215:
[17, 9, 12, 15, 7, 13, 18, 8, 2, 20, 5, 10, 16, 6, 14, 4, 19, 3, 11, 1]
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">-- Return a table respresenting a standard deck of cards in order
function newDeck ()
local cards, suits = {}, {"C", "D", "H", "S"}
Line 1,303 ⟶ 1,290:
5D 6D 7D 8D 9D TD JD QD KD AD QC KC AC 2D 3D 4C 5C 6C 7C 8C 9C TC JC 2C 3C
</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE CardShuffles;
FROM FormatString IMPORT FormatString;
FROM RandomNumbers IMPORT Random;
Line 1,503 ⟶ 1,489:
ReadChar;
END CardShuffles.</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang=Nim"nim">import algorithm, deques, random, sequtils, strutils
 
proc riffle(deck: seq[int]; iterations: Positive): seq[int] =
Line 1,571 ⟶ 1,556:
Standard library shuffle with one iteration:
7 9 15 10 1 19 5 14 6 8 4 3 18 17 16 12 13 20 2 11</pre>
 
=={{header|PARI/GP}}==
Riffle shuffle:
<syntaxhighlight lang="parigp">riffle(v)=
{
my(n=#v,k,t,deck=vector(n),left,right);
Line 1,594 ⟶ 1,578:
 
Overhand shuffle:
<syntaxhighlight lang="parigp">overhand(v)=
{
my(u=[],t,n=2*#v\5);
Line 1,607 ⟶ 1,591:
 
Usage:
<syntaxhighlight lang="parigp">riffle([1..52])
overhand([1..52])</syntaxhighlight>
{{out}}
<pre>%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52]
%2 = [44, 45, 46, 47, 48, 49, 50, 51, 52, 43, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4]</pre>
 
=={{header|Perl}}==
Follows the Raku implementation for the overhand shuffle, but uses classic one-liner for riffle.
<syntaxhighlight lang="perl">sub overhand {
our @cards; local *cards = shift;
my(@splits,@shuffle);
Line 1,643 ⟶ 1,626:
<pre>9 11 5 2 4 14 1 3 8 6 15 13 16 12 19 20 7 18 10 17
1 10 19 9 18 8 17 7 16 6 15 5 14 4 13 3 12 2 11 20</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">riffle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 1,697 ⟶ 1,679:
KH TH AH QH 8D JC QC 8C JH 8H 9D KS TD AS KD 8S TC AD TS AC 9C KC 9H QD JD JS 9S QS
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(load "@lib/simul.l")
 
(de riffle (Lst)
Line 1,722 ⟶ 1,703:
shuffle (5 3 13 15 17 12 14 11 2 1 19 7 6 9 18 8 10 4 16)
</pre>
 
=={{header|Python}}==
{{trans|D}}
<syntaxhighlight lang="python">import random
 
def riffleShuffle(va, flips):
Line 1,827 ⟶ 1,807:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
[14, 12, 2, 17, 18, 21, 8, 4, 15, 9, 11, 10, 3, 1, 7, 19, 20, 6, 5, 16, 13]</pre>
 
=={{header|Racket}}==
 
Line 1,836 ⟶ 1,815:
Racket has a built in <code>shuffle</code> function. Frankly, I'd go with that in your own code!
 
<syntaxhighlight lang="racket">#lang typed/racket
;; ---------------------------------------------------------------------------------------------------
;; Types and shuffle builder
Line 1,996 ⟶ 1,975:
shuffle: (J♣ 2♠ 4♦ A♦ K♥ 6♦ 5♦ 8♣ 2♦ T♥ 4♠ 3♣ 7♦ 9♠ T♦ J...
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>
use v6;
Line 2,027 ⟶ 2,005:
say (^20).pick(*);
</syntaxhighlight>
 
=={{header|REXX}}==
A little extra effort was put into the '''create''' subroutine to build any sort of deck, even a multiple deck as in canasta and samba (with/without jokers). &nbsp; Adding options for short decks, pinochle, schmear, six-handed &nbsp; '''500''', &nbsp; and the like would be prohibitive and muddy up the code and be distracting.
 
Six-handed 500 has additional cards of: &nbsp; <big> ♣11 &nbsp; ♣12 &nbsp; &nbsp; &nbsp; &nbsp; ♠11 &nbsp; ♠12 &nbsp; &nbsp; &nbsp; ♦11 &nbsp; ♦12 &nbsp; ♦13 &nbsp; &nbsp; &nbsp; ♦11 &nbsp; ♦12 &nbsp; ♦13 </big>
<syntaxhighlight lang="rexx">/*REXX program simulates various types of shuffling a deck of cards (any kind of deck).*/
call create; call show 'new deck' /*build and display a new card deck. */
 
Line 2,101 ⟶ 2,078:
♣A ♣2 ♣3 ♠2 ♥6 ♣6 ♣7 ♣8 ♦8 ♥8 ♣J ♣Q ♣K ♦A ♦2 ♦3 ♦4 ♥t ♦6 ♦7 ♠A ♦9 ♦t ♦J ♦Q ♠t ♥A ♥2 ♣t ♥4 ♠5 ♣5 ♦K ♥Q ♥9 ♦5 ♥J ♥3 ♥K ♣9 ♣4 ♠3 ♠4 ♠K ♠6 ♥7 ♠8 ♠9 ♠7 ♠J ♠Q ♥5
</pre>
 
=={{header|Ruby}}==
 
Two methods to solve the requirements, and a third one as bonus.
 
<syntaxhighlight lang=Ruby"ruby">
def riffle deck
left, right = deck.partition{rand(10).odd?}
Line 2,137 ⟶ 2,113:
p bonus(deck)
</syntaxhighlight>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import scala.collection.mutable.ListBuffer
import scala.util.Random
 
Line 2,259 ⟶ 2,234:
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
List(1, 14, 12, 8, 15, 19, 4, 18, 11, 16, 13, 3, 2, 17, 10, 5, 9, 7, 6, 20)</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang=Tcl"tcl">
proc riffle deck {
set length [llength $deck]
Line 2,275 ⟶ 2,249:
puts [overhand [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]]
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Line 2,407 ⟶ 2,380:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[19, 20, 15, 16, 17, 18, 13, 14, 10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascript">import "random" for Random
 
var r = Random.new()
Line 2,489 ⟶ 2,461:
[9, 16, 2, 12, 8, 20, 13, 5, 4, 18, 17, 6, 11, 19, 1, 3, 14, 7, 10, 15]
</pre>
 
=={{header|zkl}}==
A much better shuffle is List's shuffle method.
<syntaxhighlight lang="zkl">fcn riffle(deck){
len,N:=deck.len(),len/2;
newDeck:=N.pump(List,'wrap(n){ return(Void.Write,deck[n],deck[N+n]) });
Line 2,502 ⟶ 2,473:
piles.pump(List,'wrap(n){ deck[n*N,N] }).reverse().flatten()
}</syntaxhighlight>
<syntaxhighlight lang="zkl">riffle( [1..19].walk()).println();
overHand([1..19].walk()).println();
[1..19].walk().shuffle().println();</syntaxhighlight>
10,327

edits