Playing cards: Difference between revisions
Content added Content deleted
(Updated both D entries) |
|||
Line 722:
=={{header|D}}==
<lang d>import std.stdio, std.random, std.algorithm, std.string, std.conv;
struct Card {
static immutable suits = ["Club", "Heart", "Diamond", "Spade"];
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() const {
return format("%3s of %-7s", pips[pip], suits[suit]).
rightJustify(15);
}
@property int order() const nothrow {
pip :
((pip != 0) ? pip - 1 : 12);
return
}
bool opEqual(in Card rhs) const pure nothrow {
return
}
int opCmp(in Card rhs) const nothrow {
return order - rhs.order;
}
}
final class Deck {
private Card[] cards
this(in bool initShuffle =
foreach
Card.pips.length,
c % Card.suits.length);
if (initShuffle)
cards.randomShuffle();
}
return cards.length;
}
Deck add(in Card c)
return this;
}
if (toDeck !is null)
toDeck.add(cards[loc]);
cards = cards[0 .. loc] ~ cards[loc + 1 .. $];
return this;
}
Deck dealTop(Deck toDeck = null) pure nothrow {
return deal(length - 1, toDeck);
}
Card opIndex(in int loc) const pure nothrow {
return cards[loc];
}
alias opIndex peek;
Deck showDeck() {
writeln(this);
return this;
}
Deck shuffle() {
cards.randomShuffle(
return this
}
Deck sortDeck() {
sort!
return this
}
override string toString() const {
string result;
foreach (c; std.range.chunks(cards, 4))
result ~= map!text(c).join() ~ "\n";
return result;
}
}
void main() {
Deck[4] guests
foreach (ref g
g = new Deck; // empty deck
auto host = new Deck(false, 1);
writeln("Host");
while
foreach (ref g;
if (host.length > 0)
foreach (i, g; guests) {
writefln("Player #%d", i + 1);
g.sortDeck().showDeck();
}
}</lang>
{{out}}
<pre>Host
2 of Heart J of Diamond 7 of Diamond K of Club
K of Heart Ace of Heart J of Club 6 of Heart
7 of Spade 2 of Diamond Q of Diamond 5 of Diamond
4 of Club Q of Spade 5 of Spade 9 of Diamond
2 of Spade 9 of Club K of Spade 9 of Heart
3 of Club 8 of Heart 6 of Diamond 7 of Club
10 of Spade 10 of Heart 4 of Heart 9 of Spade
3 of Diamond 4 of Spade 8 of Spade 5 of Club
8 of Diamond 3 of Heart 5 of Heart K of Diamond
7 of Heart Ace of Diamond 8 of Club 6 of Spade
J of Spade 6 of Club Q of Heart 3 of Spade
10 of Club 2 of Club Ace of Spade 4 of Diamond
10 of Diamond Ace of Club Q of Club J of Heart
Player #1
K of Diamond K of Club J of Heart 9 of Spade
9 of Diamond 9 of Heart 7 of Club 6 of Spade
6 of Heart 5 of Diamond 5 of Club 4 of Diamond
3 of Spade
Player #2
Ace of Spade K of Spade Q of Diamond Q of Heart
Q of Club J of Club 8 of Spade 8 of Club
7 of Diamond 6 of Diamond 5 of Spade 5 of Heart
4 of Heart
Player #3
Ace of Diamond Ace of Heart Ace of Club Q of Spade
J of Diamond 10 of Heart 9 of Club 8 of Heart
6 of Club 4 of Spade 3 of Heart 2 of Diamond
2 of Club
Player #4
K of Heart J of Spade 10 of Spade 10 of Diamond
10 of Club 8 of Diamond 7 of Spade 7 of Heart
4 of Club 3 of Diamond 3 of Club 2 of Spade
2 of Heart </pre>
===Alternative version===
<lang d>import std.random, std.conv, std.stdio;
Line 840 ⟶ 914:
Card deal() {
this.shuffle();
Card c = this.deck[$ - 1];
this.deck.length
return c;
}
|