1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <deque>
#include <iostream>
struct Letter
{
char letter;
int points;
Letter( char l, int p ) : letter(l), points(p) {}
};
typedef std::deque<Letter> Letters;
class DrawBag
{
Letters m_set;
public:
DrawBag()
{
Reset();
}
void Reset()
{
m_set.clear();
AddLetter('E', 1, 12); AddLetter('A', 1, 9 );
AddLetter('I', 1, 9 ); AddLetter('O', 1, 8 );
AddLetter('N', 1, 6 ); AddLetter('R', 1, 6 );
AddLetter('T', 1, 6 ); AddLetter('L', 1, 4 );
AddLetter('S', 1, 4 ); AddLetter('U', 1, 4 );
AddLetter('D', 2, 4 ); AddLetter('G', 2, 3 );
AddLetter('B', 3, 2 ); AddLetter('C', 3, 2 );
AddLetter('M', 3, 2 ); AddLetter('P', 3, 2 );
AddLetter('F', 4, 2 ); AddLetter('H', 4, 2 );
AddLetter('V', 4, 2 ); AddLetter('W', 4, 2 );
AddLetter('Y', 4, 2 ); AddLetter('K', 5, 1 );
AddLetter('J', 8, 1 ); AddLetter('X', 8, 1 );
AddLetter('Q',10, 1 ); AddLetter('Z',10, 1 );
std::random_shuffle( m_set.begin(), m_set.end() );
}
Letter Draw()
{
Letter temp = m_set.front();
m_set.pop_front();
return temp;
}
Letters Draw(int number)
{
Letters temp;
for (int i = 0; i < number; ++i)
temp.push_back( Draw() );
return temp;
}
private:
void AddLetter(char letter, char points, char frequency)
{
for (int i = 0; i < frequency; ++i)
m_set.push_back( Letter( letter, points) );
}
};
int main()
{
srand(time(0));
DrawBag d;
Letters hand1 = d.Draw(7); // Draw 7 letters when starting the game
Letters hand2 = d.Draw(7); // Draw 7 letters when starting the game
// Two different ways of iterating:
std::cout << "Player 1's hand:\n";
for (Letters::iterator it = hand1.begin(); it != hand1.end(); ++it)
std::cout << it->letter << ':' << it->points << std::endl;
std::cout << "Player 2's hand:\n";
for (int i = 0; i < hand2.size(); ++i)
std::cout << hand2[i].letter << ':' << hand2[i].points << std::endl;
}
|
Player 1's hand:
O:1
Z:10
S:1
U:1
O:1
R:1
A:1
Player 2's hand:
T:1
G:2
D:2
Q:10
U:1
N:1
Y:4 |