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
|
class CardHand;
class PlayCard {
private:
int index; //card index 1-52, 0 is NULL
int val; //card value 2-14, 0 is NULL
int suit; //card suit 1-4, 0 is NULL
public:
void assign(int); //this doesn't seem to work when called by
void reset() {index=0;val=0;suit=0;};
int getindex() {return index; };
int getval() {return val; };
char getsuit() {return (suit + 2); }; //outputs suit symbol
char getface();
friend class CardHand;
};
void PlayCard::assign(int a) {
//assign index
index = a;
//card values, 2-10, J=11, Q=12, K=13, A=14
val = 2 + (a-1) / 4;
//suit values, 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades
suit = (a-1) % 4 + 1;
}
char PlayCard::getface() {
if (val < 10) return (val + 48); //outputs char of number
else if (val == 10) return 'T'; //output char of face from 10(T) to A
else if (val == 11) return 'J';
else if (val == 12) return 'Q';
else if (val == 13) return 'K';
else return 'A';
}
class CardHand {
private:
PlayCard cards[5]; //5 card hand max
public:
void rcvcard(PlayCard);
PlayCard getcard(int n) {return cards[n]; };
void discardall() {for(int n=0;n<=4;n++) cards[n].reset(); }; //clear card values
bool blackjack() {if((count()==2)&&(sum()==21)) return true; else return false; };
bool bust() {if(sum()>21) return true; else return false; };
int sum();
int count();
};
void CardHand::rcvcard(PlayCard a) {
int n=count(); //get next card position
cards[n] = a; //assign index to next card
if((cards[n].val>10)&&(cards[n].val<14))
cards[n].val=10; //rewrite card value for face cards
else if(cards[n].val==14)
cards[n].val=11; //rewrite card value for ace
}
int CardHand::sum() {
int a=0;
for(int n=0;n<count();n++)
a+=cards[n].val;
if(a>21) { //if hand total is greater than 21...
for(int n=0;n<=count()-1;n++) {
if(cards[n].val==11) { //search for Aces with a value of 11
cards[n].val=1; //assign Ace value as 1
return sum(); //get new sum
}
}
}
return a;
}
int CardHand::count() { //count number of cards in hand
int a=0;
for(int n=0;n<5;n++)
if(cards[n].val>0) a++;
return a;
}
class CardPlayer {
private:
long score; //player money
long bet; //current bet
public:
CardPlayer() {score=100; };
CardHand hand;
long showscore() {return score; }; //display the score
void placebet(long amt) {bet=amt; score-=bet; }; //remove bet from money
void win() {score+=bet*2; }; //place bet and winnings in money
};
class CardDeck {
private:
PlayCard cards[52],*top,*bottom;
int position; //current deck position
public:
CardDeck();
int retpos() {return position; };
PlayCard deal() {return cards[position++]; }; //return index at position, then increment position
};
CardDeck::CardDeck() {
for(int n=0;n<52;n++)
cards[n].assign(n+1);
top=&cards[0];
bottom=&cards[52];
position=0; //when dealt it will return 0 then increment from there with ++
}
|