Jump to content

Deal cards for FreeCell: Difference between revisions

Added F#
m (→‎{{header|Kotlin}}: Removed one blank line before main())
(Added F#)
Line 1,100:
♠4 ♠T ♥2 ♦5 ♣J ♣6 ♥J ♥Q
♦J ♠K ♣K ♥4
</pre>
 
 
=={{header|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 int that?
<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
</lang>
 
{{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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.