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
|
//CARD.cpp
#include "Card.h"
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
const char *const SUIT_NAMES[] = { "Spades", "Hearts", "Clubs", "Diamonds" };
const int SUIT_SIZE = 4;
const char *const RANK_NAMES[] = { "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King", "Ace" };
const int RANK_SIZE = 13;
////////////////////////////////////////////////////////////////////////////////
void Suit_print(Suit suit) {
cout << SUIT_NAMES[suit];
}
Suit Suit_next(Suit suit) {
switch (suit) {
case SPADES: return CLUBS;
case HEARTS: return DIAMONDS;
case CLUBS: return SPADES;
case DIAMONDS: return HEARTS;
default: assert(0); return SPADES; // should never get here
}
}
void Card_init(Card *card_ptr, Rank rank, Suit suit) {
card_ptr->rank = rank;
card_ptr->suit = suit;
}
void Card_print(const Card *card_ptr) {
cout << RANK_NAMES[card_ptr->rank] << " of " << SUIT_NAMES[card_ptr->suit];
}
Suit Card_get_suit(const Card *card_ptr, Suit trump) {
if (Card_is_left_bower(card_ptr, trump)) return trump;
return card_ptr->suit;
}
bool Card_is_face(const Card *card_ptr) {
return
card_ptr->rank == JACK ||
card_ptr->rank == QUEEN ||
card_ptr->rank == KING ||
card_ptr->rank == ACE;
}
bool Card_is_right_bower(const Card *card_ptr, Suit trump) {
return card_ptr->rank == JACK && card_ptr->suit == trump; // JACK of trump suit
}
bool Card_is_left_bower(const Card *card_ptr, Suit trump) {
return card_ptr->rank == JACK &&
card_ptr->suit == Suit_next(trump); // JACK of trump's color
}
bool Card_is_trump(const Card *card_ptr, Suit trump) {
return card_ptr->suit == trump ||
Card_is_left_bower(card_ptr, trump);
}
int Card_compare(const Card *a, const Card *b) {
// sanity check for NULL pointers
assert(a);
assert(b);
// To simplify debugging and ensure that every correct solution will get the
// same output, we’ve added an additional rule here. In addition to being
// ordered by rank, cards are also ordered by suit.
return (a->suit + a->rank*SUIT_SIZE)
- (b->suit + b->rank*SUIT_SIZE);
}
template <typename T>
static T string2enum(const char *s, const char *const const_names[], const int size){ //REMEMBER TO MAKE RME FOR THIS FUNCTION!!!
for (int i = 0; i < size; ++i){
if (strcmp(s, const_names[i]) == 0){
return static_cast<T>(i);
}
}
assert(0);//won't run
exit(1);
}
void Card_init(Card *card_ptr, const char* rank, const char* suit) {
card_ptr->rank = string2enum<Rank>(rank, RANK_NAMES, RANK_SIZE);
card_ptr->suit = string2enum<Suit>(suit, SUIT_NAMES, SUIT_SIZE);
}
|