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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
typedef int Suit, Rank;
const Suit hearts = 0, clubs = 1, diamonds = 2, spades = 3;
const Rank two = 0, three = 1, four = 2, five = 3,
six = 4, seven = 5, eight = 6, nine = 7,
ten = 8, jack = 9, queen = 10, king = 11,
ace = 12;
class Card
{
Suit suit;
Rank rank;
public:
Card(Suit s, Rank r) : suit(s), rank(r) { }
bool operator==(Card c) { return rank == c.rank; }
bool operator< (Card c) { return rank < c.rank; }
};
class CardStack
{
protected:
std::vector<Card> cards;
public:
Card Play()
{
Card c( *cards.begin() );
cards.erase(cards.begin());
return c;
}
std::vector<Card> Play(int i)
{
std::vector<Card> c;
while (i-- && cards.size())
c.push_back( Play() );
return c;
}
void Draw(Card c)
{
cards.push_back(c);
}
void Draw(std::vector<Card> c)
{
for (std::vector<Card>::iterator it = c.begin(); it != c.end(); ++it)
Draw(*it);
}
int CardsLeft() { return cards.size(); }
};
class Player : public CardStack { };
class Deck : public CardStack
{
public:
Deck()
{
for (Rank r = two; r <= ace; ++r)
for (Suit s = hearts; s <= spades; ++s)
cards.push_back(Card(s,r));
std::random_shuffle(cards.begin(), cards.end());
}
void Deal(Player& p1, Player& p2)
{
for(bool i = true; cards.size() ; i = !i)
{
if (i) p1.Draw( Play() );
else p2.Draw( Play() );
}
}
};
class Table
{
CardStack theStack;
Deck deck;
Player p1, p2;
public:
int iterations;
Table() : iterations(0) { deck.Deal(p1, p2); }
int Play()
{
int result = 0;
while(result == 0)
result = Attack();
return result;
}
int Attack() // return 0 (no winner yet), 1 (p1 wins), 2 (p2 wins), 3 (tie)
{
if (++iterations > 15000) return 3;
std::cout << std::endl << "p1: " << p1.CardsLeft() << "\t p2: " << p2.CardsLeft();
if (p1.CardsLeft() < 1) return 2;
if (p2.CardsLeft() < 1) return 1;
Card left( p1.Play() ), right( p2.Play() );
theStack.Draw(left);
theStack.Draw(right);
if (left == right)
{
std::cout << " \t WAR!";
if (p1.CardsLeft() < 3) return 2;
if (p2.CardsLeft() < 3) return 1;
theStack.Draw( p1.Play(3) );
theStack.Draw( p2.Play(3) );
return Attack();
}
else if (left < right)
{
std::cout << " \t p1 wins";
p1.Draw( theStack.Play( theStack.CardsLeft() ) );
}
else
{
std::cout << " \t p2 wins";
p2.Draw( theStack.Play( theStack.CardsLeft() ) );
}
return 0;
}
};
int main()
{
srand(time(NULL));
Table t;
t.Play();
}
|