Playing cards: Difference between revisions
Added Perl 6.
m (→BASIC: added "works with"; some minor clean-up) |
Underscore (talk | contribs) (Added Perl 6.) |
||
Line 1,360:
$deck->print_cards;</lang>
This creates a new deck, shuffles it, removes the top card, prints out that card's name in all caps, and then prints the rest of the deck.
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
<lang perl6>enum Pip <Two Three Four Five Six Seven Eight Nine Ten
Jack King Queen Ace>;
enum Suit <Diamonds Clubs Hearts Spades>;
class Card {
has Pip $.pip;
has Suit $.suit;
}
multi prefix:<~> (Card $c) { $c.pip.name ~ " of " ~ $c.suit.name }
class Deck {
has Card @.cards;
submethod BUILD {
@!cards = pick *, map
{ Card.new(pip => $^p, suit => $^s) },
(Pip.pick(*) X Suit.pick(*));
}
method shuffle { @!cards .= pick * }
method deal { shift @!cards }
}
multi prefix:<~> (Deck $d) { join ', ', map ~*, $d.cards }</lang>
Some examples of use:
<lang perl6>my Deck $d = Deck.new;
say "Deck: $d";
my $top = $d.deal;
say "Top card: $top";
$d.shuffle;
say "Deck, re-shuffled: ", $d;</lang>
=={{header|Python}}==
|