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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
class Card {
private:
char *face;
char *suit;
int value; // value depends just on the face
public:
Card();
void setFace(char *f);
char *getFace();
void setSuit(char *s);
char *getSuit();
void setValue(int i);
int getValue();
// implement this friend function for comparing cards
friend int compare(Card, Card);
};
// default constructor
// pointers should be initialized to NULL, numbers to zero
Card::Card() {
// your code here.
face = NULL;
suit = NULL;
value = 0;
}
void Card::setFace(char *f) {
face = f;
// your code here.
}
char *Card::getFace() {
return face;
// your code here.
}
// your code here.
// Finish the other access functions.
void Card::setSuit(char *s) {
suit = s;
// your code here.
}
char *Card::getSuit() {
return suit;
// your code here.
}
void Card::setValue(int val) {
value = val;
// your code here.
}
int Card::getValue() {
return value;
// your code here.
}
// our friend function
int compare(Card cur, Card prev) {
// first compare face, i.e. value, if face is equal, compare suit
// suit is ranked alphabetically in ascending order, so clubs < diamonds < hearts < spades
// this is also the rule of Bridge
// return 1 if cur is bigger than prev, and -1 otherwise
if (cur.getValue == prev.getValue) {
if (cur.getSuit == "Spades") {
return 1;
}
else if (cur.getSuit() == "Hearts" && prev.getSuit() != "Spades") {
return 1;
}
else if (cur.getSuit() == "Diamonds" && prev.getSuit() != "Clubs") {
return 1;
}
else {
return -1;
}
}
else {
if (cur.getValue > prev.getValue)
return 1;
else
return -1;
}
}
void filldeck(Card *, char [][10], char [][10]);
void shuffle(Card *);
int deal(Card *, int);
const int decksize = 52;
// fill the deck with 52 cards sequentially with the original order in wFace and wSuit
// that is, the 13 cards should be Ace of Hearts, Deuce of Hearts, ..., King of Hearts
// the next 13 cards are Ace of Diamonds, ..., King of Diamonds, and so on
// you also need to set the value for each Card according to its face: Ace has a value of 1,
// Deuce 2, Three 3, ..., Jack 11, Queen 12, King 13
void filldeck(Card *wDeck, char wFace[][10], char wSuit[][10]) {
for (int i = 0; i < decksize; i++) {
// your code here.
for (int i = 0; i < decksize; i++) {
wDeck[i].setFace(wFace[i % 13]);
wDeck[i].setSuit(wSuit[i % 4]);
wDeck[i].setValue((i % 13) + 1);
}
}
}
// for each card of the deck, randomly shuffle it with another in the deck
void shuffle(Card *wDeck) {
for (int i = 0; i < decksize; i++) {
int j = rand() % decksize;
// swap cards i and j
// your code here.
Card tmp;
tmp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = tmp;
}
}
// deals one card of the i-th hand from wDeck, and check if it's bigger/smaller than
// the previous hand
int deal(Card *wDeck, int i) {
cout << "Hand " << i << ": ";
cout << wDeck[i - 1].getFace() << " of " << wDeck[i - 1].getSuit() << ". ";
int result = 0;
if (i > 1) {
result = compare(wDeck[i - 1], wDeck[i - 2]);
} else
cout << endl;
return result;
}
int main() {
Card deck[decksize];
char face[][10] =
{"Ace", "Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King"};
char suit[][10] =
{"Hearts", "Diamonds", "Clubs", "Spades"};
int seed;
cout << "Enter the seed for random number generation: ";
cin >> seed;
srand(seed); // this sets the seed of random number generation
filldeck(deck, face, suit);
shuffle(deck);
// game begins
int hand = 1, balance = 10;
char next = 'y';
cout << "### High Card Game Begins! ###\n";
for (; (next != 'n') && (hand <= decksize); hand++) {
// your code here.
if (balance > 0) {
cout << "Hand 1:" << deck[0].getFace() << " of " << deck[0].getSuit() << endl;
cout << "Your balance is " << balance << " dollars. ";
cout << "Your bet?";
cin >> next;
}
else if (balance == 0) {
break;
}
if (next == 'b' || next == 's') {
deal(deck, hand);
if (next == 'b') {
if (deal(deck, hand) == 1) {
cout << "BIGGER than the previous hand.WIN! ";
cout << "Your balance is " << balance + 10 << " dollars.";
cin >> next;
}
else if (deal(deck, hand) == -1) {
cout << "SMALLER than the previous hand.LOSE! ";
cout << "Your balance is " << balance - 10 << " dollars.";
cout << "Your bet?";
cin >> next;
}
}
else {
if (deal(deck, hand) == 1) {
cout << "BIGGER than the previous hand.LOSE! ";
cout << "Your balance is " << balance - 10 << " dollars.";
cin >> next;
}
else if (deal(deck, hand) == -1) {
cout << "SMALLER than the previous hand.WIN! ";
cout << "Your balance is " << balance + 10 << " dollars.";
cout << "Your bet?";
cin >> next;
}
}
}
if (next == 'n') {
if (hand < 4) {
cout << "You can't quit now! You bet?";
cin >> next;
}
else
break;
}
}
if (balance > 0)
cout << "Game ends, you have " << balance << " dollars. Congratulations!\n";
else
cout << "\nGame ends, you are broke! :(\n";
return 0;
}
|