Deal cards for FreeCell: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) (Added Delphi example) |
(Updated to work with Nim 1.4.) |
||
Line 2,262: | Line 2,262: | ||
{{trans|Python}} |
{{trans|Python}} |
||
<lang nim>import sequtils, strutils, os |
<lang nim>import sequtils, strutils, os |
||
proc randomGenerator(seed: int): iterator: int = |
proc randomGenerator(seed: int): iterator: int = |
||
var |
var state = seed |
||
return iterator: int = |
return iterator: int = |
||
while true: |
while true: |
||
state = (state * 214013 + 2531011) and int32.high |
|||
yield |
yield state shr 16 |
||
proc deal(seed): seq[int] = |
proc deal(seed: int): seq[int] = |
||
const nc = 52 |
const nc = 52 |
||
result = toSeq countdown(nc - 1, 0) |
result = toSeq countdown(nc - 1, 0) |
||
var rnd = randomGenerator seed |
var rnd = randomGenerator seed |
||
for i in 0 .. |
for i in 0 ..< nc: |
||
let r = rnd() |
let r = rnd() |
||
let j = (nc - 1) - r mod (nc - i) |
let j = (nc - 1) - r mod (nc - i) |
||
swap result[i], result[j] |
swap result[i], result[j] |
||
proc show(cards) = |
proc show(cards: seq[int]) = |
||
var l = newSeq[string]() |
var l = newSeq[string]() |
||
for c in cards: |
for c in cards: |
||
Line 2,285: | Line 2,285: | ||
for i in countup(0, cards.high, 8): |
for i in countup(0, cards.high, 8): |
||
echo " ", l[i..min(i+7, l.high)].join(" ") |
echo " ", l[i..min(i+7, l.high)].join(" ") |
||
let seed = if paramCount() == 1: paramStr(1).parseInt else: 11982 |
let seed = if paramCount() == 1: paramStr(1).parseInt else: 11982 |
||
echo "Hand ", seed |
echo "Hand ", seed |