Playing cards: Difference between revisions

Content added Content deleted
(add scala)
(Updated second D entry)
Line 883: Line 883:
5 of Spades</pre>
5 of Spades</pre>
===More Refined Version===
===More Refined Version===
<lang d>import std.stdio, std.random, std.algorithm, std.string, std.conv;
<lang d>import std.stdio, std.random, std.algorithm, std.string, std.range;


struct Card {
struct Card {
static immutable suits = ["Club", "Heart", "Diamond", "Spade"];
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();
static immutable pips = "Ace 2 3 4 5 6 7 8 9 10 J Q K".split;
enum nPack = suits.length * pips.length;
enum nPack = suits.length * pips.length;


static bool rankAceTop = true;
static bool rankAceTop = true;
/*const*/ int pip, suit;
int pip, suit;


string toString() const {
string toString() pure const {
return format("%3s of %-7s", pips[pip], suits[suit]).
return format("%3s of %-7s", pips[pip], suits[suit])
rightJustify(15);
.rightJustify(15);
}
}


Line 926: Line 926:


if (initShuffle)
if (initShuffle)
cards.randomShuffle();
cards.randomShuffle;
}
}


Line 956: Line 956:


Deck showDeck() {
Deck showDeck() {
writeln(this);
this.writeln;
return this;
return this;
}
}


Deck shuffle() {
Deck shuffle() {
cards.randomShuffle();
cards.randomShuffle;
return this;
return this;
}
}


Deck sortDeck() {
Deck sortDeck() {
sort!q{a > b}(cards);
cards.sort!q{a > b};
return this;
return this;
}
}


override string toString() const {
override string toString() pure const {
return format("%(%(%s%)\n%)", std.range.chunks(cards, 4));
return format("%(%(%s%)\n%)", cards.chunks(4));
}
}
}
}
Line 982: Line 982:
auto host = new Deck(false, 1);
auto host = new Deck(false, 1);
writeln("Host");
writeln("Host");
host.shuffle().showDeck();
host.shuffle.showDeck;


while (host.length > 0)
while (host.length > 0)
Line 991: Line 991:
foreach (immutable i, g; guests) {
foreach (immutable i, g; guests) {
writefln("Player #%d", i + 1);
writefln("Player #%d", i + 1);
g.sortDeck().showDeck();
g.sortDeck.showDeck;
}
}
}</lang>
}</lang>