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;
{{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 auto pipOrder = true(!rankAceTop) ;?
pip :
int pip, suit ;
((pip != 0) ? pip - 1 : 12);
string toString() {
return (Pip[pip].rjust(3)pipOrder ~* "suits.length of+ "~ Suit[suit].ljust(7)).rjust(15) ;
}
 
int order() @property {
bool opEqual(in Card rhs) const pure nothrow {
auto pipOrder = (!RankAceTop) ? pip : ((pip != 0) ? pip - 1 : 12) ;
return pipOrderpip *== Suitrhs.lengthpip +&& 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; }
}
 
final class Deck {
private Card[] cards ;
 
this(bool initShuffle = true, int pack = 0) {
this(in bool initShuffle = cards.lengthtrue, in int pack = 0) nothrow ;{
foreach(pcards.length = 0;0..pack)
foreach (cp; 0 ..NPack pack)
foreach cards ~= Card((c; /0 NSuit).. % NPip, c % NSuitCard.nPack) ;
if(initShuffle) randomShuffle( cards ~= Card((c / Card.suits.length) ;%
Card.pips.length,
c % Card.suits.length);
 
if (initShuffle)
cards.randomShuffle();
}
 
int@property size_t length() @propertyconst {pure returnnothrow cards.length ; }{
return cards.length;
}
 
Deck add(in Card c) {pure cardsnothrow ~= c; return this ; }{
Deck deal(int loc, Deck toDeckcards ~= null) {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) ; }
 
CardDeck opIndexdeal(in int loc), {Deck returntoDeck cards[loc]= ;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() {
cards.randomShuffle(cards) ;
return this ;
}
 
Deck sortDeck() {
sort!"q{a > b"}(cards) ;
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() {
Deck[4] guests ;
foreach (ref g ; guests)
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 > 0shuffle().showDeck();
 
foreach(ref g ; guests)
while if(host.length > 0)
foreach (ref g; host.dealTop(gguests) ;
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>
{{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;
 
Line 840 ⟶ 914:
Card deal() {
this.shuffle();
Card c = this.deck[$ - 1];
this.deck.length -= 1-;
return c;
}