Playing cards: Difference between revisions

Content added Content deleted
(Updated both D entries)
Line 722: Line 722:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.random, std.algorithm, std.string, std.conv;
{{works with|D|2.051}}


struct Card {
<lang d>import std.stdio, std.random, std.algorithm, std.regexp ;
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;
import std.string : rjust = rjustify, ljust = ljustify ;
/*const*/ int pip, suit;


string toString() const {
enum Suit = ["Club", "Heart", "Diamond", "Spade"] ;
return format("%3s of %-7s", pips[pip], suits[suit]).
enum Pip = ["Ace","2","3","4","5","6","7","8","9","10","J","Q","K"];
rightJustify(15);
enum NSuit = Suit.length ;
}
enum NPip = Pip.length ;
enum NPack = Suit.length*Pip.length ;


@property int order() const nothrow {
struct Card {
static bool RankAceTop = true ;
auto pipOrder = (!rankAceTop) ?
pip :
int pip, suit ;
((pip != 0) ? pip - 1 : 12);
string toString() {
return (Pip[pip].rjust(3) ~ " of "~ Suit[suit].ljust(7)).rjust(15) ;
return pipOrder * suits.length + suit;
}
}

int order() @property {
bool opEqual(in Card rhs) const pure nothrow {
auto pipOrder = (!RankAceTop) ? pip : ((pip != 0) ? pip - 1 : 12) ;
return pipOrder * Suit.length + suit ;
return pip == rhs.pip && suit == rhs.suit;
}

int opCmp(in Card rhs) const nothrow {
return order - rhs.order;
}
}
int opCmp(Card rhs) { return order - rhs.order ; }
bool opEqual(Card rhs) { return pip == rhs.pip && suit == rhs.suit; }
}
}


class Deck {
final class Deck {
private Card[] cards ;
private Card[] cards;

this(bool initShuffle = true, int pack = 0) {
cards.length = 0 ;
this(in bool initShuffle = true, in int pack = 0) nothrow {
foreach(p;0..pack)
cards.length = 0;
foreach(c;0..NPack)
foreach (p; 0 .. pack)
cards ~= Card((c / NSuit) % NPip, c % NSuit) ;
foreach (c; 0 .. Card.nPack)
if(initShuffle) randomShuffle(cards) ;
cards ~= Card((c / Card.suits.length) %
Card.pips.length,
c % Card.suits.length);

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


int length() @property { return cards.length ; }
@property size_t length() const pure nothrow {
return cards.length;
}


Deck add(Card c) { cards ~= c; return this ; }
Deck add(in Card c) pure nothrow {
Deck deal(int loc, Deck toDeck = null) {
cards ~= 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) { return deal(length - 1, toDeck) ; }


Card opIndex(int loc) { return cards[loc] ; }
Deck deal(in int loc, Deck toDeck = null) pure nothrow {
if (toDeck !is null)
alias opIndex peek ;
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 showDeck() { writeln(this) ; return this ; }
Deck shuffle() {
Deck shuffle() {
randomShuffle(cards) ;
cards.randomShuffle();
return this ;
return this;
}
}

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

string toString() {
override string toString() const {
auto s = reduce!`a~","~b`(map!`format("%s", a)`(cards)) ;
string result;
return s.sub("([^,]+,[^,]+,[^,]+,[^,]+),", "$1\n", "g") ;
foreach (c; std.range.chunks(cards, 4))
result ~= map!text(c).join() ~ "\n";
return result;
}
}
}
}

void main() {
void main() {
Deck[4] guests ;
Deck[4] guests;
foreach(ref g ; guests)
foreach (ref g; guests)
g = new Deck; // empty deck
g = new Deck; // empty deck

Deck host = new Deck(false, 1) ;
auto host = new Deck(false, 1);
writeln("Host") ;
writeln("Host");
host.shuffle.showDeck ;
while(host.length > 0)
host.shuffle().showDeck();

foreach(ref g ; guests)
if(host.length > 0)
while (host.length > 0)
host.dealTop(g) ;
foreach (ref g; guests)
if (host.length > 0)
foreach(i, g ; guests) {
writefln("Player #%d", i + 1) ;
host.dealTop(g);

g.sortDeck.showDeck ;
foreach (i, g; guests) {
writefln("Player #%d", i + 1);
g.sortDeck().showDeck();
}
}
}</lang>
}</lang>
{{out}}
From the Python version:
<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;
<lang d>import std.random, std.conv, std.stdio;


Line 840: Line 914:
Card deal() {
Card deal() {
this.shuffle();
this.shuffle();
Card c = this.deck[$-1];
Card c = this.deck[$ - 1];
this.deck.length -= 1;
this.deck.length--;
return c;
return c;
}
}