How to resolve the algorithm Playing cards step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Playing cards step by step in the D programming language
Table of Contents
Problem Statement
Create a data structure and the associated methods to define and manipulate a deck of playing cards. The deck should contain 52 unique cards. The methods must include the ability to: Each card must have a pip value and a suit value which constitute the unique value of the card. Related tasks:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Playing cards step by step in the D programming language
Source code in the d programming language
import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
std.range, std.random;
enum Pip {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
Jack, Queen, King, Ace}
enum Suit {Diamonds, Spades, Hearts, Clubs}
alias Card = Tuple!(Pip, Suit);
auto newDeck() pure nothrow @safe {
return cartesianProduct([EnumMembers!Pip], [EnumMembers!Suit]);
}
alias shuffleDeck = randomShuffle;
Card dealCard(ref Card[] deck) pure nothrow @safe @nogc {
immutable card = deck.back;
deck.popBack;
return card;
}
void show(in Card[] deck) @safe {
writefln("Deck:\n%(%s\n%)\n", deck);
}
void main() /*@safe*/ {
auto d = newDeck.array;
d.show;
d.shuffleDeck;
while (!d.empty)
d.dealCard.writeln;
}
import std.stdio, std.random, std.algorithm, std.string, std.range;
struct Card {
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;
enum nPack = suits.length * pips.length;
static bool rankAceTop = true;
int pip, suit;
string toString() pure const {
return format("%3s of %-7s", pips[pip], suits[suit])
.rightJustify(15);
}
@property int order() const nothrow {
immutable pipOrder = (!rankAceTop) ?
pip :
(pip ? pip - 1 : 12);
return pipOrder * suits.length + suit;
}
bool opEqual(in Card rhs) const pure nothrow {
return pip == rhs.pip && suit == rhs.suit;
}
int opCmp(in Card rhs) const nothrow {
return order - rhs.order;
}
}
final class Deck {
private Card[] cards;
this(in bool initShuffle = true, in int pack = 0) {
cards.length = 0;
foreach (immutable p; 0 .. pack)
foreach (immutable c; 0 .. Card.nPack)
cards ~= Card((c / Card.suits.length) %
Card.pips.length,
c % Card.suits.length);
if (initShuffle)
cards.randomShuffle;
}
@property size_t length() const pure nothrow {
return cards.length;
}
Deck add(in Card c) pure nothrow {
cards ~= c;
return this;
}
Deck deal(in int loc, Deck toDeck = null) pure nothrow {
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() {
this.writeln;
return this;
}
Deck shuffle() {
cards.randomShuffle;
return this;
}
Deck sortDeck() {
cards.sort!q{a > b};
return this;
}
override string toString() pure const {
return format("%(%(%s%)\n%)", cards.chunks(4));
}
}
void main() {
Deck[4] guests;
foreach (ref g; guests)
g = new Deck; // Empty deck.
auto host = new Deck(false, 1);
writeln("Host");
host.shuffle.showDeck;
while (host.length > 0)
foreach (ref g; guests)
if (host.length > 0)
host.dealTop(g);
foreach (immutable i, g; guests) {
writefln("Player #%d", i + 1);
g.sortDeck.showDeck;
}
}
You may also check:How to resolve the algorithm Loops/For step by step in the Plain English programming language
You may also check:How to resolve the algorithm Minkowski question-mark function step by step in the REXX programming language
You may also check:How to resolve the algorithm Object serialization step by step in the PHP programming language
You may also check:How to resolve the algorithm Animate a pendulum step by step in the Factor programming language
You may also check:How to resolve the algorithm File input/output step by step in the Beef programming language