Deal cards for FreeCell: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|PureBasic}}: Changed parameters of RNG())
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(137 intermediate revisions by 64 users not shown)
Line 1:
{{task|Games}} [[Category:Cards]]
''Free Cell'' is the solitaire card game that Paul Alfille introduced to the PLATO system in 1978. Jim Horne, at Microsoft, changed the name to ''FreeCell'' and reimplemented the game for [[DOS]], then [[Windows]]. This version introduced 32000 numbered deals. (The [http://www.solitairelaboratory.com/fcfaq.html Freecell FAQ] tells this history.)
 
''Free Cell'' is the solitaire card game that Paul Alfille introduced to the PLATO system in 1978. Jim Horne, at Microsoft, changed the name to ''FreeCell'' and reimplemented the game for [[DOS]], then [[Windows]]. <br>
As the game became popular, Jim Horne disclosed [http://www.solitairelaboratory.com/mshuffle.txt the algorithm], and other implementations of FreeCell began to reproduce the Microsoft deals. These deals are numbered from 1 to 32000. Newer versions from Microsoft have 1 million deals, numbered from 1 to 1000000; some implementations allow numbers outside that range.
This version introduced 32000 numbered deals. (The [http://www.solitairelaboratory.com/fcfaq.html FreeCell FAQ] tells this history.)
 
As the game became popular, Jim Horne disclosed [http://www.solitairelaboratory.com/mshuffle.txt the algorithm], and other implementations of FreeCell began to reproduce the Microsoft deals. <br>
These deals are numbered from 1 to 32000.
Newer versions from Microsoft have 1 million deals, numbered from 1 to 1000000; some implementations allow numbers outside that range.
 
The algorithm uses this [[linear congruential generator]] from Microsoft C:
Line 11 ⟶ 15:
* Rosetta Code has another task, [[linear congruential generator]], with code for this RNG in several languages.
 
<br>
The algorithm follows:
 
Line 22 ⟶ 27:
# Deal all 52 cards, face up, across 8 columns. The first 8 cards go in 8 columns, the next 8 cards go on the first 8 cards, and so on.
 
::::{| class="wikitable"
!| Order to deal cards
!| Game #1
Line 50 ⟶ 55:
|}
 
Deals can also be checked against [http://freecellgamesolutions.com/ FreeCell solutions to 1000000 games]. (Summon a video solution, and it displays the initial deal.)
(Summon a video solution, and it displays the initial deal.)
 
Write a program to take a deal number and deal cards in the same order as this algorithm. The program may display the cards with ASCII, with Unicode, by drawing graphics, or any other way.
The program may display the cards with ASCII, with Unicode, by drawing graphics, or any other way.
 
Related tasks:
* [[Playing cards]]
* [[Card shuffles]]
* [[War Card_Game]]
* [[Poker hand_analyser]]
* [[Go Fish]]
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F randomGenerator(=seed, n)
[Int] r
-V max_int32 = 7FFF'FFFF
seed = seed [&] max_int32
 
L r.len < n
seed = (seed * 214013 + 2531011) [&] max_int32
r [+]= seed >> 16
 
R r
 
F deal(seed)
V nc = 52
V cards = Array((nc - 1 .< -1).step(-1))
V rnd = randomGenerator(seed, nc)
L(r) rnd
V j = (nc - 1) - r % (nc - L.index)
swap(&cards[L.index], &cards[j])
R cards
 
F show(cards)
V l = cards.map(c -> ‘A23456789TJQK’[Int(c / 4)]‘’‘CDHS’[c % 4])
L(i) (0 .< cards.len).step(8)
print((l[i .< i + 8]).join(‘ ’))
 
:start:
V seed = I :argv.len == 2 {Int(:argv[1])} E 11982
print(‘Hand #.’.format(seed))
V deck = deal(seed)
show(deck)</syntaxhighlight>
 
{{out}}
<pre>
Hand 11982
AH AS 4H AC 2D 6S TS JS
3D 3H QS QC 8S 7H AD KS
KD 6H 5S 4D 9H JH 9S 3C
JC 5D 5C 8C 9D TD KH 7C
6C 2C TH QH 6D TC 4S 7S
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
procedure FreeCell is
type State is mod 2**31;
type Deck is array (0..51) of String(1..2);
 
package Random is
procedure Init(Seed: State);
function Rand return State;
end Random;
package body Random is
S : State := State'First;
procedure Init(Seed: State) is begin S := Seed; end Init;
function Rand return State is begin
S := S * 214013 + 2531011; return S / 2**16;
end Rand;
end Random;
 
procedure Deal (num : State) is
thedeck : Deck; pick : State;
Chars : constant String := "A23456789TJQKCDHS";
begin
for i in thedeck'Range loop
thedeck(i):= Chars(i/4+1) & Chars(i mod 4 + 14);
end loop;
Random.Init(num);
for i in 0..51 loop
pick := Random.Rand mod State(52-i);
Put(thedeck(Natural(pick))&' ');
if (i+1) mod 8 = 0 then New_Line; end if;
thedeck(Natural(pick)) := thedeck(51-i);
end loop; New_Line;
end Deal;
 
begin
Deal(1);
New_Line;
Deal(617);
end FreeCell;</syntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">FreeCell(num){
cards := "A23456789TJQK", suits := "♣♦♥♠", card := [], Counter := 0
loop, parse, cards
{
ThisCard := A_LoopField
loop, parse, suits
Card[Counter++] := ThisCard . A_LoopField
}
loop, 52
{
a := MS(num)
num:=a[1]
MyCardNo := mod(a[2],53-A_Index)
MyCard := Card[MyCardNo]
Card[MyCardNo] := Card[52-A_Index]
Card.Remove(52-A_Index)
Res .= MyCard (Mod(A_Index,8)?" ":"`n")
}
return Res
}
MS(Seed) {
Seed := Mod(214013 * Seed + 2531011, 2147483648)
return, [Seed, Seed // 65536]
}</syntaxhighlight>
MS() found at http://rosettacode.org/wiki/Linear_congruential_generator#AutoHotkey
Examples:<syntaxhighlight lang="autohotkey">Gui, font, s12, Courier
Gui, add, edit, w320 r17 -VScroll, % "Game# 1`n" FreeCell(1) "`n`nGame#617`n" FreeCell(617)
Gui, show
return
 
GuiClose:
GuiEscape:
ExitApp
return</syntaxhighlight>
Outputs:<pre>Game# 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Game#617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥ </pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> *FLOAT 64
hand% = 617
REM Initialise card library:
SYS "LoadLibrary", "CARDS.DLL" TO cards%
IF cards% = 0 ERROR 100, "No CARDS library"
SYS "GetProcAddress", cards%, "cdtInit" TO cdtInit%
SYS "GetProcAddress", cards%, "cdtDraw" TO cdtDraw%
SYS cdtInit%, ^dx%, ^dy%
VDU 23,22,8*dx%;5*dy%;8,16,16,128
REM Initialise deck:
DIM card&(51)
FOR I% = 0 TO 51 : card&(I%) = I% : NEXT
REM Shuffle deck:
dummy% = FNrng(hand%)
FOR I% = 51 TO 0 STEP -1
C% = FNrng(-1) MOD (I% + 1)
SWAP card&(C%), card&(I%)
NEXT
REM Display deck:
FOR I% = 0 TO 51
C% = card&(51 - I%)
X% = (I% MOD 8) * dx%
Y% = (I% DIV 8) * dy% * 2 / 3
SYS cdtDraw%, @memhdc%, X%, Y%, C%, 0, 0
NEXT
SYS "InvalidateRect", @hwnd%, 0, 0
*GSAVE freecell
END
DEF FNrng(seed)
PRIVATE state, M%
IF seed >= 0 THEN
state = seed
ELSE
state = (state * 214013 + 2531011)
FOR M% = 52 TO 31 STEP -1
IF state >= 2^M% state -= 2^M%
NEXT
ENDIF
= state >> 16</syntaxhighlight>
{{out}}
[[File:freecell_BBC.gif]]
 
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">vutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDC
>4$0" :rebmun emaG">:#,_$&>55+,>"O?+"**2+*"C4'' "**v
>8%!492*+*48*\-,1-:11p0g\0p11g#^_@A23456789TJQKCDHS*
^+3:g11,g2+"/"%4,g2+g14/4:-\"v"g0:%g11+*-/2-10-1*<>+
>8#8*#4*#::#%*#*/#*:#*0#:\#*`#:8#::#*:#8*#8:#2*#+^#<</syntaxhighlight>
{{out}}
<pre>Game number: 1
 
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H</pre>
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">( ( createArray
= array rank ranks suit suits
. A 2 3 4 5 6 7 8 9 T J Q K:?ranks
& :?array
& whl
' ( !ranks:%?rank ?ranks
& ♣ ♦ ♥ ♠:?suits
& whl
' ( !suits:%?suit ?suits
& !array str$(!rank !suit):?array
)
)
& !array
)
& ( deal
= A B D L Z pick card dealt deck
, i last rand row state
. !arg:(?deck:? [?L.?state)
& 8:?row
& :?dealt
& ( pick
= sep
. ( -1+!row:>0:?row
& " ":?sep
| \n:?sep&8:?row
)
& !dealt !arg !sep:?dealt
)
& 2^31:?B
& 2^16:?D
& "
'Hard code' the numbers B and D into the rand function using
macro expansion. (Gives a marginally faster execution speed.)
"
&
' (
. mod$(!state*214013+2531011.$B):?state
& div$(!state.$D)
)
: (=?rand)
& !L+1:?L
& whl
' ( mod$(rand$.!L+-1:?L):?i
& !deck:?A [!i %?card ?Z
& ( !Z:?Z %@?last&!A !last !Z
| !A
)
: ?deck
& pick$!card
)
& pick$\n
& str$!dealt
)
& createArray$:?deck
& put$("Game #1\n","dealt.txt",NEW)
& put$(deal$(!deck.1),"dealt.txt",APP)
& put$("
 
Game #617
","dealt.txt",APP)
& put$(deal$(!deck.617),"dealt.txt",APP)
&
)</syntaxhighlight>
Content of <code>dealt.txt</code>:
<pre>Game #1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Game #617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥ </pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
 
wchar_t s_suits[] = L"♣♦♥♠", s_nums[] = L"A23456789TJQK";
 
#define RMAX32 ((1U << 31) - 1)
#define RMAX ((1U << 15) - 1)
static int seed = 1;
int rnd(void) { return (seed = (seed * 214013 + 2531011) & RMAX32) >> 16; }
void srnd(int x) { seed = x; }
 
void show(const int *c)
{
int i;
Line 76 ⟶ 396:
}
}
 
void deal(int s, int *t)
{
Line 88 ⟶ 408:
}
}
 
int main(int c, char **v)
{
int s, card[52];
if (c < 2 || (s = atoi(v[1])) <= 0) s = 11982;
 
setlocale(LC_ALL, "");
 
Line 101 ⟶ 421:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Longer than it absolutely needs to be because I split out several independently useful classes.
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Text;
 
namespace FreeCellDeals
{
public class RNG
{
private int _state;
 
public RNG()
{
_state = (int)DateTime.Now.Ticks;
}
 
public RNG(int n)
{
_state = n;
}
public int Next()
{
return ((_state = 214013 * _state + 2531011) & int.MaxValue) >> 16;
}
}
 
public enum Rank
{
Ace,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
}
 
public enum Suit
{
Clubs,
Diamonds,
Hearts,
Spades
}
 
public class Card
{
private const string Ranks = "A23456789TJQK";
private const string Suits = "CDHS";
 
private Rank _rank;
public Rank Rank
{
get
{
return _rank;
}
set
{
if ((int)value < 0 || (int)value > 12)
{
throw new InvalidOperationException("Setting card rank out of range");
}
_rank = value;
}
}
 
private Suit _suit;
public Suit Suit
{
get
{
return _suit;
}
set
{
if ((int)value < 0 || (int)value > 3)
{
throw new InvalidOperationException("Setting card rank out of range");
}
_suit = value;
}
}
 
public Card(Rank rank, Suit suit)
{
Rank = rank;
Suit = suit;
}
 
public int NRank()
{
return (int) Rank;
}
 
public int NSuit()
{
return (int) Suit;
}
 
public override string ToString()
{
return new string(new[] {Ranks[NRank()], Suits[NSuit()]});
}
}
 
public class FreeCellDeal
{
public List<Card> Deck { get; private set; }
 
public FreeCellDeal(int iDeal)
{
RNG rng = new RNG(iDeal);
 
List<Card> rDeck = new List<Card>();
Deck = new List<Card>();
 
for (int rank = 0; rank < 13; rank++)
{
for (int suit = 0; suit < 4; suit++)
{
rDeck.Add(new Card((Rank)rank, (Suit)suit));
}
}
 
// Normally we deal from the front of a deck. The algorithm "deals" from the back so we reverse the
// deck here to more conventionally deal from the front/start of the array.
for (int iCard = 51; iCard >= 0; iCard--)
{
int iSwap = rng.Next() % (iCard + 1);
Deck.Add(rDeck[iSwap]);
rDeck[iSwap] = rDeck[iCard];
}
}
 
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int iRow = 0; iRow < 6; iRow++ )
{
for (int iCol = 0; iCol < 8; iCol++)
{
sb.AppendFormat("{0} ", Deck[iRow * 8 + iCol]);
}
sb.Append("\n");
}
for (int iCard = 48; iCard < 52; iCard++)
{
sb.AppendFormat("{0} ", Deck[iCard]);
}
return sb.ToString();
}
}
 
class Program
{
static void Main()
{
Console.WriteLine(new FreeCellDeal(1));
Console.WriteLine();
Console.WriteLine(new FreeCellDeal(617));
}
}
}</syntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
===Shorter version===
Shorter than the previous version. Adds a few classes, but stays closer to the gist of the C version.
<syntaxhighlight lang="csharp">
using System;
using System.Text;
 
namespace FreeCellConsole
{
public class Rand {
long _seed;
public Rand(int seed=1) {
_seed = seed;
}
public int Next() {
return (int) ((_seed = (_seed * 214013 + 2531011) & int.MaxValue) >> 16);
}
}
public class Card {
private static readonly string kSuits = "♣♦♥♠";
private static readonly string kValues = "A23456789TJQK";
public int Value { get; set; }
public int Suit { get; set; }
public Card(int rawvalue=0) : this(rawvalue / 4, rawvalue % 4) {
}
public Card(int value, int suit) {
Value = value; Suit = suit;
}
public override string ToString() {
return string.Format("{0}{1}", kValues[Value], kSuits[Suit]);
}
}
public class Deck {
public Card[] Cards;
public Deck(int seed) {
var r = new Rand(seed);
Cards = new Card[52];
for (int i=0; i < 52; i++)
Cards[i] = new Card(51 - i);
for (int i=0; i < 51; i++) {
int j = 51 - r.Next() % (52 - i);
Card tmp = Cards[i]; Cards[i] = Cards[j]; Cards[j] = tmp;
}
}
public override string ToString() {
var sb = new StringBuilder();
for (int i=0; i < Cards.Length; i++) {
sb.Append(Cards[i].ToString());
sb.Append(i % 8 == 7 ? "\n" : " ");
}
return sb.ToString();
}
}
class Program {
public static void Main(string[] args) {
Console.WriteLine("Deck 1\n{0}\n", new Deck(1));
Console.WriteLine("Deck 617\n{0}\n", new Deck(617));
}
}
}
</syntaxhighlight>
{{out}}
<pre>Deck 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Deck 617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
 
//--------------------------------------------------------------------------------------------------
using namespace std;
 
//--------------------------------------------------------------------------------------------------
class fc_dealer
{
public:
void deal( int game )
{
_gn = game;
fillDeck();
shuffle();
display();
}
 
private:
void fillDeck()
{
int p = 0;
for( int c = 0; c < 13; c++ )
for( int s = 0; s < 4; s++ )
_cards[p++] = c | s << 4;
}
 
void shuffle()
{
srand( _gn );
int cc = 52, nc, lc;
while( cc )
{
nc = rand() % cc;
lc = _cards[--cc];
_cards[cc] = _cards[nc];
_cards[nc] = lc;
}
}
 
void display()
{
char* suit = "CDHS";
char* symb = "A23456789TJQK";
int z = 0;
cout << "GAME #" << _gn << endl << "=======================" << endl;
for( int c = 51; c >= 0; c-- )
{
cout << symb[_cards[c] & 15] << suit[_cards[c] >> 4] << " ";
if( ++z >= 8 )
{
cout << endl;
z = 0;
}
}
}
 
int _cards[52], _gn;
};
//--------------------------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
fc_dealer dealer;
int gn;
while( true )
{
cout << endl << "Game number please ( 0 to QUIT ): "; cin >> gn;
if( !gn ) break;
 
system( "cls" );
dealer.deal( gn );
cout << endl << endl;
}
return 0;
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
Output:
<pre>
GAME #1
=======================
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
GAME #617
=======================
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
===OOP version===
 
This is written using a more object-oriented approach than the version above.
 
<syntaxhighlight lang="cpp">#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
#include <vector> // std::vector
 
using namespace std;
 
//------------------------------------------------------------------------------
 
class Random {
public:
void init(uint32_t seed) { _seed = seed; }
int roll() { return (_seed = (_seed * MULT + INCR) & MASK) >> 16; }
private:
int _seed;
enum { MULT = 214013, INCR = 2531011, MASK = (1U << 31) - 1 };
};
 
//------------------------------------------------------------------------------
 
class Card {
public:
Card(int value) : _value(value) { }
int suit() const { return _value % 4; }
int rank() const { return _value / 4; }
string str() const {
stringstream s; s << _ranks[rank()] << _suits[suit()]; return s.str();
}
private:
int _value;
const char* _suits = "CDHS";
const char* _ranks = "A23456789TJQK";
};
 
//------------------------------------------------------------------------------
 
class Deck {
public:
Deck(int seed) {
_random.init(seed);
for (int i = 0; i < 52; i++)
_cards.push_back(Card(51 - i));
for (int i = 0; i < 51; i++) {
int j = 51 - _random.roll() % (52 - i);
swap(_cards[i], _cards[j]);
}
}
string str() const {
stringstream s;
for (int i = 0; i < _cards.size(); i++)
s << _cards[i].str() << (i % 8 == 7 || i == 51 ? "\n" : " ");
return s.str();
}
private:
vector<Card> _cards;
Random _random;
};
 
//------------------------------------------------------------------------------
 
int main(int argc, const char * argv[])
{
{
Deck deck(1);
cout << "Deck 1" << endl << deck.str() << endl;
}
{
Deck deck(617);
cout << "Deck 617" << endl << deck.str() << endl;
}
return 0;
}
</syntaxhighlight>
{{out}}
<pre>Deck 1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Deck 617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void freeCellDeal() {
 
//a function that returns a random number generating function
function createRNG(variable Integer state) =>
() => (state = (214_013 * state + 2_531_011) % 2^31) / 2^16;
void deal(Integer num) {
// create an array with a list comprehension
variable value deck = Array {
for(rank in "A23456789TJQK")
for(suit in "CDHS")
"``rank````suit``"
};
value rng = createRNG(num);
for(i in 1..52) {
value index = rng() % deck.size;
assert(exists lastIndex = deck.lastIndex);
//swap the random card with the last one
deck.swap(index, lastIndex);
//print the last one
process.write("``deck.last else "missing card"`` " );
if(i % 8 == 0) {
print("");
}
//and shrink the array to remove the last card
deck = deck[...lastIndex - 1];
}
}
deal(1);
print("\n");
deal(617);
}</syntaxhighlight>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(def deck (into [] (for [rank "A23456789TJQK" suit "CDHS"] (str rank suit))))
 
(defn lcg [seed]
(map #(bit-shift-right % 16)
(rest (iterate #(mod (+ (* % 214013) 2531011) (bit-shift-left 1 31)) seed))))
 
(defn gen [seed]
(map (fn [rnd rng] (into [] [(mod rnd rng) (dec rng)]))
(lcg seed) (range 52 0 -1)))
 
(defn xchg [v [src dst]] (assoc v dst (v src) src (v dst)))
 
(defn show [seed] (map #(println %) (partition 8 8 ""
(reverse (reduce xchg deck (gen seed))))))
 
(show 1)</syntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun make-rng (seed)
#'(lambda ()
(ash (setf seed (mod (+ (* 214013 seed) 2531011) (expt 2 31))) -16)))
Line 112 ⟶ 958:
(let ((hand (make-array 52 :fill-pointer 0))
(rng (make-rng seed)))
(dolist (sd (split "A23456789TJQK"))
(dolist (ds (split "♣♦♥♠"))
(vector-push (concatenate 'string d s) hand)))
(dotimes (i 52)
Line 128 ⟶ 974:
 
(show-deck 1)
(show-deck 617)</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.algorithm, std.range;
 
struct RandomGenerator {
uint seed = 1;
 
@property uint next() pure nothrow @safe @nogc {
seed = (seed * 214_013 + 2_531_011) & int.max;
return seed >> 16;
Line 145 ⟶ 992:
int[52] cards;
 
void deal(in uint seed) /*pure nothrow*/ @safe @nogc {
enum int nc = cards.length; // Must be signed for iota.
nc.iota.retro.copy(cards[]);
 
// DMD 2.055 iota isn't pure nothrow.
copy(iota(nc - 1, -1, -1), cards[]);
 
auto rnd = RandomGenerator(seed);
foreach (immutable i, ref c; cards)
c.swap(c, cards[(nc - 1) - rnd.next % (nc - i)]);
}
 
void show() const @safe {
foreach writefln(row; std.range.chunks"%(%-(cards[], 8%s%)\n%) {",
foreach (c; row) cards[]
write .chunks(" ", "A23456789TJQK"[c / 4], "CDHS"[c % 4]8);
.map!(row => row.map!(c => only("A23456789TJQK"[c / 4],
writeln();
"CDHS"[c % 4]))));
}
}
}
 
void main(in string[] args) @safe {
immutable seed = (args.length == 2) ? to!uint(args[1]).to!uint : 11_982;
writeln("Hand ", seed);
Deck cards;
cards.deal(seed);
cards.show();
}</langsyntaxhighlight>
<pre>Hand 11982
AH AS 4H AC 2D 6S TS JS
Line 180 ⟶ 1,025:
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Deal_cards_for_FreeCell;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
type
TRandom = record
Seed: Int64;
function Next: Integer;
end;
 
TCard = record
const
kSuits = '♣♦♥♠';
kValues = 'A23456789TJQK';
var
Value: Integer;
Suit: Integer;
procedure Create(rawvalue: Integer); overload;
procedure Create(value, suit: Integer); overload;
procedure Assign(other: TCard);
function ToString: string;
end;
 
TDeck = record
Cards: TArray<TCard>;
procedure Create(Seed: Integer);
function ToString: string;
end;
 
{ TRandom }
 
function TRandom.Next: Integer;
begin
Seed := ((Seed * 214013 + 2531011) and Integer.MaxValue);
Result := Seed shr 16;
end;
 
{ TCard }
 
procedure TCard.Create(rawvalue: Integer);
begin
Create(rawvalue div 4, rawvalue mod 4);
end;
 
procedure TCard.Assign(other: TCard);
begin
Create(other.Value, other.Suit);
end;
 
procedure TCard.Create(value, suit: Integer);
begin
self.Value := value;
self.Suit := suit;
end;
 
function TCard.ToString: string;
begin
result := format('%s%s', [kValues[value + 1], kSuits[suit + 1]]);
end;
 
{ TDeck }
 
procedure TDeck.Create(Seed: Integer);
var
r: TRandom;
i, j: integer;
tmp: Tcard;
begin
r.Seed := Seed;
SetLength(Cards, 52);
for i := 0 to 51 do
Cards[i].Create(51 - i);
for i := 0 to 50 do
begin
j := 51 - (r.Next mod (52 - i));
tmp.Assign(Cards[i]);
Cards[i].Assign(Cards[j]);
Cards[j].Assign(tmp);
end;
end;
 
function TDeck.ToString: string;
var
i: Integer;
begin
Result := '';
for i := 0 to length(Cards) - 1 do
begin
Result := Result + Cards[i].ToString;
if i mod 8 = 7 then
Result := Result + #10
else
Result := Result + ' ';
end;
end;
 
var
Deck: TDeck;
 
begin
Deck.Create(1);
Writeln('Deck 1'#10, Deck.ToString, #10);
Deck.Create(617);
Writeln('Deck 617'#10, Deck.ToString);
readln;
end.</syntaxhighlight>
{{out}}
<pre>Deck 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Deck 617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥</pre>
=={{header|EasyLang}}==
{{trans|Phix}}
<syntaxhighlight>
global seed .
func xrnd .
seed = (seed * 214013 + 2531011) mod 0x80000000
return seed div 0x10000
.
len cards[] 52
proc deal game_num . .
print "hand " & game_num
seed = game_num
for i = 1 to 52
cards[i] = 52 - i
.
for i = 1 to 51
j = 52 - xrnd mod (53 - i)
swap cards[i] cards[j]
.
.
suits$[] = strchars "CDHS"
ranks$[] = strchars "A23456789TJQK"
#
proc show . .
for idx = 1 to 52
rank = cards[idx] div 4 + 1
suit = cards[idx] mod 4 + 1
write ranks$[rank] & suits$[suit] & " "
if idx mod1 13 = 13
print ""
.
.
print ""
.
deal 1 ; show
deal 617 ; show
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<syntaxhighlight lang="elixir">defmodule FreeCell do
import Bitwise
@suits ~w( C D H S )
@pips ~w( A 2 3 4 5 6 7 8 9 T J Q K )
@orig_deck for pip <- @pips, suit <- @suits, do: pip <> suit
def deal(games) do
games = if length(games) == 0, do: [Enum.random(1..32000)], else: games
Enum.each(games, fn seed ->
IO.puts "Game ##{seed}"
Enum.reduce(52..2, {seed,@orig_deck}, fn len,{state,deck} ->
state = ((214013 * state) + 2531011) &&& 0x7fff_ffff
index = rem(state >>> 16, len)
last = len - 1
{a, b} = {Enum.at(deck, index), Enum.at(deck, last)}
{state, deck |> List.replace_at(index, b) |> List.replace_at(last, a)}
end)
|> elem(1)
|> Enum.reverse
|> Enum.chunk(8,8,[])
|> Enum.each(fn row -> Enum.join(row, " ") |> IO.puts end)
IO.puts ""
end)
end
end
 
System.argv |> Enum.map(&String.to_integer/1)
|> FreeCell.deal</syntaxhighlight>
 
{{out}}
<pre>
C:\Elixir>elixir freecell.exs 1 617
Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM FREECELL
 
!$DOUBLE
 
DIM CARDS%[52]
 
PROCEDURE XRANDOM(SEED->XRND)
POW31=2^31
POW16=2^16
SEED=SEED*214013+2531011
SEED=SEED-POW31*INT(SEED/POW31)
XRND=INT(SEED/POW16)
END PROCEDURE
 
PROCEDURE DEAL(CARDS%[],GAME_NUM)
LOCAL I%,J%,S%
SEED=GAME_NUM
FOR I%=1 TO 52 DO
CARDS%[I%]=52-I%
END FOR
FOR I%=1 TO 51 DO
XRANDOM(SEED->XRND)
J%=52-XRND MOD (53-I%)
S%=CARDS%[I%]
CARDS%[I%]=CARDS%[J%]
CARDS%[J%]=S%
END FOR
END PROCEDURE
 
PROCEDURE SHOW(CARDS%[])
LOCAL INDEX%
FOR INDEX%=1 TO 52 DO
PRINT(MID$(SUITS$,CARDS%[INDEX%] MOD 4+1,1);MID$(NUMS$,CARDS%[INDEX%] DIV 4+1,1);" ";)
IF INDEX% MOD 8=0 OR INDEX%=52 THEN
PRINT
END IF
END FOR
END PROCEDURE
 
BEGIN
PRINT(CHR$(12);)
SUITS$="♣♦♥♠"
NUMS$="A23456789TJQK"
GAME_NUM=1982 ! if missing command line
IF CMDLINE$<>"" THEN GAME_NUM=VAL(CMDLINE$) END IF
SEED=1
DEAL(CARDS%[],GAME_NUM)
PRINT("Hand ";GAME_NUM)
SHOW(CARDS%[])
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Hand 1
♦J ♦2 ♥9 ♣J ♦5 ♥7 ♣7 ♥5
♦K ♣K ♠9 ♠5 ♦A ♣Q ♥K ♥3
♠2 ♠K ♦9 ♦Q ♠J ♠A ♥A ♣3
♣4 ♣5 ♠T ♥Q ♥4 ♣A ♦4 ♠7
♠3 ♦T ♠4 ♥T ♥8 ♣2 ♥J ♦7
♦6 ♠8 ♦8 ♠Q ♣6 ♦3 ♣8 ♣T
♠6 ♣9 ♥2 ♥6
</pre>
<pre>
Hand 617
♦7 ♦A ♣5 ♠3 ♠5 ♣8 ♦2 ♥A
♦T ♠7 ♦Q ♣A ♦6 ♥8 ♠A ♥K
♥T ♣Q ♥3 ♦9 ♠6 ♦8 ♦3 ♣T
♦K ♥5 ♠9 ♣3 ♠8 ♥7 ♦4 ♠J
♣4 ♠Q ♣9 ♥9 ♣7 ♥6 ♣2 ♠2
♠4 ♠T ♥2 ♦5 ♣J ♣6 ♥J ♥Q
♦J ♠K ♣K ♥4
</pre>
 
=={{header|F_sharp|F#}}==
The deal for this one is a little arduous, as we're using a list and maintain an immutable state. Of course, an array could be used, but what's the fun in that?
<syntaxhighlight lang="fsharp">
let msKindaRand seed =
let state = ref seed
(fun (_:unit) ->
state := (214013 * !state + 2531011) &&& System.Int32.MaxValue
!state / (1<<<16))
 
let unshuffledDeck = [0..51] |> List.map(fun n->sprintf "%c%c" "A23456789TJQK".[n / 4] "CDHS".[n % 4])
 
let deal boot idx =
let (last,rest) = boot |> List.rev |> fun xs->(List.head xs),(xs |> List.tail |> List.rev)
if idx=((List.length boot) - 1) then last, rest
else
rest
|> List.mapi (fun i x -> i,x)
|> List.partition (fst >> ((>) idx))
|> fun (xs,ys) -> (List.map snd xs),(List.map snd ys)
|> fun (xs,ys) -> (List.head ys),(xs @ last::(List.tail ys))
 
let game gameNo =
let rnd = msKindaRand gameNo
[52..-1..1]
|> List.map (fun i->rnd() % i)
|> List.fold (fun (dealt, boot) idx->deal boot idx |> fun (x,xs) -> (x::dealt, xs)) ([],unshuffledDeck)
|> fst |> List.rev
|> List.chunkBySize 8
|> List.map (String.concat " ")
|> String.concat "\n"
|> printfn "Game #%d\n%s\n" gameNo
 
 
[1; 617] |> List.iter game
</syntaxhighlight>
 
{{out}}
<pre>
Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting grouping io kernel literals make math
math.functions namespaces qw sequences sequences.extras ;
IN: rosetta-code.freecell
 
CONSTANT: max-rand-ms $[ 1 15 shift 1 - ]
CONSTANT: suits qw{ C D H S }
CONSTANT: ranks qw{ A 2 3 4 5 6 7 8 9 T J Q K }
SYMBOL: seed
 
: (random) ( n1 n2 -- n3 ) seed get * + dup seed set ;
 
: rand-ms ( -- n )
max-rand-ms 2531011 214013 (random) -16 shift bitand ;
 
: init-deck ( -- seq )
ranks suits [ append ] cartesian-map concat V{ } like ;
 
: swap-cards ( seq -- seq' )
rand-ms over length [ mod ] [ 1 - ] bi pick exchange ;
 
: (deal) ( seq -- seq' )
[ [ swap-cards dup pop , ] until-empty ] { } make ;
 
: deal ( game# -- seq ) seed set init-deck (deal) ;
 
: .cards ( seq -- ) 8 group [ [ write bl ] each nl ] each nl ;
 
: .game ( game# -- ) dup "Game #%d\n" printf deal .cards ;
 
: freecell ( -- ) 1 617 [ .game ] bi@ ;
 
MAIN: freecell</syntaxhighlight>
{{out}}
<pre>
Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
Using the lcgs module from [[Linear congruential generator#Fortran]]:
<syntaxhighlight lang="fortran">module Freecell
use lcgs
implicit none
 
character(4) :: suit = "CDHS"
character(13) :: rank = "A23456789TJQK"
character(2) :: deck(0:51)
 
contains
 
subroutine Createdeck()
integer :: i, j, n
 
n = 0
do i = 1, 13
do j = 1, 4
deck(n) = rank(i:i) // suit(j:j)
n = n + 1
end do
end do
 
end subroutine
subroutine Freecelldeal(game)
integer, intent(in) :: game
integer(i64) :: rnum
integer :: i, n
character(2) :: tmp
 
call Createdeck()
rnum = msrand(game)
 
do i = 51, 1, -1
n = mod(rnum, i+1)
tmp = deck(n)
deck(n) = deck(i)
deck(i) = tmp
rnum = msrand()
end do
 
write(*, "(a, i0)") "Game #", game
write(*, "(8(a, tr1))") deck(51:0:-1)
write(*,*)
 
end subroutine
end module Freecell
 
program Freecell_test
use Freecell
implicit none
 
call Freecelldeal(1)
call Freecelldeal(617)
end program</syntaxhighlight>
{{out}}
<pre>Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 04-11-2016
' compile with: fbc -s console
 
' to seed ms_lcg(seed > -1)
' to get random number ms_lcg(-1) or ms_lcg() or just ms_lcg
Function ms_lcg(seed As Integer = -1) As UInteger
 
Static As UInteger ms_state
 
If seed <> -1 Then
ms_state = seed Mod 2 ^ 31
Else
ms_state = (214013 * ms_state + 2531011) Mod 2 ^ 31
End If
 
Return ms_state Shr 16
 
End Function
 
' ------=< MAIN >=------
 
Dim As UByte card(51)
Dim As String suit = "CDHS", value = "A23456789TJQK"
Dim As Long i, c, s, v, game = 1
Dim As ULong game_nr(1 To 2) = { 1, 617}
 
Do
 
ms_lcg(game_nr(game)) ' seed generator
Print "game #"; game_nr(game)
game = game + 1
 
For i = 0 To 51 ' set up the cards
card(i) = i
Next
 
For i = 51 To 0 Step -1 ' shuffle
c = ms_lcg Mod (i +1)
Swap card(i), card(c)
Next
 
c = 0
Do
For i = 0 To 7
s = card(51 - c) Mod 4
v = card(51 - c) \ 4
Print Chr(value[v]); Chr(suit[s]); " ";
c = c +1
If c > 51 Then Exit Do
Next
Print
Loop
Print : Print
 
Loop Until game > UBound(game_nr)
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Deal_cards_for_FreeCell}}
 
'''Solution'''
 
There is no need to remove from a deal to be added to another one, it can be performed on a single array. It is and iteration from 52 to 1 indicating the end of the first array, and therefore the start of a second one after each swap. The only inconvenience is that second array contains the information in reversed order, but when it is shown it is also read in reversed order.
 
The number 127,185 is the decimal number of the 🃑 Unicode character.
 
In the Unicode playing cards characters, there is the Knight, between the Jack and Queen suits, which is not used, so it is skipped in the order.
 
[[File:Fōrmulæ - Deal cards for FreeCell 01.png]]
 
'''Test case.''' The —only— impossible deal #11982 in the original version of FreeCell for windows:
 
[[File:Fōrmulæ - Deal cards for FreeCell 02.png]]
 
[[File:Fōrmulæ - Deal cards for FreeCell 03.png]]
 
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
"math/rand"
"os"
"strconv"
"time"
)
 
const sSuits = "CDHS"
const sNums = "A23456789TJQK"
const rMax32 = math.MaxInt32
 
var seed = 1
 
func rnd() int {
seed = (seed*214013 + 2531011) & rMax32
return seed >> 16
}
 
func deal(s int) []int {
seed = s
t := make([]int, 52)
for i := 0; i < 52; i++ {
t[i] = 51 - i
}
for i := 0; i < 51; i++ {
j := 51 - rnd()%(52-i)
t[i], t[j] = t[j], t[i]
}
return t
}
 
func show(cs []int) {
for i, c := range cs {
fmt.Printf(" %c%c", sNums[c/4], sSuits[c%4])
if (i+1)%8 == 0 || i+1 == len(cs) {
fmt.Println()
}
}
}
 
func main() {
var game int
switch len(os.Args) {
case 1:
rand.Seed(time.Now().UnixNano())
game = 1 + rand.Intn(32000)
case 2:
var err error
game, err = strconv.Atoi(os.Args[1])
if err == nil && game >= 1 && game <= 32000 {
break
}
fallthrough
default:
fmt.Println("usage: deal [game]")
fmt.Println(" where game is a number in the range 1 to 32000")
return
}
fmt.Printf("\nGame #%d\n", game)
show(deal(game))
}</syntaxhighlight>
{{out}}
<pre>
Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
class FreeCell{
int seed
List<String> createDeck(){
List<String> suits = ['♣','♦','♥','♠']
List<String> values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
return [suits,values].combinations{suit,value -> "$suit$value"}
}
int random() {
seed = (214013 * seed + 2531011) & Integer.MAX_VALUE
return seed >> 16
}
List<String> shuffledDeck(List<String> cards) {
List<String> deck = cards.clone()
(deck.size() - 1..1).each{index ->
int r = random() % (index + 1)
deck.swap(r, index)
}
return deck
}
List<String> dealGame(int seed = 1){
this.seed= seed
List<String> cards = shuffledDeck(createDeck())
(1..cards.size()).each{ number->
print "${cards.pop()}\t"
if(number % 8 == 0) println('')
}
println('\n')
}
}
 
def freecell = new FreeCell()
freecell.dealGame()
freecell.dealGame(617)
</syntaxhighlight>
 
{{out}}
<pre>
Game #1
♦J ♦2 ♥9 ♣J ♦5 ♥7 ♣7 ♥5
♦K ♣K ♠9 ♠5 ♦A ♣Q ♥K ♥3
♠2 ♠K ♦9 ♦Q ♠J ♠A ♥A ♣3
♣4 ♣5 ♠10 ♥Q ♥4 ♣A ♦4 ♠7
♠3 ♦10 ♠4 ♥10 ♥8 ♣2 ♥J ♦7
♦6 ♠8 ♦8 ♠Q ♣6 ♦3 ♣8 ♣10
♠6 ♣9 ♥2 ♥6
 
Game #617
♦7 ♦A ♣5 ♠3 ♠5 ♣8 ♦2 ♥A
♦10 ♠7 ♦Q ♣A ♦6 ♥8 ♠A ♥K
♥10 ♣Q ♥3 ♦9 ♠6 ♦8 ♦3 ♣10
♦K ♥5 ♠9 ♣3 ♠8 ♥7 ♦4 ♠J
♣4 ♠Q ♣9 ♥9 ♣7 ♥6 ♣2 ♠2
♠4 ♠10 ♥2 ♦5 ♣J ♣6 ♥J ♥Q
♦J ♠K ♣K ♥4
</pre>
 
=={{header|Haskell}}==
{{trans|C}}
<syntaxhighlight lang="haskell">import Data.Int
import Data.Bits
import Data.List
import Data.Array.ST
import Control.Monad
import Control.Monad.ST
import System.Environment
 
srnd :: Int32 -> [Int]
srnd = map (fromIntegral . flip shiftR 16) .
tail . iterate (\x -> (x * 214013 + 2531011) .&. maxBound)
 
deal :: Int32 -> [String]
deal s = runST (do
ar <- newListArray (0,51) $ sequence ["A23456789TJQK", "CDHS"]
:: ST s (STArray s Int String)
forM (zip [52,51..1] rnd) $ \(n, r) -> do
let j = r `mod` n
vj <- readArray ar j
vn <- readArray ar (n - 1)
writeArray ar j vn
return vj)
where rnd = srnd s
 
showCards :: [String] -> IO ()
showCards = mapM_ (putStrLn . unwords) .
takeWhile (not . null) .
unfoldr (Just . splitAt 8)
 
main :: IO ()
main = do
args <- getArgs
let s = read (head args) :: Int32
putStrLn $ "Deal " ++ show s ++ ":"
let cards = deal s
showCards cards</syntaxhighlight>
 
Execution:
 
<pre>$ runghc freecell.hs 617
Deal 617:
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(A) # freecelldealer
freecelldealer(\A[1] | &null) # seed from command line
end
Line 224 ⟶ 1,861:
procedure rand_freecell() #: lcrng
return ishift(srand_freecell((214013 * srand_freecell() + 2531011) % 2147483648),-16)
end</langsyntaxhighlight>
{{out|Sample output for game 1}}
 
Sample output for game 1:<pre>Hand:
 
1 2 3 4 5 6 7 8
Line 238 ⟶ 1,875:
 
=={{header|J}}==
Paraphrase of [[#C|C]]:
 
<syntaxhighlight lang="j">deck=: ,/ 'A23456789TJQK' ,"0/ 7 u: '♣♦♥♠'
Paraphrase of C:
 
<lang j>deck=: ,/ 'A23456789TJQK' ,"0/ 7 u: '♣♦♥♠'
 
srnd=: 3 :'SEED=:{.y,11982'
Line 252 ⟶ 1,887:
deal=: |.@(swaps pairs) bind deck
 
show=: (,"2)@:(_8 ]\ ' '&,.)</langsyntaxhighlight>
{{out|Example use}}
<syntaxhighlight lang="j"> show deal srnd 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
show deal srnd 617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥ </syntaxhighlight>
 
=={{header|Java}}==
Example use:
<syntaxhighlight lang="java">
import java.util.Arrays;
 
public class Shuffler {
private int seed;
private String[] deck = {
"AC", "AD", "AH", "AS",
"2C", "2D", "2H", "2S",
"3C", "3D", "3H", "3S",
"4C", "4D", "4H", "4S",
"5C", "5D", "5H", "5S",
"6C", "6D", "6H", "6S",
"7C", "7D", "7H", "7S",
"8C", "8D", "8H", "8S",
"9C", "9D", "9H", "9S",
"TC", "TD", "TH", "TS",
"JC", "JD", "JH", "JS",
"QC", "QD", "QH", "QS",
"KC", "KD", "KH", "KS",
};
private int random() {
seed = (214013 * seed + 2531011) & Integer.MAX_VALUE;
return seed >> 16;
}
//shuffled cards go to the end
private String[] getShuffledDeck() {
String[] deck = Arrays.copyOf(this.deck, this.deck.length);
for(int i = deck.length - 1; i > 0; i--) {
int r = random() % (i + 1);
String card = deck[r];
deck[r] = deck[i];
deck[i] = card;
}
return deck;
}
//deal from end first
public void dealGame(int seed) {
this.seed = seed;
String[] shuffledDeck = getShuffledDeck();
for(int count = 1, i = shuffledDeck.length - 1; i >= 0; count++, i--) {
System.out.print(shuffledDeck[i]);
if(count % 8 == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
public static void main(String[] args) {
Shuffler s = new Shuffler();
s.dealGame(1);
System.out.println();
s.dealGame(617);
}
}
</syntaxhighlight>
{{out}}
<pre>
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|JavaScript}}==
 
<syntaxhighlight lang="javascript">"use strict";
/*
* Microsoft C Run-time-Library-compatible Random Number Generator
* Copyright by Shlomi Fish, 2011.
* Released under the MIT/X11 License
* ( http://en.wikipedia.org/wiki/MIT_License ).
* */
/* This uses Joose 2.x-or-above, an object system for JavaScript - http://code.google.com/p/joose-js/ . */
 
Class('MSRand', {
has: {
seed: { is: rw, },
},
methods: {
rand: function() {
this.setSeed((this.getSeed() * 214013 + 2531011) & 0x7FFFFFFF);
return ((this.getSeed() >> 16) & 0x7fff);
},
max_rand: function(mymax) {
return this.rand() % mymax;
},
shuffle: function(deck) {
if (deck.length) {
var i = deck.length;
while (--i) {
var j = this.max_rand(i+1);
var tmp = deck[i];
deck[i] = deck[j];
deck[j] = tmp;
}
}
return deck;
},
},
});
 
/*
* Microsoft Windows Freecell / Freecell Pro boards generation.
*
* See:
*
* - http://rosettacode.org/wiki/Deal_cards_for_FreeCell
*
* - http://www.solitairelaboratory.com/mshuffle.txt
*
* Under MIT/X11 Licence.
*
* */
 
function deal_ms_fc_board(seed) {
var randomizer = new MSRand({ seed: seed });
var num_cols = 8;
 
var _perl_range = function(start, end) {
var ret = [];
 
for (var i = start; i <= end; i++) {
ret.push(i);
}
 
return ret;
};
 
var columns = _perl_range(0, num_cols-1).map(function () { return []; });
var deck = _perl_range(0, 4*13-1);
 
randomizer.shuffle(deck);
 
deck = deck.reverse()
 
for (var i = 0; i < 52; i++) {
columns[i % num_cols].push(deck[i]);
}
 
var render_card = function (card) {
var suit = (card % 4);
var rank = Math.floor(card / 4);
 
return "A23456789TJQK".charAt(rank) + "CDHS".charAt(suit);
}
 
var render_column = function(col) {
return ": " + col.map(render_card).join(" ") + "\n";
}
 
return columns.map(render_column).join("");
}
</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Lua}}
<syntaxhighlight lang="julia">const rank = split("A23456789TJQK", "")
const suit = split("♣♦♥♠", "")
const deck = Vector{String}()
 
const mslcg = [0]
rng() = (mslcg[1] = ((mslcg[1] * 214013 + 2531011) & 0x7fffffff)) >> 16
 
initdeck() = for r in rank, s in suit push!(deck, "$r$s") end
 
function deal(num = rand(UInt,1)[1] % 32000 + 1)
initdeck()
mslcg[1] = num
println("\nGame # ", num)
while length(deck) > 0
choice = rng() % length(deck) + 1
deck[choice], deck[end] = deck[end], deck[choice]
print(" ", pop!(deck), length(deck) % 8 == 4 ? "\n" : "")
end
end
 
deal(1)
<lang j> show deal srnd 1
deal(617)
deal()
</syntaxhighlight> {{output}} <pre>
Game # 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
Line 264 ⟶ 2,115:
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
Game # 617
show deal srnd 617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
Line 271 ⟶ 2,122:
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥ </lang>
Game # 20065
7♥ 8♠ T♣ 3♦ T♥ 2♥ 6♥ K♥
K♣ A♣ 4♣ 5♥ 9♠ K♠ K♦ J♦
8♦ 6♦ J♣ 6♠ T♠ 7♦ 7♠ Q♥
7♣ J♠ A♠ 4♦ 5♣ 2♠ A♥ T♦
A♦ 6♣ Q♣ 8♥ 3♣ 5♦ 3♠ 3♥
9♦ 2♦ Q♦ 4♠ 5♠ 9♥ 8♣ 4♥
9♣ 2♣ J♥ Q♠
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.3
 
class Lcg(val a: Long, val c: Long, val m: Long, val d: Long, val s: Long) {
private var state = s
fun nextInt(): Long {
state = (a * state + c) % m
return state / d
}
}
 
const val CARDS = "A23456789TJQK"
const val SUITS = "♣♦♥♠"
 
fun deal(): Array<String?> {
val cards = arrayOfNulls<String>(52)
for (i in 0 until 52) {
val card = CARDS[i / 4]
val suit = SUITS[i % 4]
cards[i] = "$card$suit"
}
return cards
}
 
fun game(n: Int) {
require(n > 0)
println("Game #$n:")
val msc = Lcg(214013, 2531011, 1 shl 31, 1 shl 16, n.toLong())
val cards = deal()
for (m in 52 downTo 1) {
val index = (msc.nextInt() % m).toInt()
val temp = cards[index]
cards[index] = cards[m - 1]
print("$temp ")
if ((53 - m) % 8 == 0) println()
}
println("\n")
}
 
fun main(args: Array<String>) {
game(1)
game(617)
}</syntaxhighlight>
 
{{out}}
<pre>
Game #1:
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Game #617:
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">; Linear congruential random number generator
make "_lcg_state 0
 
to seed_lcg :seed
make "_lcg_state :seed
end
 
to sample_lcg
make "_lcg_state modulo sum product 214013 :_lcg_state 2531011 2147483648
output int quotient :_lcg_state 65536
end
 
; FreeCell
to card_from_number :number
output word item sum 1 int quotient :number 4 "A23456789TJQK item sum 1 modulo :number 4 "CDHS
end
 
to generate_deal :number
(local "deck "size "index "deal)
seed_lcg :number
make "deck []
repeat 52 [
make "deck lput difference # 1 :deck
]
make "deck listtoarray :deck
make "deal []
repeat 52 [
make "size difference 53 #
make "index sum 1 modulo sample_lcg :size
make "deal lput item :index :deck :deal
setitem :index :deck item :size :deck
]
output :deal
end
 
to print_deal :number
(local "deal "i "j "index)
make "deal generate_deal :number
repeat 7 [
make "i difference # 1
repeat (ifelse [equal? :i 6] 4 8) [
make "j difference # 1
make "index (sum 1 product :i 8 :j)
type (word (card_from_number item :index :deal) "| |)
]
print "||
]
end
 
print [Game #1]
print_deal 1
print "||
print [Game #617]
print_deal 617
print "||
bye</syntaxhighlight>
{{Out}}
<pre>Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Lua}}==
Uses bit32 library added in Lua 5.2.
<syntaxhighlight lang="lua">deck = {}
rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}
suit = {"C", "D", "H", "S"}
two31, state = bit32.lshift(1, 31), 0
 
function rng()
state = (214013 * state + 2531011) % two31
return bit32.rshift(state, 16)
end
 
function initdeck()
for i, r in ipairs(rank) do
for j, s in ipairs(suit) do
table.insert(deck, r .. s)
end
end
end
 
function deal(num)
initdeck()
state = num
print("Game #" .. num)
repeat
choice = rng(num) % #deck + 1
deck[choice], deck[#deck] = deck[#deck], deck[choice]
io.write(" " .. deck[#deck])
if (#deck % 8 == 5) then
print()
end
deck[#deck] = nil
until #deck == 0
print()
end
 
deal(1)
deal(617)</syntaxhighlight>
{{out}}
<pre>Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">next[last_] := Mod[214013 last + 2531011, 2^31];
deal[n_] :=
Module[{last = n, idx,
deck = StringJoin /@
Tuples[{{"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J",
"Q", "K"}, {"C", "D", "H", "S"}}], res = {}},
While[deck != {}, last = next[last];
idx = Mod[BitShiftRight[last, 16], Length[deck]] + 1;
deck = ReplacePart[deck, {idx -> deck[[-1]], -1 -> deck[[idx]]}];
AppendTo[res, deck[[-1]]]; deck = deck[[;; -2]]]; res];
format[deal_] := Grid[Partition[deal, 8, 8, {1, 4}, Null]];
Print[format[deal[1]]];
Print[format[deal[617]]];</syntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import sequtils, strutils, os
proc randomGenerator(seed: int): iterator: int =
var state = seed
return iterator: int =
while true:
state = (state * 214013 + 2531011) and int32.high
yield state shr 16
proc deal(seed: int): seq[int] =
const nc = 52
result = toSeq countdown(nc - 1, 0)
var rnd = randomGenerator seed
for i in 0 ..< nc:
let r = rnd()
let j = (nc - 1) - r mod (nc - i)
swap result[i], result[j]
proc show(cards: seq[int]) =
var l = newSeq[string]()
for c in cards:
l.add "A23456789TJQK"[c div 4] & "CDHS"[c mod 4]
for i in countup(0, cards.high, 8):
echo " ", l[i..min(i+7, l.high)].join(" ")
let seed = if paramCount() == 1: paramStr(1).parseInt else: 11982
echo "Hand ", seed
let deck = deal seed
show deck</syntaxhighlight>
Output:
<pre>Hand 11982
AH AS 4H AC 2D 6S TS JS
3D 3H QS QC 8S 7H AD KS
KD 6H 5S 4D 9H JH 9S 3C
JC 5D 5C 8C 9D TD KH 7C
6C 2C TH QH 6D TC 4S 7S
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S</pre>
 
=={{header|Objeck}}==
{{trans|C#}}
<syntaxhighlight lang="objeck">class FreeCell {
function : Main(args : String[]) ~ Nil {
Deal(1)->PrintLine();
Deal(617)->PrintLine();
}
function : Deal(seed : Int) ~ String {
deck := Deck->New(seed)->ToString();
return "Game #{$seed}:\n{$deck}\n";
}
}
 
class Deck {
@cards : Card[];
New(seed : Int) {
r := Random->New(seed);
@cards := Card->New[52];
for(i := 0; i < 52; i+= 1;) {
@cards[i] := Card->New(51 - i);
};
for(i := 0; i < 51; i += 1;) {
j := 51 - r->Next() % (52 - i);
tmp := @cards[i]; @cards[i] := @cards[j]; @cards[j] := tmp;
};
}
method : public : ToString() ~ String {
buffer := "";
each(i : @cards) {
buffer += @cards[i]->ToString();
buffer += (i % 8 = 7 ? "\n" : " ");
};
return buffer;
}
}
 
class Random {
@seed : Int;
New(seed : Int) {
@seed := seed;
}
method : public : Next() ~ Int {
@seed := (@seed * 214013 + 2531011) and Int->MaxSize();
return @seed >> 16;
}
}
 
class Card {
@value : Int;
@suit : Int;
New(value : Int) {
@value := value / 4; @suit := value % 4;
}
method : public : ToString() ~ String {
suits := "♣♦♥♠"; values := "A23456789TJQK";
value := values->Get(@value); suit := suits->Get(@suit);
return "{$value}{$suit}";
}
}</syntaxhighlight>
 
Output:
<pre>
Game #1:
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Game #617:
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|Objective-C}}==
Based on the shorter C# version. Objective-C can use the C code as-is, but this example uses some NS foundation classes. The [http://weblog.bignerdranch.com/398-objective-c-literals-part-1/ latest clang compiler] is assumed with [http://en.wikipedia.org/wiki/Automatic_Reference_Counting ARC] enabled. For the sake of clarity & simplicity, method prototypes have been omitted from @interface sections: they are not necessary if everything is in one file.
<syntaxhighlight lang="objc">#define RMAX32 ((1U << 31) - 1)
 
//--------------------------------------------------------------------
 
@interface Rand : NSObject
-(instancetype) initWithSeed: (int)seed;
-(int) next;
@property (nonatomic) long seed;
@end
 
@implementation Rand
-(instancetype) initWithSeed: (int)seed {
if ((self = [super init])) {
self.seed = seed;
}
return self;
}
-(int) next {
return (int) ((_seed = (_seed * 214013 + 2531011) & RMAX32) >> 16);
}
@end
 
//--------------------------------------------------------------------
 
@interface Card : NSObject
-(instancetype) initWithSequence: (int)n;
-(instancetype) initWithValue: (int)v suit: (int)s;
@property (nonatomic) int value;
@property (nonatomic) int suit;
@end
 
@implementation Card
-(instancetype) initWithSequence: (int)n {
return [self initWithValue:n/4 suit:n%4];
}
-(instancetype) initWithValue: (int)v suit: (int)s {
if ((self = [super init])) {
_value = v; _suit = s;
}
return self;
}
-(NSString *) description {
static NSString * const kSuits = @"♣♦♥♠";
static NSString * const kValues = @"A23456789TJQK";
return [NSString stringWithFormat:@"%C%C",
[kValues characterAtIndex:_value],
[kSuits characterAtIndex:_suit]];
}
@end
 
//--------------------------------------------------------------------
 
@interface Deck : NSObject
-(instancetype) initWithSeed: (int)seed;
@property (nonatomic, strong) NSMutableArray *cards;
@end
 
@implementation Deck
-(instancetype) initWithSeed: (int)seed {
if ((self = [super init])) {
Rand *r = [[Rand alloc] initWithSeed:seed];
_cards = [NSMutableArray array];
for (int i = 0; i < 52; i++)
[_cards addObject:[[Card alloc] initWithSequence:51 - i]];
for (int i = 0; i < 51; i++)
[_cards exchangeObjectAtIndex:i withObjectAtIndex:51 - [r next] % (52 - i)];
}
return self;
}
-(NSString *) description {
NSMutableString *s = [NSMutableString string];
for (int i = 0; i < [_cards count]; i++) {
[s appendString:[_cards[i] description]];
[s appendString:i%8==7 ? @"\n" : @" "];
}
return s;
}
@end
 
//--------------------------------------------------------------------
 
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Deck 1\n%@\n", [[Deck alloc] initWithSeed:1]);
NSLog(@"Deck 617\n%@\n", [[Deck alloc] initWithSeed:617]);
}
return 0;
}</syntaxhighlight>
{{out}}
<pre>Deck 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Deck 617
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|OCaml}}==
{{trans|C}}
<syntaxhighlight lang="ocaml">let srnd x =
 
(* since OCaml's built-in int type is at least 31 (note: not 32) bits wide,
<lang ocaml>let srnd =
and this problem takes mod 2^31, it is just enough if we treat it as
let ( + ) = Int32.add
an unsigned integer, which means taking the logical right shift *)
and ( * ) = Int32.mul
andlet (seed &= )ref =x Int32.logandin
fun () ->
and ( >> ) = Int32.shift_right
seed := (!seed * 214013 + 2531011) land 0x7fffffff;
and rmax32 = Int32.max_int in
!seed lsr 16
function x ->
let seed = ref (Int32.of_int x) in
function () ->
seed := (!seed * 214013l + 2531011l) & rmax32;
Int32.to_int (!seed >> 16)
 
let deal s =
Line 319 ⟶ 2,650:
Printf.printf "Deal %d:\n" s;
let cards = deal s in
show cards</langsyntaxhighlight>
{{out|Execution}}
 
Execution:
 
<pre>$ ocaml freecell.ml 617
Deal 617:
Line 332 ⟶ 2,661:
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|PARI/GP}}==
The use of <code>local</code> is critical here, so that <code>nextrand()</code> has access to the current state unaffected by whatever the user may have stored in the variable <code>'state</code>.
<syntaxhighlight lang="parigp">card(n)=concat(["A","2","3","4","5","6","7","8","9","T","J","Q","K"][n\4+1],["C","D","H","S"][n%4+1]);
nextrand()={
(state=(214013*state+2531011)%2^31)>>16
};
deal(seed)={
my(deck=vector(52,n,n-1),t);
local(state=seed);
forstep(last=52,1,-1,
t=nextrand()%last+1;
print1(card(deck[t]),if(last%8==5,"\n"," "));
deck[t]=deck[last]
)
};</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
<lang perl>use utf8;
 
use strict;
use warnings;
 
use utf8;
 
sub deal {
my ($s, @a, @d) = shift;
sub rnd { ($s = ($s * 214013 + 2531011) & 0x7fffffff) >> 16 }
 
my $rnd = sub {
print "Hand $s\n";
return (($s = ($s * 214013 + 2531011) & 0x7fffffff) >> 16 );
for my $b (split "", "A23456789TJQK") {
};
push @d, map("$_$b", qw/♣ ♦ ♥ ♠/);
}
 
my @d;
for (reverse 0 .. 51) {
for my $b (split "", "A23456789TJQK") {
my $r = rnd() % ($_ + 1);
push @d, map("$_$b", qw/♣ ♦ ♥ ♠/);
($d[$r], $d[$_]) = ($d[$_], $d[$r]);
}
}
 
reverse @d
for my $idx (reverse 0 .. $#d) {
my $r = $rnd->() % ($idx + 1);
@d[$r, $idx] = @d[$idx, $r];
}
 
return [reverse @d];
}
 
my $hand_idx = shift(@ARGV) // 11_982;
# This may look confusing.
(my $s = "@{[deal(shift @ARGV // 11982)]}") =~ s/(.{1,24})(?: |$)/$1\n/g;
print $s;</lang>
 
my $cards = deal($hand_idx);
=={{header|Perl 6}}==
<lang perl6>my $game_number = @*ARGS.shift || 1;
 
my $num_cards_in_height = 8;
sub ms_lcg_method ($seed) { ( 214013 * $seed + 2531011 ) % 2**31 };
my $string = '';
 
while (@$cards)
# lazy list of the random sequence
{
my @ms_lcg := gather take $_ +> 16
$string .= join(' ', splice(@$cards, 0, 8)) . "\n";
for ( $game_number.&ms_lcg_method, -> $seed { $seed.&ms_lcg_method } ... * );
}
 
binmode STDOUT, ':encoding(utf-8)';
my @deck = <A 2 3 4 5 6 7 8 9 T J Q K> X~ <♣ ♦ ♥ ♠>;
print "Hand $hand_idx\n";
print $string;</syntaxhighlight>
 
=={{header|Phix}}==
{{trans|ERRE}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">seed</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">xrnd</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">seed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">seed</span><span style="color: #0000FF;">*</span><span style="color: #000000;">214013</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2531011</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#7FFFFFFF</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">seed</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cards</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">52</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">game_num</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">seed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">game_num</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">52</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">52</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">51</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">52</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xrnd</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">53</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">suits</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"CDHS"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">ranks</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"A23456789TJQK"</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">52</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rank</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">suit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">],</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">eol</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">12</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%c%c%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rank</span><span style="color: #0000FF;">],</span><span style="color: #000000;">suits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">suit</span><span style="color: #0000FF;">],</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eol</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">game_num</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000080;font-style:italic;">--integer game_num=617</span>
<span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">game_num</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"hand %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">game_num</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
hand 1
JD 2D 9H JC 5D 7H 7C 5H KD KC 9S 5S AD
QC KH 3H 2S KS 9D QD JS AS AH 3C 4C 5C
TS QH 4H AC 4D 7S 3S TD 4S TH 8H 2C JH
7D 6D 8S 8D QS 6C 3D 8C TC 6S 9C 2H 6H
 
hand 617
7D AD 5C 3S 5S 8C 2D AH TD 7S QD AC 6D
8H AS KH TH QC 3H 9D 6S 8D 3D TC KD 5H
9S 3C 8S 7H 4D JS 4C QS 9C 9H 7C 6H 2C
2S 4S TS 2H 5D JC 6C JH QH JD KS KC 4H
</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang="php">class FreeCell_Deal {
 
protected $deck = array(
'AC', 'AD', 'AH', 'AS', '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S',
'4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S', '6C', '6D', '6H', '6S',
'7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
'TC', 'TD', 'TH', 'TS', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS',
'KC', 'KD', 'KH', 'KS'
);
protected $game; // Freecell Game Number
protected $state; // Current state of the LCG
 
public $deal = array(); // Generated card sequence to deal
 
function __construct( $game ) {
 
$this->game = max( min( $game, 32000 ), 1 );
 
// seed RNG with game number
$this->state = $this->game;
 
while ( ! empty( $this->deck ) ) {
 
// choose random card
$i = $this->lcg_rnd() % count( $this->deck );
 
// move random card to game deal pile
$this->deal[] = $this->deck[ $i ];
 
// move last card to random card spot
$this->deck[ $i ] = end( $this->deck );
 
// remove last card from deck
array_pop( $this->deck );
 
}
 
}
 
protected function lcg_rnd() {
return ( $this->state = ( $this->state * 214013 + 2531011 ) % 2147483648 ) >> 16;
}
 
function print( $cols = 8 ) {
echo str_pad( " Game " . $this->game . " ", $cols * 3 - 1, '=', STR_PAD_BOTH ), PHP_EOL;
foreach ( array_chunk( $this->deal, $cols ) as $row ) {
echo implode( " ", $row ), PHP_EOL;
}
echo PHP_EOL;
}
 
my @game = gather while @deck {
my $index = @ms_lcg.shift % @deck;
take @deck[$index];
@deck[$index] = @deck.pop;
}
 
$tests = array( 1, 617, 11982 );
say "Game #$game_number";
 
say @game.splice(0, min(@game.elems, 8)) while @game;</lang>
foreach ( $tests as $game_num ) {
$deal = new FreeCell_Deal( $game_num );
$deal->print();
}
 
</syntaxhighlight>
{{Out}}
<pre>
======= Game 1 ========
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
====== Game 617 =======
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
 
===== Game 11982 ======
AH AS 4H AC 2D 6S TS JS
3D 3H QS QC 8S 7H AD KS
KD 6H 5S 4D 9H JH 9S 3C
JC 5D 5C 8C 9D TD KH 7C
6C 2C TH QH 6D TC 4S 7S
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S
</pre>
 
=={{header|PicoLisp}}==
Using the random generator from [[Linear congruential generator#PicoLisp]]:
<langsyntaxhighlight PicoLisplang="picolisp">(setq *MsSeed 11982)
 
(de msRand ()
Line 396 ⟶ 2,895:
(prin " ^[[" (cadr C) "m" (cddr C) "^[[m" (car C))
(at (0 . 8) (prinl)) )
(prinl) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#MaxCardNum = 51 ;zero-based count of cards in a deck
Global deckSize
Global Dim cards(#MaxCardNum) ;card with highest index is at the top of deck
Line 444 ⟶ 2,944:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out|Sample output:}}
<pre>Hand #1
JD 2D 9H JC 5D 7H 7C 5H
Line 473 ⟶ 2,973:
=={{header|Python}}==
{{trans|D}}
<syntaxhighlight lang ="python">from sys import argv
 
def randomGenerator(seed=1):
class RandomGenerator:
max_int32 = (1 << 31) - 1
def __init__(self, seed=1):
max_uint32seed = (1 << 32)seed -& 1max_int32
self.seed = seed & max_uint32
 
defwhile next(self)True:
max_int32seed = (1seed <<* 31214013 + 2531011) -& 1max_int32
self.seedyield = (self.seed * 214013 + 2531011) &>> max_int3216
return self.seed >> 16
 
def deal(seed):
nc = 52
cards = list(range(nc - 1, -1, -1))
rnd = RandomGeneratorrandomGenerator(seed)
for i, r in xrangezip(range(nc), rnd):
j = (nc - 1) - rnd.next()r % (nc - i)
cards[i], cards[j] = cards[j], cards[i]
return cards
 
def show(cards):
l = ["A23456789TJQK"[int(c / 4)] + "CDHS"[c % 4] for c in cards]
for i in xrangerange(0, len(cards), 8):
print " ", (" ".join(l[i : i+8]))
 
if __name__ == '__main__':
from sys import argv
seed = int(argv[1]) if len(argv) == 2 else 11982
print ("Hand {}", .format(seed))
deck = deal(seed)
show(deck)</langsyntaxhighlight>
{{out}}
Output:
<pre>Hand 11982
AH AS 4H AC 2D 6S TS JS
3D 3H QS QC 8S 7H AD KS
KD 6H 5S 4D 9H JH 9S 3C
JC 5D 5C 8C 9D TD KH 7C
6C 2C TH QH 6D TC 4S 7S
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S</pre>
 
=={{header|RubyQuackery}}==
<lang ruby># Deal cards for FreeCell.
# http://rosettacode.org/wiki/Deal_cards_for_FreeCell
 
<code>MCR-seed</code> and <code>MCR-rand</code> are defined at [[Linear congruential generator#Quackery]].
require 'optparse'
 
<syntaxhighlight lang="Quackery"> [ [ [] 52 times
# Parse command-line arguments.
# games = ARGV converted[ toi^ Integerjoin ] ]
constant ] is newpack ( --> n )
# No arguments? Pick any of first 32000 games.
games = nil
OptionParser.new do |o|
begin
o.banner = "Usage: #{o.program_name} number..."
o.parse!
games = ARGV.map {|s| Integer(s)}
games.empty? and games = [rand(32000)]
rescue => e
$stderr.puts e, o
abort
end
end
 
[ 2dup peek
# Define methods for old Ruby versions.
dip [ over -1 peek ]
# Enumerable#each_slice appeared in Ruby 1.8.7.
swap 2swap poke
# Enumerable#flat_map appeared in Ruby 1.9.2.
-1 poke ] is to-end ( [ n --> [ )
module Enumerable
unless method_defined? :each_slice
def each_slice(count)
block_given? or return enum_for(:each_slice, count)
ary = []
each {|e|
ary << e
ary.length == count and (yield ary.dup; ary.clear)}
ary.empty? or yield ary.dup
nil
end
end
 
[ [] swap
unless method_defined? :flat_map
def52 flat_maptimes
[ MCR-rand
block_given? or return enum_for(:flat_map)
ary = []over size mod
each {|e| to-end
y-1 = yield esplit
ary.concat(y)swap rescuedip ary.push(y)}join ]
drop ] is mixem ( [ --> [ )
ary
 
end
[ 4 /mod
end
$ "A23456789TJQK"
rot peek emit
$ "CDHS"
swap peek emit ] is echocard ( n --> )
 
[ witheach
[ echocard
i^ 8 mod 7 =
iff cr else sp ] ] is echopack ( [ --> )
 
[ MCR-seed replace
newpack
mixem
echopack ] is deal ( n --> )
 
' [ 1 617 11982 ]
witheach
[ say "Deal #"
dup echo cr
deal cr cr ]</syntaxhighlight>
 
{{out}}
 
<pre>Deal #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Deal #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
 
Deal #11982
AH AS 4H AC 2D 6S TS JS
3D 3H QS QC 8S 7H AD KS
KD 6H 5S 4D 9H JH 9S 3C
JC 5D 5C 8C 9D TD KH 7C
6C 2C TH QH 6D TC 4S 7S
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S
</pre>
 
=={{header|R}}==
<syntaxhighlight lang="r">## Linear congruential generator code not original -
## copied from
## http://www.rosettacode.org/wiki/Linear_congruential_generator#R
## altered to allow seed as an argument
 
library(gmp) # for big integers
 
rand_MS <- function(n = 1, seed = 1) {
a <- as.bigz(214013)
c <- as.bigz(2531011)
m <- as.bigz(2^31)
x <- rep(as.bigz(0), n)
x[1] <- (a * as.bigz(seed) + c) %% m
i <- 1
while (i < n) {
x[i+1] <- (a * x[i] + c) %% m
i <- i + 1
}
as.integer(x / 2^16)
}
 
## =============================
## New code follows:
## =============================
 
dealFreeCell <- function(seedNum) {
deck <- paste(rep(c("A",2,3,4,5,6,7,8,9,10,"J","Q","K"), each = 4), c("C","D","H","S"), sep = "")
cards = rand_MS(52,seedNum)
for (i in 52:1) {
cardToPick <- (cards[53-i]%% i)+1 # R indexes from 1, not 0
deck[c(cardToPick,i)] <- deck[c(i, cardToPick)]
}
 
deck <- rev(deck) # flip the deck to deal
deal = matrix(c(deck,NA,NA,NA,NA),ncol = 8, byrow = TRUE)
# using a matrix for simple printing, but requires filling with NA
# if implementing as a game, a list for each pile would make more sense
print(paste("Hand numer:",seedNum), quote = FALSE)
print(deal, quote = FALSE, na.print = "")
}
</syntaxhighlight>
{{out}}
<pre>
> dealFreeCell(1)
[1] Hand numer: 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] JD 2D 9H JC 5D 7H 7C 5H
[2,] KD KC 9S 5S AD QC KH 3H
[3,] 2S KS 9D QD JS AS AH 3C
[4,] 4C 5C 10S QH 4H AC 4D 7S
[5,] 3S 10D 4S 10H 8H 2C JH 7D
[6,] 6D 8S 8D QS 6C 3D 8C 10C
[7,] 6S 9C 2H 6H
> dealFreeCell(617)
[1] Hand numer: 617
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 7D AD 5C 3S 5S 8C 2D AH
[2,] 10D 7S QD AC 6D 8H AS KH
[3,] 10H QC 3H 9D 6S 8D 3D 10C
[4,] KD 5H 9S 3C 8S 7H 4D JS
[5,] 4C QS 9C 9H 7C 6H 2C 2S
[6,] 4S 10S 2H 5D JC 6C JH QH
[7,] JD KS KC 4H
 
</pre>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(module Linear_congruential_generator racket
;; taken from http://rosettacode.org/wiki/Linear_congruential_generator#Racket
;; w/o BSD generator
(require racket/generator)
(provide ms-rand)
(define (ms-update state_n)
(modulo (+ (* 214013 state_n) 2531011)
(expt 2 31)))
(define ((rand update ->rand) seed)
(generator () (let loop ([state_n seed])
(define state_n+1 (update state_n))
(yield (->rand state_n+1))
(loop state_n+1))))
(define ms-rand (rand ms-update (lambda (x) (quotient x (expt 2 16))))))
 
(require (submod "." Linear_congruential_generator))
 
;; Personally I prefer CDHS to the unicode characters (on an aesthetic basis,
;; rather than anything else. Plus it helps match with the examples given at the
;; head of the task.
(define suits "CDHS")
(define (initial-deck)
(for*/vector #:length 52
((face "A23456789TJQK")
(suit suits))
(cons face suit)))
 
;; srfi/43 has one of these, but is quick enough to reimplement!
(define (vector-swap! v i j)
(let ((t (vector-ref v i)))
(vector-set! v i (vector-ref v j))
(vector-set! v j t)))
 
(define (deal hand)
(define pack (initial-deck))
(define rnd (ms-rand hand))
(define (deal-nth-card pack-sz card-no deal)
(vector-swap! pack card-no (sub1 pack-sz))
(cons (vector-ref pack (sub1 pack-sz)) deal))
(let inner-deal ((pack-sz (vector-length pack)) (deal null))
(if (zero? pack-sz) (reverse deal) ;; we accumulated this backwards!
(inner-deal (sub1 pack-sz)
(deal-nth-card pack-sz (modulo (rnd) pack-sz) deal)))))
 
(define (present-deal hand)
(printf "Game #~a~%" hand)
(let inner-present-deal ((pile 0) (deck (deal hand)))
(unless (null? deck)
(printf "~a~a~a" (caar deck) (cdar deck)
(if (or (null? (cdr deck)) (= 7 (modulo pile 8))) "\n" " "))
(inner-present-deal (add1 pile) (cdr deck)))))
 
;; Run it so we get some output:
(present-deal 1)
(newline)
(present-deal 617)</syntaxhighlight>
 
{{out}}
<pre>Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #617
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2016.05}}
<syntaxhighlight lang="raku" line>sub dealgame ($game-number = 1) {
sub ms-lcg-method($seed = $game-number) { ( 214013 * $seed + 2531011 ) % 2**31 }
 
# lazy list of the random sequence
my @ms-lcg = |(&ms-lcg-method ... *).map: * +> 16;
 
constant CardBlock = '🂠'.ord;
my @deck = gather for flat(1..11,13,14) X+ (48,32...0) -> $off {
take chr CardBlock + $off;
}
 
my @game = gather while @deck {
@deck[@ms-lcg.shift % @deck, @deck-1] .= reverse;
take @deck.pop;
}
 
say "Game #$game-number";
say @game.splice(0, 8 min +@game) while @game;
}
 
dealgame;
dealgame 617;</syntaxhighlight>
{{out}}
<big><big><pre>Game #1
🃋 🃂 🂹 🃛 🃅 🂷 🃗 🂵
🃎 🃞 🂩 🂥 🃁 🃝 🂾 🂳
🂢 🂮 🃉 🃍 🂫 🂡 🂱 🃓
🃔 🃕 🂪 🂽 🂴 🃑 🃄 🂧
🂣 🃊 🂤 🂺 🂸 🃒 🂻 🃇
🃆 🂨 🃈 🂭 🃖 🃃 🃘 🃚
🂦 🃙 🂲 🂶
Game #617
🃇 🃁 🃕 🂣 🂥 🃘 🃂 🂱
🃊 🂧 🃍 🃑 🃆 🂸 🂡 🂾
🂺 🃝 🂳 🃉 🂦 🃈 🃃 🃚
🃎 🂵 🂩 🃓 🂨 🂷 🃄 🂫
🃔 🂭 🃙 🂹 🃗 🂶 🃒 🂢
🂤 🂪 🂲 🃅 🃛 🃖 🂻 🂽
🃋 🂮 🃞 🂴</pre></big></big>
 
=={{header|REXX}}==
This REXX version supports EBCDIC and ASCII symbols (used for the cards).
 
It also supports any number for the number of columns &nbsp; (default is 8).
 
See the &nbsp; ''discussion'' &nbsp; page for support for &nbsp; '''game = ─1''' &nbsp; and &nbsp; '''game= ─2''' &nbsp; &nbsp; &nbsp; (minus one and minus two).
<syntaxhighlight lang="rexx">/*REXX program deals cards for a specific FreeCell solitaire card game (0 ──► 32767).*/
numeric digits 15 /*ensure enough digits for the random #*/
parse arg game cols . /*obtain optional arguments from the CL*/
if game=='' | game=="," then game=1 /*No game specified? Then use default.*/
if cols=='' | cols=="," then cols=8 /* " cols " " " " */
state=game /*seed random # generator with game num*/
if 8=='f8'x then suit= "cdhs" /*EBCDIC? Then use letters for suits.*/
else suit= "♣♦♥♠" /* ASCII? " " symbols " " */
rank= 'A23456789tJQK' /*t in the rank represents a ten (10).*/
pad=left('', 13) /*used for indentation for the tableau.*/
say center('tableau for FreeCell game' game, 50, "─") /*show title for FreeCell game #*/
say /* [↓] @ is an array of all 52 cards.*/
#=-1; do r=1 for length(rank) /*build the deck first by the rank. */
do s=1 for length(suit); #=#+1 /* " " " secondly " " suit. */
@.#=substr(rank, r,1)substr(suit, s,1) /*build the $ array one card at at time*/
end /*s*/ /* [↑] first card is number 0 (zero).*/
end /*r*/ /* [↑] build deck per FreeCell rules. */
$=pad /*@: cards to be dealt, eight at a time*/
do cards=51 by -1 for 52 /* [↓] deal the cards for the tableau.*/
?=rand() // (cards+1) /*get next rand#; card # is remainder.*/
$=$ @.?; @.?=@.cards /*swap two cards: use random and last.*/
if words($)==cols then do; say $; $=pad /*deal FreeCell cards for the tableau. */
end
end /*cards*/ /*normally, 8 cards are dealt to a row.*/
/* [↓] residual cards may exist. */
if $\='' then say $ /*Any residual cards in the tableau ? */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
rand: state=(214013*state + 2531011) // 2**31; return state % 2**16 /*FreeCell rand#*/</syntaxhighlight>
'''output''' &nbsp; when using the default game number: &nbsp; <tt> 1 </tt>
<pre>
───────────tableau for FreeCell game 1────────────
 
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ t♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ t♦ 4♠ t♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ t♣
6♠ 9♣ 2♥ 6♥
</pre>
'''output''' &nbsp; when using the game number: &nbsp; <tt> 617 </tt>
<pre>
──────────tableau for FreeCell game 617───────────
 
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
t♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
t♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ t♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ t♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby"># games = ARGV converted to Integer
# No arguments? Pick any of first 32000 games.
begin
games = ARGV.map {|s| Integer(s)}
rescue => err
$stderr.puts err.inspect
$stderr.puts "Usage: #{__FILE__} number..."
abort
end
games.empty? and games = [rand(32000)]
 
# Create original deck of 52 cards, not yet shuffled.
orig_deck = %w{A 2 3 4 5 6 7 8 9 T J Q K}.product(%w{C D H S}).map(&:join)
}.flat_map {|rank| %w{C D H S}.map {|suit| "#{rank}#{suit}"}}
 
games.each do |seed|
deck = orig_deck.dup
 
# Shuffle deck with random index from linear congruential
# generator like Microsoft.
Line 580 ⟶ 3,366:
deck[index], deck[last] = deck[last], deck[index]
end
 
deck.reverse! # Shuffle did reverse deck. Do reverse again.
 
# Deal cards.
puts "Game ##{seed}"
deck.each_slice(8) {|row| puts " " + row.join(" ")}
puts
end</lang>
end</syntaxhighlight>
{{out}}
<pre>
$ ruby freecell.rb 1 165
Game #1
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
 
Game #165
<pre>$ ruby freecell.rb 11982
2D 2C 2H 5D 3C 6S JC 7S
Game #11982
AH7H ASQD 4H6D ACKC 2D3D 6S9H TS8H JSQC
3DTS 3H5H QSJD QC7D 8S6H 7H7C ADKH KS4D
KDQS 6H8S 5SQH 4D9S 9H8D JH3H 9SJS 3CAH
JCAD 5DKS 5CTC 8CKD 9D TD4S KH2S 7C3S
6CTH 2C4C TH9C QH6C 6DTD TC4H 4S8C 7SAC
5C 5S JH AS
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">projectDir$ = "a_project" ' project directory
imageDir$ = DefaultDir$ + "\projects\" + projectDir$ + "\image\" ' directory of deck images
imagePath$ = "../";projectDir$;"/image/" ' path of deck images
 
suite$ = "C,D,H,S" ' Club,Diamond,Heart,Spades
card$ = "A,2,3,4,5,6,7,8,9,T,J,Q,K" ' 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(rnd(1)*52) + 1
i2 = int(rnd(1)*52) + 1
h2 = n(i1)
n(i1) = n(i2)
n(i2) = h2
next i
 
for yy = 1 to 8 ' display 7 across and 8 down
for xx = 1 to 7
card = card + 1
s = (n(card) mod 4) + 1 ' determine suite
c = (n(card) mod 13) + 1 ' determine card
cardId$ = word$(card$,c,",");word$(suite$,s,",");".gif"
html "<div style='position: relative; left:";(xx -1) * 80;"px; top:";(yy -1) * 20;"px; height:0px; width:0px;>"
html "<div style='width:100px; height:100px; border:solid 0px #000;'>"
html "<img src=";imagePath$;cardId$;" width=70px >"
html "</div></div>"
if card = 52 then end ' out of cards
next xx
next yy</syntaxhighlight>
[[File:freeCell.png]]
 
=={{header|Rust}}==
 
Based on JavaScript.
 
<syntaxhighlight lang="rust">// Code available at https://rosettacode.org/wiki/Linear_congruential_generator#Rust
extern crate linear_congruential_generator;
 
use linear_congruential_generator::{MsLcg, Rng, SeedableRng};
 
// We can't use `rand::Rng::shuffle` because it uses the more uniform `rand::Rng::gen_range`
// (`% range` is subject to modulo bias). If an exact match of the old dealer is not needed,
// `rand::Rng::shuffle` should be used.
fn shuffle<T>(rng: &mut MsLcg, deck: &mut [T]) {
let len = deck.len() as u32;
for i in (1..len).rev() {
let j = rng.next_u32() % (i + 1);
deck.swap(i as usize, j as usize);
}
}
 
fn gen_deck() -> Vec<String> {
const RANKS: [char; 13] = ['A','2','3','4','5','6','7','8','9','T','J','Q','K'];
const SUITS: [char; 4] = ['C', 'D', 'H', 'S'];
 
let render_card = |card: usize| {
let (suit, rank) = (card % 4, card / 4);
format!("{}{}", RANKS[rank], SUITS[suit])
};
 
(0..52).map(render_card).collect()
}
 
fn deal_ms_fc_board(seed: u32) -> Vec<String> {
let mut rng = MsLcg::from_seed(seed);
let mut deck = gen_deck();
 
shuffle(&mut rng, &mut deck);
deck.reverse();
 
deck.chunks(8).map(|row| row.join(" ")).collect::<Vec<_>>()
}
 
fn main() {
let seed = std::env::args()
.nth(1)
.and_then(|n| n.parse().ok())
.expect("A 32-bit seed is required");
 
for row in deal_ms_fc_board(seed) {
println!(": {}", row);
}
}
</syntaxhighlight>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">object Shuffler extends App {
 
private val suits = Array("C", "D", "H", "S")
private val values = Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K")
private val deck = values.flatMap(v => suits.map(s => s"$v$s"))
 
private var seed: Int = _
 
private def random() = {
seed = (214013 * seed + 2531011) & Integer.MAX_VALUE
seed >> 16
}
 
private def getShuffledDeck = {
val d = deck.map(c => c)
for(i <- deck.length - 1 until 0 by -1) {
val r = random() % (i + 1)
val card = d(r)
d(r) = d(i)
d(i) = card
}
d.reverse
}
 
def deal(seed: Int): Unit = {
this.seed = seed
getShuffledDeck.grouped(8).foreach(e => println(e.mkString(" ")))
}
 
deal(1)
println
deal(617)
}</syntaxhighlight>
 
{{out}}
<pre>
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
</pre>
<pre>
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
const string: suits is "♣♦♥♠";
const string: nums is "A23456789TJQK";
 
var integer: randomSeed is 1;
 
const func integer: random is func
result
var integer: rand is 1;
begin
randomSeed := (randomSeed * 214013 + 2531011) mod 2 ** 31;
rand := randomSeed >> 16;
end func;
 
const proc: show (in array integer: cards) is func
local
var integer: index is 0;
begin
for index range 1 to 52 do
write(" " <& suits[succ(cards[index] rem 4)] <& nums[succ(cards[index] div 4)]);
if index rem 8 = 0 or index = 52 then
writeln;
end if;
end for;
end func;
 
const func array integer: deal (in integer: gameNum) is func
result
var array integer: cards is 52 times 0;
local
var integer: i is 0;
var integer: j is 0;
var integer: s is 0;
begin
randomSeed := gameNum;
for i range 1 to 52 do
cards[i] := 52 - i;
end for;
for i range 1 to 51 do
j := 52 - random mod (53 - i);
s := cards[i];
cards[i] := cards[j];
cards[j] := s;
end for;
end func;
 
const proc: main is func
local
var integer: gameNum is 11982;
var array integer: cards is 0 times 0;
begin
OUT := STD_CONSOLE;
if length(argv(PROGRAM)) >= 1 then
block
gameNum := integer parse (argv(PROGRAM)[1]);
exception
catch RANGE_ERROR: noop;
end block;
end if;
cards := deal(gameNum);
writeln("Hand " <& gameNum);
show(cards);
end func;</syntaxhighlight>
 
{{out}}
<pre>
Hand 1
♦J ♦2 ♥9 ♣J ♦5 ♥7 ♣7 ♥5
♦K ♣K ♠9 ♠5 ♦A ♣Q ♥K ♥3
♠2 ♠K ♦9 ♦Q ♠J ♠A ♥A ♣3
♣4 ♣5 ♠T ♥Q ♥4 ♣A ♦4 ♠7
♠3 ♦T ♠4 ♥T ♥8 ♣2 ♥J ♦7
♦6 ♠8 ♦8 ♠Q ♣6 ♦3 ♣8 ♣T
♠6 ♣9 ♥2 ♥6
</pre>
<pre>
Hand 617
♦7 ♦A ♣5 ♠3 ♠5 ♣8 ♦2 ♥A
♦T ♠7 ♦Q ♣A ♦6 ♥8 ♠A ♥K
♥T ♣Q ♥3 ♦9 ♠6 ♦8 ♦3 ♣T
♦K ♥5 ♠9 ♣3 ♠8 ♥7 ♦4 ♠J
♣4 ♠Q ♣9 ♥9 ♣7 ♥6 ♣2 ♠2
♠4 ♠T ♥2 ♦5 ♣J ♣6 ♥J ♥Q
♦J ♠K ♣K ♥4
</pre>
 
=={{header|Swift}}==
Swift 4.2. Largely based on the Objective-C example.
<syntaxhighlight lang="swift">enum Suit : String, CustomStringConvertible, CaseIterable {
case clubs = "C", diamonds = "D", hearts = "H", spades = "S"
var description: String {
return self.rawValue
}
}
enum Rank : Int, CustomStringConvertible, CaseIterable {
case ace=1, two, three, four, five, six, seven
case eight, nine, ten, jack, queen, king
var description: String {
let d : [Rank:String] = [.ace:"A", .king:"K", .queen:"Q", .jack:"J", .ten:"T"]
return d[self] ?? String(self.rawValue)
}
}
struct Card : CustomStringConvertible {
let rank : Rank, suit : Suit
var description : String {
return String(describing:self.rank) + String(describing:self.suit)
}
init(rank:Rank, suit:Suit) {
self.rank = rank; self.suit = suit
}
init(sequence n:Int) {
self.init(rank:Rank.allCases[n/4], suit:Suit.allCases[n%4])
}
}
struct Deck : CustomStringConvertible {
var cards = [Card]()
init(seed:Int) {
for i in (0..<52).reversed() {
self.cards.append(Card(sequence:i))
}
struct MicrosoftLinearCongruentialGenerator {
var seed : Int
mutating func next() -> Int {
self.seed = (self.seed * 214013 + 2531011) % (Int(Int32.max)+1)
return self.seed >> 16
}
}
var r = MicrosoftLinearCongruentialGenerator(seed: seed)
for i in 0..<51 {
self.cards.swapAt(i, 51-r.next()%(52-i))
}
}
var description : String {
var s = ""
for (ix,c) in self.cards.enumerated() {
s.write(String(describing:c))
s.write(ix % 8 == 7 ? "\n" : " ")
}
return s
}
}
let d1 = Deck(seed: 1)
print(d1)
let d617 = Deck(seed: 617)
print(d617)
</syntaxhighlight>
 
{{out}}
<pre>
JD 2D 9H JC 5D 7H 7C 5H
KD KC 9S 5S AD QC KH 3H
2S KS 9D QD JS AS AH 3C
4C 5C TS QH 4H AC 4D 7S
3S TD 4S TH 8H 2C JH 7D
6D 8S 8D QS 6C 3D 8C TC
6S 9C 2H 6H
</pre>
<pre>
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|Tcl}}==
{{trans|C}}
<langsyntaxhighlight lang="tcl">proc rnd {{*r seed}} {
upvar 1 ${*r} r
expr {[set r [expr {($r * 214013 + 2531011) & 0x7fffffff}]] >> 16}
Line 630 ⟶ 3,743:
set cards [deal $s]
puts "Hand $s"
show $cards</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|zsh}}
<syntaxhighlight lang="bash">test $# -gt 0 || set -- $((RANDOM % 32000))
for seed; do
print Game $seed:
 
# Shuffle deck.
deck=({A,{2..9},T,J,Q,K}{C,D,H,S})
for i in {52..1}; do
((seed = (214013 * seed + 2531011) & 0x7fffffff))
((j = (seed >> 16) % i + 1))
t=$deck[$i]
deck[$i]=$deck[$j]
deck[$j]=$t
done
 
# Deal cards.
print -n ' '
for i in {52..1}; do
print -n ' '$deck[$i]
((i % 8 == 5)) && print -n $'\n '
done
print
done</syntaxhighlight>
{{out}}
<pre>$ zsh freecell.sh 80388
Game 80388:
QC 5H AS 7H 8S 4S 4H 3H
QD 3S 2C 2S 7D AH 6D 3D
QS TH QH 3C 2H JS 5D 5C
AD TD 6H JD 5S 7S 4D 7C
9S KC TC KH 8C 9D 8D JH
KS AC KD 9C 9H 6C JC 2D
4C 8H TS 6S</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="wren">class Lcg {
construct new(a, c, m, d, s) {
_a = a
_c = c
_m = m
_d = d
_s = s
}
 
nextInt() {
_s = (_a * _s + _c) % _m
return _s / _d
}
}
 
var CARDS = "A23456789TJQK"
var SUITS = "♣♦♥♠".toList
 
var deal = Fn.new {
var cards = List.filled(52, null)
for (i in 0...52) {
var card = CARDS[(i/4).floor]
var suit = SUITS[i%4]
cards[i] = card + suit
}
return cards
}
 
var game = Fn.new { |n|
if (n.type != Num || !n.isInteger || n <= 0) {
Fiber.abort("Game number must be a positive integer.")
}
System.print("Game #%(n):")
var msc = Lcg.new(214013, 2531011, 1<<31, 1<<16, n)
var cards = deal.call()
for (m in 52..1) {
var index = (msc.nextInt() % m).floor
var temp = cards[index]
cards[index] = cards[m - 1]
System.write("%(temp) ")
if ((53 - m) % 8 == 0) System.print()
}
System.print("\n")
}
 
game.call(1)
game.call(617)</syntaxhighlight>
 
{{out}}
<pre>
Game #1:
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
2♠ K♠ 9♦ Q♦ J♠ A♠ A♥ 3♣
4♣ 5♣ T♠ Q♥ 4♥ A♣ 4♦ 7♠
3♠ T♦ 4♠ T♥ 8♥ 2♣ J♥ 7♦
6♦ 8♠ 8♦ Q♠ 6♣ 3♦ 8♣ T♣
6♠ 9♣ 2♥ 6♥
 
Game #617:
7♦ A♦ 5♣ 3♠ 5♠ 8♣ 2♦ A♥
T♦ 7♠ Q♦ A♣ 6♦ 8♥ A♠ K♥
T♥ Q♣ 3♥ 9♦ 6♠ 8♦ 3♦ T♣
K♦ 5♥ 9♠ 3♣ 8♠ 7♥ 4♦ J♠
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated string convention
int RandState;
 
func Rand; \Random number in range 0 to 32767
[RandState:= (214013*RandState + 2531011) & $7FFF_FFFF;
return RandState >> 16;
];
 
int Card, Deck(52), Size;
char Suit, Rank;
[RandState:= IntIn(8); \seed RNG with number from command line
for Card:= 0 to 52-1 do Deck(Card):= Card; \create array of 52 cards
Rank:= "A23456789TJQK";
Suit:= "CDHS";
Size:= 52;
repeat Card:= rem(Rand/Size); \choose a random card
ChOut(0, Rank(Deck(Card)/4)); \deal it by showing it
ChOut(0, Suit(rem(0)));
if rem(Size/8)=5 then CrLf(0) else ChOut(0, ^ );
Size:= Size-1; \one less card in deck
Deck(Card):= Deck(Size); \replace dealt card with last card
until Size = 0; \all cards have been dealt
]</syntaxhighlight>
 
Output:
 
<pre>
7D AD 5C 3S 5S 8C 2D AH
TD 7S QD AC 6D 8H AS KH
TH QC 3H 9D 6S 8D 3D TC
KD 5H 9S 3C 8S 7H 4D JS
4C QS 9C 9H 7C 6H 2C 2S
4S TS 2H 5D JC 6C JH QH
JD KS KC 4H
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">var suits=T(0x1F0D1,0x1F0C1,0x1F0B1,0x1F0A1); //unicode 🃑,🃁,🂱,🂡
 
var seed=1; const RMAX32=(1).shiftLeft(31) - 1;
fcn rnd{ (seed=((seed*214013 + 2531011).bitAnd(RMAX32))).shiftRight(16) }
 
fcn game(n){
seed=n;
deck:=(0).pump(52,List,'wrap(n){ if(n>=44) n+=4; // I want JQK, not JCQ
(suits[n%4] + n/4).toString(8) }).copy(); // int-->UTF-8
[52..1,-1].pump(Void,'wrap(len){ deck.swap(len-1,rnd()%len); });
deck.reverse();
println("Game #",n);
foreach n in ([0..51,8]){ deck[n,8].concat(" ").println(); }
}
 
game(1);
game(617);
</syntaxhighlight>
{{out}}
<pre>
Game #1
🃋 🃂 🂹 🃛 🃅 🂷 🃗 🂵
🃎 🃞 🂩 🂥 🃁 🃝 🂾 🂳
🂢 🂮 🃉 🃍 🂫 🂡 🂱 🃓
🃔 🃕 🂪 🂽 🂴 🃑 🃄 🂧
🂣 🃊 🂤 🂺 🂸 🃒 🂻 🃇
🃆 🂨 🃈 🂭 🃖 🃃 🃘 🃚
🂦 🃙 🂲 🂶
Game #617
🃇 🃁 🃕 🂣 🂥 🃘 🃂 🂱
🃊 🂧 🃍 🃑 🃆 🂸 🂡 🂾
🂺 🃝 🂳 🃉 🂦 🃈 🃃 🃚
🃎 🂵 🂩 🃓 🂨 🂷 🃄 🂫
🃔 🂭 🃙 🂹 🃗 🂶 🃒 🂢
🂤 🂪 🂲 🃅 🃛 🃖 🂻 🂽
🃋 🂮 🃞 🂴
</pre>
 
{{omit from|ZX Spectrum Basic|Cannot calculate high numbers for the generator accurately enough}}
9,485

edits