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