Deal cards for FreeCell: Difference between revisions

Updated D version
(Added PicoLisp)
(Updated D version)
Line 106:
<lang d>import std.stdio, std.conv, std.algorithm, std.range;
 
struct RndGeneratorRandomGenerator {
uint seed = 1;
 
@property uint next() pure nothrow {
enum uint RMAX32seed = (1Useed * 214_013 <<+ 312_531_011) -& 1int.max;
seed = (seed * 214_013 + 2_531_011) & RMAX32;
return seed >> 16;
}
}
 
alias int[52]struct Deck; {
Deckint[52] cards;
 
Deck void deal(in intuint seed) /*pure nothrow*/ {
auto r = RndGeneratorRandomGenerator(seed);
// Can't be pure nothrow because of iota, DMD 2.055.
auto r = RndGenerator(seed);
 
// DMD 2.055 iota isn't pure nothrow. Can't remove this cast.
Deck cards;
// Can't remove this copy(iota(cast(int)cards.length - 1, -1, -1), cards[]);
copy(iota(cast(int)cards.length - 1, -1, -1), cards[]);
 
foreach (i, ref c; cards) {
immutable int j = swap(c, cards[(cards.length - 1) -r.next%(cards.length-i)]);
r.next % (cards.length - i);
swap(c, cards[j]);
}
 
void show() {
return cards; // Return a copy by value.
foreach (i, crow; std.range.chunks(cards[], 8)) {
}
foreach (c; row)
 
write(" ", nums"A23456789TJQK"[c / 4], suits"CDHS"[c % 4]);
void showDeck(const ref Deck cards) {
enum suits = "CDHS";
enum nums = "A23456789TJQK";
foreach (i, c; cards) {
write(" ", nums[c / 4], suits[c % 4]);
if (!((i+1) % 8))
writeln();
}
}
writeln();
}
 
void main(in string[] args) {
immutable uint seed = (args.length == 2) ? to!uint(args[1]) : 11_982;
to!uint(args[1]) : 11_982;
const cards = deal(seed);
writeln("Hand ", seed);
showDeck(Deck cards);
const cards = .deal(seed);
writelncards.show();
}</lang>
<pre>Hand 11982
Anonymous user