Jump to content

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>
{{works with|g++|4.1.2 20061115 (prerelease) (SUSE Linux)}}
 
Since all the functions are quite simple, they are all defined inline
<lang cpp>#ifndef CARDS_H_INC
#define CARDS_H_INC
 
#include <deque>
#include <algorithm>
#include <ostream>
Line 511 ⟶ 505:
namespace cards
{
class card
{
public:
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 + 4suite_count * p) {}
// construct a card of a given suite and pip
card(suite_type s, pip_type p): value(s + 4*p) {}
 
// construct aexplicit card(unsigned directlychar fromv its= 0): value(v) {}
card(unsigned char v = 0): value(v) {}
 
pip_type pip() { return pip_type(value /4 suite_count); }
// return the pip of the card
pip_type pip() { return pip_type(value/4); }
 
suite_type suite() { return suite_type(value %4 suite_count); }
// return the suit of the card
suite_type suite() { return suite_type(value%4); }
 
private:
// there are only 52 cards, therefore unsigned char suffices
unsigned char value;
};
 
const char const* const pip_names[] =
{ "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"jack", "queen", "king", "ace" };
 
std::ostream& operator<<(std::ostream& os, card::pip_type pip)
// output a pip
{
std::ostream& operator<<(std::ostream& os, card::pip_type pip)
{
return os << pip_names[pip];
};
 
const char const* const suite_names[] =
{ "hearts", "spades", "diamonds", "clubs" };
 
std::ostream& operator<<(std::ostream& os, card::suite_type suite)
// output a suite
{
std::ostream& operator<<(std::ostream& os, card::suite_type suite)
{
return os << suite_names[suite];
}
 
std::ostream& operator<<(std::ostream& os, card c)
// output a card
{
std::ostream& operator<<(std::ostream& os, card c)
{
return os << c.pip() << " of " << c.suite();
}
 
class deck
{
public:
// default constructor: construct a default-ordered deck
deck()
{
for (int i = 0; i < 52card::unique_count; ++i) {
cards.push_back(card(i));
}
}
 
// shuffle the deck
void shuffle() { std::random_shuffle(cards.begin(), cards.end()); }
 
// deal a card from the top
card deal() { card c = cards.front(); cards.pop_front(); return c; }
 
// iterators (only reading access is allowed)
typedef std::deque<card>::const_iterator const_iterator;
const_iterator begin() const { return cards.begincbegin(); }
const_iterator end() const { return cards.endcend(); }
private:
// the cards
std::deque<card> cards;
};
 
inline std::ostream& operator<<(std::ostream& os, deck const deck& d)
// output the deck
{
inline std::ostream& operator<<(std::ostream& os, deck const& d)
{
std::copy(d.begin(), d.end(), std::ostream_iterator<card>(os, "\n"));
return os;
}
}
}
 
#endif</lang>
 
=={{header|C sharp|C#}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.