Anonymous user
Playing cards: Difference between revisions
→{{header|C++}}: Getting rid of magic constants, useless comments
m (Removed trailing whitespace) |
(→{{header|C++}}: Getting rid of magic constants, useless comments) |
||
Line 498:
=={{header|C++}}==
<lang cpp>#include <deque>▼
▲#include <deque>
#include <algorithm>
#include <ostream>
Line 511 ⟶ 505:
namespace cards
{
enum pip_type { two, three, four, five, six, seven, eight, nine, ten,
jack, queen, king, ace, pip_count };
enum suite_type { hearts, spades, diamonds, clubs, suite_count };
enum { unique_count = pip_count * suite_count };
▲ card(suite_type s, pip_type p): value(s + 4*p) {}
▲ pip_type pip() { return pip_type(value/4); }
▲ suite_type suite() { return suite_type(value%4); }
unsigned char value;
{ "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"jack", "queen", "king", "ace" };
{
▲ std::ostream& operator<<(std::ostream& os, card::pip_type pip)
return os << pip_names[pip];
{ "hearts", "spades", "diamonds", "clubs" };
{
▲ std::ostream& operator<<(std::ostream& os, card::suite_type suite)
return os << suite_names[suite];
{
▲ std::ostream& operator<<(std::ostream& os, card c)
return os << c.pip() << " of " << c.suite();
deck()
{
for (int i = 0; i <
cards.push_back(card(i));
}
}
void shuffle() { std::random_shuffle(cards.begin(), cards.end()); }
card deal() { card c = cards.front(); cards.pop_front(); return c; }
typedef std::deque<card>::const_iterator const_iterator;
const_iterator begin() const { return cards.
const_iterator end() const { return cards.
std::deque<card> cards;
{
▲ inline std::ostream& operator<<(std::ostream& os, deck const& d)
std::copy(d.begin(), d.end(), std::ostream_iterator<card>(os, "\n"));
return os;
}
}
=={{header|C sharp|C#}}==
|