Jump to content

Playing cards: Difference between revisions

Updated second D entry
(add scala)
(Updated second D entry)
Line 883:
5 of Spades</pre>
===More Refined Version===
<lang d>import std.stdio, std.random, std.algorithm, std.string, std.convrange;
 
struct Card {
static immutable suits = ["Club", "Heart", "Diamond", "Spade"].split;
static immutable pips = "Ace 2 3 4 5 6 7 8 9 10 J Q K".split();
enum nPack = suits.length * pips.length;
 
static bool rankAceTop = true;
/*const*/ int pip, suit;
 
string toString() pure const {
return format("%3s of %-7s", pips[pip], suits[suit]).
.rightJustify(15);
}
 
Line 926:
 
if (initShuffle)
cards.randomShuffle();
}
 
Line 956:
 
Deck showDeck() {
writeln(this).writeln;
return this;
}
 
Deck shuffle() {
cards.randomShuffle();
return this;
}
 
Deck sortDeck() {
cards.sort!q{a > b}(cards);
return this;
}
 
override string toString() pure const {
return format("%(%(%s%)\n%)", std.rangecards.chunks(cards, 4));
}
}
Line 982:
auto host = new Deck(false, 1);
writeln("Host");
host.shuffle().showDeck();
 
while (host.length > 0)
Line 991:
foreach (immutable i, g; guests) {
writefln("Player #%d", i + 1);
g.sortDeck().showDeck();
}
}</lang>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.