tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
43
Task/Playing-cards/D/playing-cards-1.d
Normal file
43
Task/Playing-cards/D/playing-cards-1.d
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import std.stdio, std.random, std.string, std.array;
|
||||
|
||||
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() {
|
||||
auto card = deck.back;
|
||||
deck.popBack();
|
||||
return card;
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
111
Task/Playing-cards/D/playing-cards-2.d
Normal file
111
Task/Playing-cards/D/playing-cards-2.d
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
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 {
|
||||
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() {
|
||||
writeln(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck shuffle() {
|
||||
cards.randomShuffle();
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck sortDeck() {
|
||||
sort!q{a > b}(cards);
|
||||
return this;
|
||||
}
|
||||
|
||||
override string toString() const {
|
||||
return format("%(%(%s%)\n%)", std.range.chunks(cards, 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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue