Deal cards for FreeCell: Difference between revisions
Content added Content deleted
(→{{header|F#}}: Corrected header as suggested on the Count examples/Full list/Tier 4 talk page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 72: | Line 72: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F randomGenerator(=seed, n) |
||
[Int] r |
[Int] r |
||
-V max_int32 = 7FFF'FFFF |
-V max_int32 = 7FFF'FFFF |
||
Line 101: | Line 101: | ||
print(‘Hand #.’.format(seed)) |
print(‘Hand #.’.format(seed)) |
||
V deck = deal(seed) |
V deck = deal(seed) |
||
show(deck)</ |
show(deck)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 116: | Line 116: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure FreeCell is |
procedure FreeCell is |
||
type State is mod 2**31; |
type State is mod 2**31; |
||
Line 153: | Line 153: | ||
New_Line; |
New_Line; |
||
Deal(617); |
Deal(617); |
||
end FreeCell;</ |
end FreeCell;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>JD 2D 9H JC 5D 7H 7C 5H |
<pre>JD 2D 9H JC 5D 7H 7C 5H |
||
Line 172: | Line 172: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">FreeCell(num){ |
||
cards := "A23456789TJQK", suits := "♣♦♥♠", card := [], Counter := 0 |
cards := "A23456789TJQK", suits := "♣♦♥♠", card := [], Counter := 0 |
||
loop, parse, cards |
loop, parse, cards |
||
Line 195: | Line 195: | ||
Seed := Mod(214013 * Seed + 2531011, 2147483648) |
Seed := Mod(214013 * Seed + 2531011, 2147483648) |
||
return, [Seed, Seed // 65536] |
return, [Seed, Seed // 65536] |
||
}</ |
}</syntaxhighlight> |
||
MS() found at http://rosettacode.org/wiki/Linear_congruential_generator#AutoHotkey |
MS() found at http://rosettacode.org/wiki/Linear_congruential_generator#AutoHotkey |
||
Examples:< |
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, add, edit, w320 r17 -VScroll, % "Game# 1`n" FreeCell(1) "`n`nGame#617`n" FreeCell(617) |
||
Gui, show |
Gui, show |
||
Line 205: | Line 205: | ||
GuiEscape: |
GuiEscape: |
||
ExitApp |
ExitApp |
||
return</ |
return</syntaxhighlight> |
||
Outputs:<pre>Game# 1 |
Outputs:<pre>Game# 1 |
||
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
||
Line 226: | Line 226: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> *FLOAT 64 |
||
hand% = 617 |
hand% = 617 |
||
Line 270: | Line 270: | ||
NEXT |
NEXT |
||
ENDIF |
ENDIF |
||
= state >> 16</ |
= state >> 16</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[[File:freecell_BBC.gif]] |
[[File:freecell_BBC.gif]] |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang="befunge">vutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDC |
||
>4$0" :rebmun emaG">:#,_$&>55+,>"O?+"**2+*"C4'' "**v |
>4$0" :rebmun emaG">:#,_$&>55+,>"O?+"**2+*"C4'' "**v |
||
>8%!492*+*48*\-,1-:11p0g\0p11g#^_@A23456789TJQKCDHS* |
>8%!492*+*48*\-,1-:11p0g\0p11g#^_@A23456789TJQKCDHS* |
||
^+3:g11,g2+"/"%4,g2+g14/4:-\"v"g0:%g11+*-/2-10-1*<>+ |
^+3:g11,g2+"/"%4,g2+g14/4:-\"v"g0:%g11+*-/2-10-1*<>+ |
||
>8#8*#4*#::#%*#*/#*:#*0#:\#*`#:8#::#*:#8*#8:#2*#+^#<</ |
>8#8*#4*#::#%*#*/#*:#*0#:\#*`#:8#::#*:#8*#8:#2*#+^#<</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Game number: 1 |
<pre>Game number: 1 |
||
Line 292: | Line 292: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( createArray |
||
= array rank ranks suit suits |
= array rank ranks suit suits |
||
. A 2 3 4 5 6 7 8 9 T J Q K:?ranks |
. A 2 3 4 5 6 7 8 9 T J Q K:?ranks |
||
Line 354: | Line 354: | ||
& put$(deal$(!deck.617),"dealt.txt",APP) |
& put$(deal$(!deck.617),"dealt.txt",APP) |
||
& |
& |
||
)</ |
)</syntaxhighlight> |
||
Content of <code>dealt.txt</code>: |
Content of <code>dealt.txt</code>: |
||
<pre>Game #1 |
<pre>Game #1 |
||
Line 376: | Line 376: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <locale.h> |
#include <locale.h> |
||
Line 421: | Line 421: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Longer than it absolutely needs to be because I split out several independently useful classes. |
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.Collections.Generic; |
||
using System.Text; |
using System.Text; |
||
Line 594: | Line 594: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>JD 2D 9H JC 5D 7H 7C 5H |
<pre>JD 2D 9H JC 5D 7H 7C 5H |
||
Line 615: | Line 615: | ||
===Shorter version=== |
===Shorter version=== |
||
Shorter than the previous version. Adds a few classes, but stays closer to the gist of the C 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; |
||
using System.Text; |
using System.Text; |
||
Line 675: | Line 675: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Deck 1 |
<pre>Deck 1 |
||
Line 697: | Line 697: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 774: | Line 774: | ||
} |
} |
||
//-------------------------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 802: | Line 802: | ||
This is written using a more object-oriented approach than the version above. |
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 <iostream> // std::cout |
||
#include <sstream> // std::stringstream |
#include <sstream> // std::stringstream |
||
Line 874: | Line 874: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Deck 1 |
<pre>Deck 1 |
||
Line 896: | Line 896: | ||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">shared void freeCellDeal() { |
||
//a function that returns a random number generating function |
//a function that returns a random number generating function |
||
Line 928: | Line 928: | ||
print("\n"); |
print("\n"); |
||
deal(617); |
deal(617); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(def deck (into [] (for [rank "A23456789TJQK" suit "CDHS"] (str rank suit)))) |
||
(defn lcg [seed] |
(defn lcg [seed] |
||
Line 946: | Line 946: | ||
(reverse (reduce xchg deck (gen seed)))))) |
(reverse (reduce xchg deck (gen seed)))))) |
||
(show 1)</ |
(show 1)</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun make-rng (seed) |
||
#'(lambda () |
#'(lambda () |
||
(ash (setf seed (mod (+ (* 214013 seed) 2531011) (expt 2 31))) -16))) |
(ash (setf seed (mod (+ (* 214013 seed) 2531011) (expt 2 31))) -16))) |
||
Line 974: | Line 974: | ||
(show-deck 1) |
(show-deck 1) |
||
(show-deck 617)</ |
(show-deck 617)</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.conv, std.algorithm, std.range; |
||
struct RandomGenerator { |
struct RandomGenerator { |
||
Line 1,016: | Line 1,016: | ||
cards.deal(seed); |
cards.deal(seed); |
||
cards.show; |
cards.show; |
||
}</ |
}</syntaxhighlight> |
||
<pre>Hand 11982 |
<pre>Hand 11982 |
||
AH AS 4H AC 2D 6S TS JS |
AH AS 4H AC 2D 6S TS JS |
||
Line 1,028: | Line 1,028: | ||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{Trans|C#}} |
{{Trans|C#}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Deal_cards_for_FreeCell; |
program Deal_cards_for_FreeCell; |
||
Line 1,137: | Line 1,137: | ||
Writeln('Deck 617'#10, Deck.ToString); |
Writeln('Deck 617'#10, Deck.ToString); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Deck 1 |
<pre>Deck 1 |
||
Line 1,158: | Line 1,158: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule FreeCell do |
||
import Bitwise |
import Bitwise |
||
Line 1,186: | Line 1,186: | ||
System.argv |> Enum.map(&String.to_integer/1) |
System.argv |> Enum.map(&String.to_integer/1) |
||
|> FreeCell.deal</ |
|> FreeCell.deal</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,211: | Line 1,211: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM FREECELL |
PROGRAM FREECELL |
||
Line 1,262: | Line 1,262: | ||
SHOW(CARDS%[]) |
SHOW(CARDS%[]) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,287: | Line 1,287: | ||
=={{header|F_sharp|F#}}== |
=={{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? |
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 msKindaRand seed = |
||
let state = ref seed |
let state = ref seed |
||
Line 1,319: | Line 1,319: | ||
[1; 617] |> List.iter game |
[1; 617] |> List.iter game |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,343: | Line 1,343: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting grouping io kernel literals make math |
||
math.functions namespaces qw sequences sequences.extras ; |
math.functions namespaces qw sequences sequences.extras ; |
||
IN: rosetta-code.freecell |
IN: rosetta-code.freecell |
||
Line 1,374: | Line 1,374: | ||
: freecell ( -- ) 1 617 [ .game ] bi@ ; |
: freecell ( -- ) 1 617 [ .game ] bi@ ; |
||
MAIN: freecell</ |
MAIN: freecell</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,399: | Line 1,399: | ||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
Using the lcgs module from [[Linear congruential generator#Fortran]]: |
Using the lcgs module from [[Linear congruential generator#Fortran]]: |
||
< |
<syntaxhighlight lang="fortran">module Freecell |
||
use lcgs |
use lcgs |
||
implicit none |
implicit none |
||
Line 1,453: | Line 1,453: | ||
call Freecelldeal(617) |
call Freecelldeal(617) |
||
end program</ |
end program</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Game #1 |
<pre>Game #1 |
||
Line 1,474: | Line 1,474: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 04-11-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,535: | Line 1,535: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>game #1 |
<pre>game #1 |
||
Line 1,565: | Line 1,565: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,629: | Line 1,629: | ||
fmt.Printf("\nGame #%d\n", game) |
fmt.Printf("\nGame #%d\n", game) |
||
show(deal(game)) |
show(deal(game)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,652: | Line 1,652: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy"> |
||
class FreeCell{ |
class FreeCell{ |
||
int seed |
int seed |
||
Line 1,694: | Line 1,694: | ||
freecell.dealGame() |
freecell.dealGame() |
||
freecell.dealGame(617) |
freecell.dealGame(617) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,719: | Line 1,719: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="haskell">import Data.Int |
||
import Data.Bits |
import Data.Bits |
||
import Data.List |
import Data.List |
||
Line 1,754: | Line 1,754: | ||
putStrLn $ "Deal " ++ show s ++ ":" |
putStrLn $ "Deal " ++ show s ++ ":" |
||
let cards = deal s |
let cards = deal s |
||
showCards cards</ |
showCards cards</syntaxhighlight> |
||
Execution: |
Execution: |
||
Line 1,769: | Line 1,769: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main(A) # freecelldealer |
||
freecelldealer(\A[1] | &null) # seed from command line |
freecelldealer(\A[1] | &null) # seed from command line |
||
end |
end |
||
Line 1,811: | Line 1,811: | ||
procedure rand_freecell() #: lcrng |
procedure rand_freecell() #: lcrng |
||
return ishift(srand_freecell((214013 * srand_freecell() + 2531011) % 2147483648),-16) |
return ishift(srand_freecell((214013 * srand_freecell() + 2531011) % 2147483648),-16) |
||
end</ |
end</syntaxhighlight> |
||
{{out|Sample output for game 1}} |
{{out|Sample output for game 1}} |
||
<pre>Hand: |
<pre>Hand: |
||
Line 1,826: | Line 1,826: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Paraphrase of [[#C|C]]: |
Paraphrase of [[#C|C]]: |
||
< |
<syntaxhighlight lang="j">deck=: ,/ 'A23456789TJQK' ,"0/ 7 u: '♣♦♥♠' |
||
srnd=: 3 :'SEED=:{.y,11982' |
srnd=: 3 :'SEED=:{.y,11982' |
||
Line 1,837: | Line 1,837: | ||
deal=: |.@(swaps pairs) bind deck |
deal=: |.@(swaps pairs) bind deck |
||
show=: (,"2)@:(_8 ]\ ' '&,.)</ |
show=: (,"2)@:(_8 ]\ ' '&,.)</syntaxhighlight> |
||
{{out|Example use}} |
{{out|Example use}} |
||
< |
<syntaxhighlight lang="j"> show deal srnd 1 |
||
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
||
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥ |
K♦ K♣ 9♠ 5♠ A♦ Q♣ K♥ 3♥ |
||
Line 1,854: | Line 1,854: | ||
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠ |
4♣ Q♠ 9♣ 9♥ 7♣ 6♥ 2♣ 2♠ |
||
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥ |
4♠ T♠ 2♥ 5♦ J♣ 6♣ J♥ Q♥ |
||
J♦ K♠ K♣ 4♥ </ |
J♦ K♠ K♣ 4♥ </syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
import java.util.Arrays; |
import java.util.Arrays; |
||
Line 1,920: | Line 1,920: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,942: | Line 1,942: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">"use strict"; |
||
/* |
/* |
||
* Microsoft C Run-time-Library-compatible Random Number Generator |
* Microsoft C Run-time-Library-compatible Random Number Generator |
||
Line 2,029: | Line 2,029: | ||
return columns.map(render_column).join(""); |
return columns.map(render_column).join(""); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Lua}} |
{{trans|Lua}} |
||
< |
<syntaxhighlight lang="julia">const rank = split("A23456789TJQK", "") |
||
const suit = split("♣♦♥♠", "") |
const suit = split("♣♦♥♠", "") |
||
const deck = Vector{String}() |
const deck = Vector{String}() |
||
Line 2,056: | Line 2,056: | ||
deal(617) |
deal(617) |
||
deal() |
deal() |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Game # 1 |
Game # 1 |
||
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
J♦ 2♦ 9♥ J♣ 5♦ 7♥ 7♣ 5♥ |
||
Line 2,084: | Line 2,084: | ||
=={{header|Kotlin}}== |
=={{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) { |
class Lcg(val a: Long, val c: Long, val m: Long, val d: Long, val s: Long) { |
||
Line 2,126: | Line 2,126: | ||
game(1) |
game(1) |
||
game(617) |
game(617) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,150: | Line 2,150: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">; Linear congruential random number generator |
||
make "_lcg_state 0 |
make "_lcg_state 0 |
||
Line 2,205: | Line 2,205: | ||
print_deal 617 |
print_deal 617 |
||
print "|| |
print "|| |
||
bye</ |
bye</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Game #1 |
<pre>Game #1 |
||
Line 2,227: | Line 2,227: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Uses bit32 library added in Lua 5.2. |
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"} |
rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"} |
||
suit = {"C", "D", "H", "S"} |
suit = {"C", "D", "H", "S"} |
||
Line 2,262: | Line 2,262: | ||
deal(1) |
deal(1) |
||
deal(617)</ |
deal(617)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Game #1 |
<pre>Game #1 |
||
Line 2,282: | Line 2,282: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">next[last_] := Mod[214013 last + 2531011, 2^31]; |
||
deal[n_] := |
deal[n_] := |
||
Module[{last = n, idx, |
Module[{last = n, idx, |
||
Line 2,294: | Line 2,294: | ||
format[deal_] := Grid[Partition[deal, 8, 8, {1, 4}, Null]]; |
format[deal_] := Grid[Partition[deal, 8, 8, {1, 4}, Null]]; |
||
Print[format[deal[1]]]; |
Print[format[deal[1]]]; |
||
Print[format[deal[617]]];</ |
Print[format[deal[617]]];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>JD 2D 9H JC 5D 7H 7C 5H |
<pre>JD 2D 9H JC 5D 7H 7C 5H |
||
Line 2,314: | Line 2,314: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import sequtils, strutils, os |
||
proc randomGenerator(seed: int): iterator: int = |
proc randomGenerator(seed: int): iterator: int = |
||
Line 2,342: | Line 2,342: | ||
echo "Hand ", seed |
echo "Hand ", seed |
||
let deck = deal seed |
let deck = deal seed |
||
show deck</ |
show deck</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Hand 11982 |
<pre>Hand 11982 |
||
Line 2,355: | Line 2,355: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="objeck">class FreeCell { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Deal(1)->PrintLine(); |
Deal(1)->PrintLine(); |
||
Line 2,424: | Line 2,424: | ||
return "{$value}{$suit}"; |
return "{$value}{$suit}"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,449: | Line 2,449: | ||
=={{header|Objective-C}}== |
=={{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. |
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) |
||
//-------------------------------------------------------------------- |
//-------------------------------------------------------------------- |
||
Line 2,537: | Line 2,537: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Deck 1 |
<pre>Deck 1 |
||
Line 2,560: | Line 2,560: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="ocaml">let srnd x = |
||
(* since OCaml's built-in int type is at least 31 (note: not 32) bits wide, |
(* 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 |
and this problem takes mod 2^31, it is just enough if we treat it as |
||
Line 2,600: | Line 2,600: | ||
Printf.printf "Deal %d:\n" s; |
Printf.printf "Deal %d:\n" s; |
||
let cards = deal s in |
let cards = deal s in |
||
show cards</ |
show cards</syntaxhighlight> |
||
{{out|Execution}} |
{{out|Execution}} |
||
<pre>$ ocaml freecell.ml 617 |
<pre>$ ocaml freecell.ml 617 |
||
Line 2,614: | Line 2,614: | ||
=={{header|PARI/GP}}== |
=={{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>. |
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()={ |
nextrand()={ |
||
(state=(214013*state+2531011)%2^31)>>16 |
(state=(214013*state+2531011)%2^31)>>16 |
||
Line 2,626: | Line 2,626: | ||
deck[t]=deck[last] |
deck[t]=deck[last] |
||
) |
) |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 2,670: | Line 2,670: | ||
binmode STDOUT, ':encoding(utf-8)'; |
binmode STDOUT, ':encoding(utf-8)'; |
||
print "Hand $hand_idx\n"; |
print "Hand $hand_idx\n"; |
||
print $string;</ |
print $string;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|ERRE}} |
{{trans|ERRE}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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: #004080;">atom</span> <span style="color: #000000;">seed</span> |
||
Line 2,715: | Line 2,715: | ||
<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: #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> |
<span style="color: #000000;">show</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,732: | Line 2,732: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">class FreeCell_Deal { |
||
protected $deck = array( |
protected $deck = array( |
||
Line 2,793: | Line 2,793: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,826: | Line 2,826: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Using the random generator from [[Linear congruential generator#PicoLisp]]: |
Using the random generator from [[Linear congruential generator#PicoLisp]]: |
||
< |
<syntaxhighlight lang="picolisp">(setq *MsSeed 11982) |
||
(de msRand () |
(de msRand () |
||
Line 2,845: | Line 2,845: | ||
(prin " ^[[" (cadr C) "m" (cddr C) "^[[m" (car C)) |
(prin " ^[[" (cadr C) "m" (cddr C) "^[[m" (car C)) |
||
(at (0 . 8) (prinl)) ) |
(at (0 . 8) (prinl)) ) |
||
(prinl) )</ |
(prinl) )</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">#MaxCardNum = 51 ;zero-based count of cards in a deck |
||
Global deckSize |
Global deckSize |
||
Global Dim cards(#MaxCardNum) ;card with highest index is at the top of deck |
Global Dim cards(#MaxCardNum) ;card with highest index is at the top of deck |
||
Line 2,894: | Line 2,894: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre>Hand #1 |
<pre>Hand #1 |
||
Line 2,923: | Line 2,923: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python"> |
||
def randomGenerator(seed=1): |
def randomGenerator(seed=1): |
||
Line 2,952: | Line 2,952: | ||
print("Hand {}".format(seed)) |
print("Hand {}".format(seed)) |
||
deck = deal(seed) |
deck = deal(seed) |
||
show(deck)</ |
show(deck)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hand 11982 |
<pre>Hand 11982 |
||
Line 2,965: | Line 2,965: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">## Linear congruential generator code not original - |
||
## copied from |
## copied from |
||
## http://www.rosettacode.org/wiki/Linear_congruential_generator#R |
## http://www.rosettacode.org/wiki/Linear_congruential_generator#R |
||
Line 3,006: | Line 3,006: | ||
print(deal, quote = FALSE, na.print = "") |
print(deal, quote = FALSE, na.print = "") |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,034: | Line 3,034: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(module Linear_congruential_generator racket |
(module Linear_congruential_generator racket |
||
Line 3,092: | Line 3,092: | ||
(present-deal 1) |
(present-deal 1) |
||
(newline) |
(newline) |
||
(present-deal 617)</ |
(present-deal 617)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,116: | Line 3,116: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|rakudo|2016.05}} |
{{works with|rakudo|2016.05}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub dealgame ($game-number = 1) { |
||
sub ms-lcg-method($seed = $game-number) { ( 214013 * $seed + 2531011 ) % 2**31 } |
sub ms-lcg-method($seed = $game-number) { ( 214013 * $seed + 2531011 ) % 2**31 } |
||
Line 3,137: | Line 3,137: | ||
dealgame; |
dealgame; |
||
dealgame 617;</ |
dealgame 617;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<big><big><pre>Game #1 |
<big><big><pre>Game #1 |
||
Line 3,162: | Line 3,162: | ||
See the ''discussion'' page for support for '''game = ─1''' and '''game= ─2''' (minus one and minus two). |
See the ''discussion'' page for support for '''game = ─1''' and '''game= ─2''' (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 #*/ |
numeric digits 15 /*ensure enough digits for the random #*/ |
||
parse arg game cols . /*obtain optional arguments from the CL*/ |
parse arg game cols . /*obtain optional arguments from the CL*/ |
||
Line 3,190: | Line 3,190: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
rand: state=(214013*state + 2531011) // 2**31; return state % 2**16 /*FreeCell rand#*/</ |
rand: state=(214013*state + 2531011) // 2**31; return state % 2**16 /*FreeCell rand#*/</syntaxhighlight> |
||
'''output''' when using the default game number: <tt> 1 </tt> |
'''output''' when using the default game number: <tt> 1 </tt> |
||
<pre> |
<pre> |
||
Line 3,217: | Line 3,217: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby"># games = ARGV converted to Integer |
||
# No arguments? Pick any of first 32000 games. |
# No arguments? Pick any of first 32000 games. |
||
begin |
begin |
||
Line 3,250: | Line 3,250: | ||
deck.each_slice(8) {|row| puts " " + row.join(" ")} |
deck.each_slice(8) {|row| puts " " + row.join(" ")} |
||
puts |
puts |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,274: | Line 3,274: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">projectDir$ = "a_project" ' project directory |
||
imageDir$ = DefaultDir$ + "\projects\" + projectDir$ + "\image\" ' directory of deck images |
imageDir$ = DefaultDir$ + "\projects\" + projectDir$ + "\image\" ' directory of deck images |
||
imagePath$ = "../";projectDir$;"/image/" ' path of deck images |
imagePath$ = "../";projectDir$;"/image/" ' path of deck images |
||
Line 3,306: | Line 3,306: | ||
if card = 52 then end ' out of cards |
if card = 52 then end ' out of cards |
||
next xx |
next xx |
||
next yy</ |
next yy</syntaxhighlight> |
||
[[File:freeCell.png]] |
[[File:freeCell.png]] |
||
Line 3,313: | Line 3,313: | ||
Based on JavaScript. |
Based on JavaScript. |
||
< |
<syntaxhighlight lang="rust">// Code available at https://rosettacode.org/wiki/Linear_congruential_generator#Rust |
||
extern crate linear_congruential_generator; |
extern crate linear_congruential_generator; |
||
Line 3,361: | Line 3,361: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object Shuffler extends App { |
||
private val suits = Array("C", "D", "H", "S") |
private val suits = Array("C", "D", "H", "S") |
||
Line 3,396: | Line 3,396: | ||
println |
println |
||
deal(617) |
deal(617) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,419: | Line 3,419: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "console.s7i"; |
include "console.s7i"; |
||
Line 3,483: | Line 3,483: | ||
writeln("Hand " <& gameNum); |
writeln("Hand " <& gameNum); |
||
show(cards); |
show(cards); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,509: | Line 3,509: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
Swift 4.2. Largely based on the Objective-C example. |
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" |
case clubs = "C", diamonds = "D", hearts = "H", spades = "S" |
||
var description: String { |
var description: String { |
||
Line 3,566: | Line 3,566: | ||
let d617 = Deck(seed: 617) |
let d617 = Deck(seed: 617) |
||
print(d617) |
print(d617) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,590: | Line 3,590: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="tcl">proc rnd {{*r seed}} { |
||
upvar 1 ${*r} r |
upvar 1 ${*r} r |
||
expr {[set r [expr {($r * 214013 + 2531011) & 0x7fffffff}]] >> 16} |
expr {[set r [expr {($r * 214013 + 2531011) & 0x7fffffff}]] >> 16} |
||
Line 3,620: | Line 3,620: | ||
set cards [deal $s] |
set cards [deal $s] |
||
puts "Hand $s" |
puts "Hand $s" |
||
show $cards</ |
show $cards</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash">test $# -gt 0 || set -- $((RANDOM % 32000)) |
||
for seed; do |
for seed; do |
||
print Game $seed: |
print Game $seed: |
||
Line 3,645: | Line 3,645: | ||
done |
done |
||
print |
print |
||
done</ |
done</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ zsh freecell.sh 80388 |
<pre>$ zsh freecell.sh 80388 |
||
Line 3,659: | Line 3,659: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="ecmascript">class Lcg { |
||
construct new(a, c, m, d, s) { |
construct new(a, c, m, d, s) { |
||
_a = a |
_a = a |
||
Line 3,705: | Line 3,705: | ||
game.call(1) |
game.call(1) |
||
game.call(617)</ |
game.call(617)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,729: | Line 3,729: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
string 0; \use zero-terminated string convention |
string 0; \use zero-terminated string convention |
||
int RandState; |
int RandState; |
||
Line 3,752: | Line 3,752: | ||
Deck(Card):= Deck(Size); \replace dealt card with last card |
Deck(Card):= Deck(Size); \replace dealt card with last card |
||
until Size = 0; \all cards have been dealt |
until Size = 0; \all cards have been dealt |
||
]</ |
]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,767: | Line 3,767: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">var suits=T(0x1F0D1,0x1F0C1,0x1F0B1,0x1F0A1); //unicode 🃑,🃁,🂱,🂡 |
||
var seed=1; const RMAX32=(1).shiftLeft(31) - 1; |
var seed=1; const RMAX32=(1).shiftLeft(31) - 1; |
||
Line 3,784: | Line 3,784: | ||
game(1); |
game(1); |
||
game(617); |
game(617); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |