Deal cards for FreeCell: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Delphi example)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(17 intermediate revisions by 11 users not shown)
Line 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 60:
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.
 
<br><br>
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}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
procedure FreeCell is
type State is mod 2**31;
Line 100 ⟶ 153:
New_Line;
Deal(617);
end FreeCell;</langsyntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
Line 119 ⟶ 172:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">FreeCell(num){
cards := "A23456789TJQK", suits := "♣♦♥♠", card := [], Counter := 0
loop, parse, cards
Line 142 ⟶ 195:
Seed := Mod(214013 * Seed + 2531011, 2147483648)
return, [Seed, Seed // 65536]
}</langsyntaxhighlight>
MS() found at http://rosettacode.org/wiki/Linear_congruential_generator#AutoHotkey
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">Gui, font, s12, Courier
Gui, add, edit, w320 r17 -VScroll, % "Game# 1`n" FreeCell(1) "`n`nGame#617`n" FreeCell(617)
Gui, show
Line 152 ⟶ 205:
GuiEscape:
ExitApp
return</langsyntaxhighlight>
Outputs:<pre>Game# 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
Line 173 ⟶ 226:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> *FLOAT 64
hand% = 617
Line 217 ⟶ 270:
NEXT
ENDIF
= state >> 16</langsyntaxhighlight>
{{out}}
[[File:freecell_BBC.gif]]
 
=={{header|Befunge}}==
<langsyntaxhighlight 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*#+^#<</langsyntaxhighlight>
{{out}}
<pre>Game number: 1
Line 239 ⟶ 292:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( createArray
= array rank ranks suit suits
. A 2 3 4 5 6 7 8 9 T J Q K:?ranks
Line 301 ⟶ 354:
& put$(deal$(!deck.617),"dealt.txt",APP)
&
)</langsyntaxhighlight>
Content of <code>dealt.txt</code>:
<pre>Game #1
Line 323 ⟶ 376:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
Line 368 ⟶ 421:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Longer than it absolutely needs to be because I split out several independently useful classes.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Text;
Line 541 ⟶ 594:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
Line 562 ⟶ 615:
===Shorter version===
Shorter than the previous version. Adds a few classes, but stays closer to the gist of the C version.
<langsyntaxhighlight lang="csharp">
using System;
using System.Text;
Line 622 ⟶ 675:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Deck 1
Line 644 ⟶ 697:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 721 ⟶ 774:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
Output:
<pre>
Line 749 ⟶ 802:
This is written using a more object-oriented approach than the version above.
 
<langsyntaxhighlight lang="cpp">#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
Line 821 ⟶ 874:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Deck 1
Line 843 ⟶ 896:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void freeCellDeal() {
 
//a function that returns a random number generating function
Line 875 ⟶ 928:
print("\n");
deal(617);
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def deck (into [] (for [rank "A23456789TJQK" suit "CDHS"] (str rank suit))))
 
(defn lcg [seed]
Line 893 ⟶ 946:
(reverse (reduce xchg deck (gen seed))))))
 
(show 1)</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun make-rng (seed)
#'(lambda ()
(ash (setf seed (mod (+ (* 214013 seed) 2531011) (expt 2 31))) -16)))
Line 921 ⟶ 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 {
Line 963 ⟶ 1,016:
cards.deal(seed);
cards.show;
}</langsyntaxhighlight>
<pre>Hand 11982
AH AS 4H AC 2D 6S TS JS
Line 975 ⟶ 1,028:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Deal_cards_for_FreeCell;
 
Line 1,084 ⟶ 1,137:
Writeln('Deck 617'#10, Deck.ToString);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Deck 1
Line 1,103 ⟶ 1,156:
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}}
<langsyntaxhighlight lang="elixir">defmodule FreeCell do
import Bitwise
Line 1,133 ⟶ 1,224:
 
System.argv |> Enum.map(&String.to_integer/1)
|> FreeCell.deal</langsyntaxhighlight>
 
{{out}}
Line 1,158 ⟶ 1,249:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM FREECELL
 
Line 1,209 ⟶ 1,300:
SHOW(CARDS%[])
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,232 ⟶ 1,323:
</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?
<langsyntaxhighlight lang="fsharp">
let msKindaRand seed =
let state = ref seed
Line 1,266 ⟶ 1,357:
 
[1; 617] |> List.iter game
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,290 ⟶ 1,381:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel literals make math
math.functions namespaces qw sequences sequences.extras ;
IN: rosetta-code.freecell
Line 1,321 ⟶ 1,412:
: freecell ( -- ) 1 617 [ .game ] bi@ ;
 
MAIN: freecell</langsyntaxhighlight>
{{out}}
<pre>
Line 1,346 ⟶ 1,437:
{{works with|Fortran|95 and later}}
Using the lcgs module from [[Linear congruential generator#Fortran]]:
<langsyntaxhighlight Fortranlang="fortran">module Freecell
use lcgs
implicit none
Line 1,400 ⟶ 1,491:
call Freecelldeal(617)
end program</langsyntaxhighlight>
{{out}}
<pre>Game #1
Line 1,421 ⟶ 1,512:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 04-11-2016
' compile with: fbc -s console
 
Line 1,482 ⟶ 1,573:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>game #1
Line 1,504 ⟶ 1,595:
=={{header|Fōrmulæ}}==
 
In [http{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Deal_cards_for_FreeCell this] page you can see the solution of this task.}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
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 option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
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}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,576 ⟶ 1,679:
fmt.Printf("\nGame #%d\n", game)
show(deal(game))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,599 ⟶ 1,702:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">
class FreeCell{
int seed
Line 1,641 ⟶ 1,744:
freecell.dealGame()
freecell.dealGame(617)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,666 ⟶ 1,769:
=={{header|Haskell}}==
{{trans|C}}
<langsyntaxhighlight lang="haskell">import Data.Int
import Data.Bits
import Data.List
Line 1,701 ⟶ 1,804:
putStrLn $ "Deal " ++ show s ++ ":"
let cards = deal s
showCards cards</langsyntaxhighlight>
 
Execution:
Line 1,716 ⟶ 1,819:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(A) # freecelldealer
freecelldealer(\A[1] | &null) # seed from command line
end
Line 1,758 ⟶ 1,861:
procedure rand_freecell() #: lcrng
return ishift(srand_freecell((214013 * srand_freecell() + 2531011) % 2147483648),-16)
end</langsyntaxhighlight>
{{out|Sample output for game 1}}
<pre>Hand:
Line 1,773 ⟶ 1,876:
=={{header|J}}==
Paraphrase of [[#C|C]]:
<langsyntaxhighlight lang="j">deck=: ,/ 'A23456789TJQK' ,"0/ 7 u: '♣♦♥♠'
 
srnd=: 3 :'SEED=:{.y,11982'
Line 1,784 ⟶ 1,887:
deal=: |.@(swaps pairs) bind deck
 
show=: (,"2)@:(_8 ]\ ' '&,.)</langsyntaxhighlight>
{{out|Example use}}
<langsyntaxhighlight lang="j"> show deal srnd 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥
Line 1,801 ⟶ 1,904:
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥
J♦ K♠ K♣ 4♥ </langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.Arrays;
 
Line 1,867 ⟶ 1,970:
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,889 ⟶ 1,992:
=={{header|JavaScript}}==
 
<langsyntaxhighlight lang="javascript">"use strict";
/*
* Microsoft C Run-time-Library-compatible Random Number Generator
Line 1,976 ⟶ 2,079:
return columns.map(render_column).join("");
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{trans|Lua}}
<langsyntaxhighlight lang="julia">const rank = split("A23456789TJQK", "")
const suit = split("♣♦♥♠", "")
const deck = Vector{String}()
Line 2,003 ⟶ 2,106:
deal(617)
deal()
</langsyntaxhighlight> {{output}} <pre>
Game # 1
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥
Line 2,031 ⟶ 2,134:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
class Lcg(val a: Long, val c: Long, val m: Long, val d: Long, val s: Long) {
Line 2,073 ⟶ 2,176:
game(1)
game(617)
}</langsyntaxhighlight>
 
{{out}}
Line 2,097 ⟶ 2,200:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">; Linear congruential random number generator
make "_lcg_state 0
 
Line 2,152 ⟶ 2,255:
print_deal 617
print "||
bye</langsyntaxhighlight>
{{Out}}
<pre>Game #1
Line 2,174 ⟶ 2,277:
=={{header|Lua}}==
Uses bit32 library added in Lua 5.2.
<langsyntaxhighlight Lualang="lua">deck = {}
rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}
suit = {"C", "D", "H", "S"}
Line 2,209 ⟶ 2,312:
 
deal(1)
deal(617)</langsyntaxhighlight>
{{out}}
<pre>Game #1
Line 2,229 ⟶ 2,332:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">next[last_] := Mod[214013 last + 2531011, 2^31];
deal[n_] :=
Module[{last = n, idx,
Line 2,241 ⟶ 2,344:
format[deal_] := Grid[Partition[deal, 8, 8, {1, 4}, Null]];
Print[format[deal[1]]];
Print[format[deal[617]]];</langsyntaxhighlight>
{{out}}
<pre>JD 2D 9H JC 5D 7H 7C 5H
Line 2,261 ⟶ 2,364:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import sequtils, strutils, os
 
proc randomGenerator(seed: int): iterator: int =
var seedstate = seed
return iterator: int =
while true:
seedstate = (seed.int64state * 214013 + 2531011) and int32.high
yield seedstate 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:
Line 2,285 ⟶ 2,388:
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</langsyntaxhighlight>
Output:
<pre>Hand 11982
Line 2,302 ⟶ 2,405:
=={{header|Objeck}}==
{{trans|C#}}
<langsyntaxhighlight lang="objeck">class FreeCell {
function : Main(args : String[]) ~ Nil {
Deal(1)->PrintLine();
Line 2,371 ⟶ 2,474:
return "{$value}{$suit}";
}
}</langsyntaxhighlight>
 
Output:
Line 2,396 ⟶ 2,499:
=={{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.
<langsyntaxhighlight lang="objc">#define RMAX32 ((1U << 31) - 1)
 
//--------------------------------------------------------------------
Line 2,484 ⟶ 2,587:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Deck 1
Line 2,507 ⟶ 2,610:
=={{header|OCaml}}==
{{trans|C}}
<langsyntaxhighlight lang="ocaml">let srnd x =
(* since OCaml's built-in int type is at least 31 (note: not 32) bits wide,
and this problem takes mod 2^31, it is just enough if we treat it as
Line 2,547 ⟶ 2,650:
Printf.printf "Deal %d:\n" s;
let cards = deal s in
show cards</langsyntaxhighlight>
{{out|Execution}}
<pre>$ ocaml freecell.ml 617
Line 2,561 ⟶ 2,664:
=={{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>.
<langsyntaxhighlight 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
Line 2,573 ⟶ 2,676:
deck[t]=deck[last]
)
};</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 2,617 ⟶ 2,720:
binmode STDOUT, ':encoding(utf-8)';
print "Hand $hand_idx\n";
print $string;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{trans|ERRE}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>atom seed
<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>
function xrnd()
seed = and_bits(seed*214013+2531011,#7FFFFFFF)
<span style="color: #008080;">function</span> <span style="color: #000000;">xrnd</span><span style="color: #0000FF;">()</span>
return floor(seed/power(2,16))
<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>
end function
<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>
sequence cards = repeat(0,52)
 
<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>
procedure deal(integer game_num)
seed = game_num
<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>
for i=1 to 52 do
<span style="color: #000000;">seed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">game_num</span>
cards[i] = 52-i
<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>
end for
<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>
for i=1 to 51 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
integer j = 52-mod(xrnd(),53-i)
<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>
integer s = cards[i]
<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>
cards[i] = cards[j]
<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>
cards[j] = s
<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>
end for
<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>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
constant suits = "CDHS",
ranks = "A23456789TJQK"
<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>
procedure show()
for idx=1 to 52 do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
integer rank = floor(cards[idx]/4)+1
<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>
integer suit = mod(cards[idx],4)+1
<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>
integer eol = remainder(idx-1,13)=12
<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>
printf(1,"%c%c%s",{ranks[rank],suits[suit],iff(eol?"\n":" ")})
<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>
end for
<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>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
integer game_num = 1
--integer game_num=617
<span style="color: #004080;">integer</span> <span style="color: #000000;">game_num</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
deal(game_num)
<span style="color: #000080;font-style:italic;">--integer game_num=617</span>
printf(1,"hand %d\n",{game_num})
<span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">game_num</span><span style="color: #0000FF;">)</span>
show()
<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>
</lang>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,678 ⟶ 2,782:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">class FreeCell_Deal {
 
protected $deck = array(
Line 2,739 ⟶ 2,843:
}
 
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,772 ⟶ 2,876:
=={{header|PicoLisp}}==
Using the random generator from [[Linear congruential generator#PicoLisp]]:
<langsyntaxhighlight PicoLisplang="picolisp">(setq *MsSeed 11982)
 
(de msRand ()
Line 2,791 ⟶ 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 2,840 ⟶ 2,944:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out|Sample output}}
<pre>Hand #1
Line 2,869 ⟶ 2,973:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">
 
def randomGenerator(seed=1):
Line 2,898 ⟶ 3,002:
print("Hand {}".format(seed))
deck = deal(seed)
show(deck)</langsyntaxhighlight>
{{out}}
<pre>Hand 11982
Line 2,908 ⟶ 3,012:
JD 7D 8H 9C 2H QD 4C 5H
KC 8D 2S 3S</pre>
 
=={{header|Quackery}}==
 
<code>MCR-seed</code> and <code>MCR-rand</code> are defined at [[Linear congruential generator#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ [ [] 52 times
[ i^ join ] ]
constant ] is newpack ( --> n )
 
[ 2dup peek
dip [ over -1 peek ]
swap 2swap poke
-1 poke ] is to-end ( [ n --> [ )
 
[ [] swap
52 times
[ MCR-rand
over size mod
to-end
-1 split
swap dip join ]
drop ] is mixem ( [ --> [ )
 
[ 4 /mod
$ "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}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(module Linear_congruential_generator racket
Line 2,969 ⟶ 3,215:
(present-deal 1)
(newline)
(present-deal 617)</langsyntaxhighlight>
 
{{out}}
Line 2,993 ⟶ 3,239:
(formerly Perl 6)
{{works with|rakudo|2016.05}}
<syntaxhighlight lang="raku" perl6line>sub dealgame ($game-number = 1) {
sub ms-lcg-method($seed = $game-number) { ( 214013 * $seed + 2531011 ) % 2**31 }
 
Line 3,014 ⟶ 3,260:
 
dealgame;
dealgame 617;</langsyntaxhighlight>
{{out}}
<big><big><pre>Game #1
Line 3,039 ⟶ 3,285:
 
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).
<langsyntaxhighlight 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*/
Line 3,067 ⟶ 3,313:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
rand: state=(214013*state + 2531011) // 2**31; return state % 2**16 /*FreeCell rand#*/</langsyntaxhighlight>
'''output''' &nbsp; when using the default game number: &nbsp; <tt> 1 </tt>
<pre>
Line 3,094 ⟶ 3,340:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby"># games = ARGV converted to Integer
# No arguments? Pick any of first 32000 games.
begin
Line 3,127 ⟶ 3,373:
deck.each_slice(8) {|row| puts " " + row.join(" ")}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,151 ⟶ 3,397:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">projectDir$ = "a_project" ' project directory
imageDir$ = DefaultDir$ + "\projects\" + projectDir$ + "\image\" ' directory of deck images
imagePath$ = "../";projectDir$;"/image/" ' path of deck images
Line 3,183 ⟶ 3,429:
if card = 52 then end ' out of cards
next xx
next yy</langsyntaxhighlight>
[[File:freeCell.png]]
 
Line 3,190 ⟶ 3,436:
Based on JavaScript.
 
<langsyntaxhighlight lang="rust">// Code available at https://rosettacode.org/wiki/Linear_congruential_generator#Rust
extern crate linear_congruential_generator;
 
Line 3,238 ⟶ 3,484:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object Shuffler extends App {
 
private val suits = Array("C", "D", "H", "S")
Line 3,273 ⟶ 3,519:
println
deal(617)
}</langsyntaxhighlight>
 
{{out}}
Line 3,296 ⟶ 3,542:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
Line 3,360 ⟶ 3,606:
writeln("Hand " <& gameNum);
show(cards);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,386 ⟶ 3,632:
=={{header|Swift}}==
Swift 4.2. Largely based on the Objective-C example.
<langsyntaxhighlight Swiftlang="swift">enum Suit : String, CustomStringConvertible, CaseIterable {
case clubs = "C", diamonds = "D", hearts = "H", spades = "S"
var description: String {
Line 3,443 ⟶ 3,689:
let d617 = Deck(seed: 617)
print(d617)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,467 ⟶ 3,713:
=={{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 3,497 ⟶ 3,743:
set cards [deal $s]
puts "Hand $s"
show $cards</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|zsh}}
<langsyntaxhighlight lang="bash">test $# -gt 0 || set -- $((RANDOM % 32000))
for seed; do
print Game $seed:
Line 3,522 ⟶ 3,768:
done
print
done</langsyntaxhighlight>
{{out}}
<pre>$ zsh freecell.sh 80388
Line 3,533 ⟶ 3,779:
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}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated string convention
int RandState;
Line 3,558 ⟶ 3,875:
Deck(Card):= Deck(Size); \replace dealt card with last card
until Size = 0; \all cards have been dealt
]</langsyntaxhighlight>
 
Output:
Line 3,573 ⟶ 3,890:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var suits=T(0x1F0D1,0x1F0C1,0x1F0B1,0x1F0A1); //unicode 🃑,🃁,🂱,🂡
 
var seed=1; const RMAX32=(1).shiftLeft(31) - 1;
Line 3,590 ⟶ 3,907:
game(1);
game(617);
</syntaxhighlight>
</lang>
{{out}}
<pre>
9,476

edits