Playing cards: Difference between revisions
Content added Content deleted
m (→{{header|C}}) |
(Swapped and improved both D entries) |
||
Line 786: | Line 786: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.random, std.string, std.array, std.algorithm; |
|||
struct Card { |
|||
enum suits = ["Clubs", "Hearts", "Spades", "Diamonds"]; |
|||
enum pips = ["2", "3", "4", "5", "6", "7", "8", "9", "10", |
|||
"Jack", "Queen", "King", "Ace"]; |
|||
string pip, suit; |
|||
string toString() { |
|||
return pip ~ " of " ~ suit; |
|||
} |
|||
} |
|||
class Deck { |
|||
Card[] deck; |
|||
this() { |
|||
foreach (suit; Card.suits) |
|||
foreach (pip; Card.pips) |
|||
deck ~= Card(pip, suit); |
|||
} |
|||
void shuffle() { |
|||
deck.randomShuffle(); |
|||
} |
|||
Card deal() { |
|||
swap(deck.back, deck[uniform(0, $)]); |
|||
auto c = deck.back; |
|||
deck.popBack(); |
|||
return c; |
|||
} |
|||
override string toString() { |
|||
return format("%(%s\n%)", deck); |
|||
} |
|||
} |
|||
void main() { |
|||
auto deck = new Deck; // Make A new deck. |
|||
deck.shuffle(); // Shuffle the deck. |
|||
writeln("Card: ", deck.deal()); // Deal from the deck. |
|||
writeln(deck); // Print the current contents of the deck. |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Card: 8 of Spades |
|||
10 of Clubs |
|||
Queen of Hearts |
|||
3 of Clubs |
|||
Ace of Clubs |
|||
7 of Clubs |
|||
2 of Hearts |
|||
6 of Clubs |
|||
Jack of Spades |
|||
King of Clubs |
|||
Ace of Spades |
|||
3 of Hearts |
|||
7 of Spades |
|||
6 of Hearts |
|||
King of Diamonds |
|||
10 of Diamonds |
|||
8 of Diamonds |
|||
Jack of Diamonds |
|||
Queen of Diamonds |
|||
King of Hearts |
|||
4 of Diamonds |
|||
4 of Clubs |
|||
Jack of Hearts |
|||
4 of Hearts |
|||
7 of Hearts |
|||
9 of Spades |
|||
3 of Spades |
|||
King of Spades |
|||
Jack of Clubs |
|||
Ace of Hearts |
|||
6 of Diamonds |
|||
5 of Hearts |
|||
5 of Diamonds |
|||
10 of Spades |
|||
6 of Spades |
|||
8 of Clubs |
|||
2 of Clubs |
|||
Queen of Clubs |
|||
10 of Hearts |
|||
Ace of Diamonds |
|||
2 of Spades |
|||
2 of Diamonds |
|||
4 of Spades |
|||
8 of Hearts |
|||
7 of Diamonds |
|||
3 of Diamonds |
|||
9 of Diamonds |
|||
9 of Hearts |
|||
5 of Clubs |
|||
9 of Clubs |
|||
Queen of Spades |
|||
5 of Spades</pre> |
|||
===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.conv; |
||
Line 802: | Line 900: | ||
@property int order() const nothrow { |
@property int order() const nothrow { |
||
immutable pipOrder = (!rankAceTop) ? |
|||
pip : |
pip : |
||
(pip ? pip - 1 : 12); |
|||
return pipOrder * suits.length + suit; |
return pipOrder * suits.length + suit; |
||
} |
} |
||
Line 820: | Line 918: | ||
private Card[] cards; |
private Card[] cards; |
||
this(in bool initShuffle = true, in int pack = 0) |
this(in bool initShuffle = true, in int pack = 0) { |
||
cards.length = 0; |
cards.length = 0; |
||
foreach (p; 0 .. pack) |
foreach (immutable p; 0 .. pack) |
||
foreach (c; 0 .. Card.nPack) |
foreach (immutable c; 0 .. Card.nPack) |
||
cards ~= Card((c / Card.suits.length) % |
cards ~= Card((c / Card.suits.length) % |
||
Card.pips.length, |
Card.pips.length, |
||
Line 874: | Line 972: | ||
override string toString() const { |
override string toString() const { |
||
return format("%(%(%s%)\n%)", std.range.chunks(cards, 4)); |
|||
string result; |
|||
foreach (c; std.range.chunks(cards, 4)) |
|||
result ~= map!text(c).join() ~ "\n"; |
|||
return result; |
|||
} |
} |
||
} |
} |
||
Line 884: | Line 979: | ||
Deck[4] guests; |
Deck[4] guests; |
||
foreach (ref g; guests) |
foreach (ref g; guests) |
||
g = new Deck; // |
g = new Deck; // Empty deck. |
||
auto host = new Deck(false, 1); |
auto host = new Deck(false, 1); |
||
Line 895: | Line 990: | ||
host.dealTop(g); |
host.dealTop(g); |
||
foreach (i, g; guests) { |
foreach (immutable i, g; guests) { |
||
writefln("Player #%d", i + 1); |
writefln("Player #%d", i + 1); |
||
g.sortDeck().showDeck(); |
g.sortDeck().showDeck(); |
||
Line 902: | Line 997: | ||
{{out}} |
{{out}} |
||
<pre>Host |
<pre>Host |
||
6 of Heart Q of Club Ace of Club 9 of Spade |
|||
8 of Club K of Diamond Q of Heart 6 of Spade |
|||
7 of |
7 of Diamond 5 of Heart 6 of Diamond 8 of Heart |
||
4 of Club |
4 of Club J of Club 10 of Diamond 2 of Club |
||
9 of Diamond K of Club 4 of Spade 8 of Spade |
|||
5 of Club 3 of Club 9 of Club 3 of Diamond |
|||
2 of Diamond 9 of Heart 7 of Heart 10 of Club |
|||
Ace of Heart 2 of Spade J of Spade 5 of Diamond |
|||
2 of Heart 4 of Heart Ace of Diamond J of Heart |
|||
3 of Spade K of Spade 7 of Spade 5 of Spade |
|||
Ace of Spade 10 of Heart K of Heart 3 of Heart |
|||
6 of Club Q of Spade J of Diamond 8 of Diamond |
|||
10 of |
10 of Spade 7 of Club Q of Diamond 4 of Diamond |
||
Player #1 |
Player #1 |
||
J of Heart 10 of Club 9 of Spade 8 of Spade |
|||
8 of Diamond 8 of Heart 6 of Spade 5 of Spade |
|||
5 of Diamond 4 of Diamond 3 of Diamond 3 of Heart |
|||
2 of Club |
|||
Player #2 |
Player #2 |
||
Ace of |
Ace of Diamond Ace of Club K of Heart Q of Diamond |
||
Q of |
Q of Heart J of Spade J of Diamond 10 of Diamond |
||
9 of Club 7 of Spade 7 of Heart 6 of Diamond |
|||
4 of |
4 of Spade |
||
Player #3 |
Player #3 |
||
K of Spade K of Diamond K of Club Q of Spade |
|||
Q of Club J of Club 10 of Heart 9 of Heart |
|||
7 of Club 5 of Heart 4 of Heart 3 of Club |
|||
2 of |
2 of Spade |
||
Player #4 |
Player #4 |
||
Ace of Spade Ace of Heart 10 of Spade 9 of Diamond |
|||
8 of Club 7 of Diamond 6 of Heart 6 of Club |
|||
5 of Club 4 of Club 3 of Spade 2 of Diamond |
|||
2 of Heart </pre> |
2 of Heart </pre> |
||
===Alternative version=== |
|||
<lang d>import std.random, std.conv, std.stdio; |
|||
class Card { |
|||
enum suits = ["Clubs", "Hearts", "Spades", "Diamonds"]; |
|||
enum pips = ["2", "3", "4", "5", "6", "7", "8", "9", "10", |
|||
"Jack", "Queen", "King", "Ace"]; |
|||
string pip, suit; |
|||
this(string pip, string suit) { |
|||
this.pip = pip; |
|||
this.suit = suit; |
|||
} |
|||
override string toString() { |
|||
return this.pip ~ " of " ~ this.suit; |
|||
} |
|||
} |
|||
class Deck { |
|||
Card[] deck; |
|||
this() { |
|||
foreach (suit; Card.suits) |
|||
foreach (pip; Card.pips) |
|||
this.deck ~= new Card(pip, suit); |
|||
} |
|||
override string toString() { |
|||
return text(this.deck); |
|||
} |
|||
void shuffle() { |
|||
randomShuffle(this.deck); |
|||
} |
|||
Card deal() { |
|||
this.shuffle(); |
|||
Card c = this.deck[$ - 1]; |
|||
this.deck.length--; |
|||
return c; |
|||
} |
|||
} |
|||
void main() {}</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |