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
|
#include <iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
char* Suits[4] = {"s", "c", "d", "h"};//references for enum types
char* Ranks[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
class CARD{
public:
enum class SUIT{ Spades, Clubs, Diamonds, Hearts}; //enumerate card suits and ranks so they can be randomly assigned
enum class RANK{ Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};
private:
SUIT Card_Suit; //suit and rank of card
RANK Card_Rank;
public:
CARD(){} //default constructor
CARD(int Card_Number) //constructor which takes a card number as an argument and assigns the corresponding suit and rank
{
Card_Suit = (SUIT) (Card_Number / 13);
Card_Rank = (RANK) ((Card_Number % 13) + 1);
}
void Print_Card()
{
cout << Ranks[((int) Card_Rank) - 1] << Suits[(int) Card_Suit];
}
};
//create information class which stores player's chip count and hole cards
class information{
public:
int chips, position;
CARD hole_cards[2];
};
class DECK{
public:
int Cards_Remaining;//indicates how many cards are left in the deck
bool Cards_Dealt[52];//includes information about cards already dealt
DECK();
void Reshuffle(bool *Cards_Dealt);
CARD Deal_Card();
int RNG(int n); //random number generator
int Select_Available_Card(int Card);
};
//default constructor
DECK::DECK(){
Reshuffle(Cards_Dealt);
int Cards_Remaining = 52;
}
//reshuffles the deck setting all elements in cards_dealt array to false
void DECK::Reshuffle(bool *Cards_Dealt){
for(int i = 0; i < 52; i++){
Cards_Dealt[i] = false;
}
}
CARD DECK::Deal_Card(){
int Card_Number;
//Card_Number = Select_Available_Card(RNG(Cards_Remaining--));
Card_Number = Select_Available_Card(RNG(52));
return CARD(Card_Number);
}
//genenrates a random number between 0 and (n-1), inclusive
int DECK::RNG(int n) {
return rand() % n;
}
//selects the next available card preventing a card from being drawn twice
int DECK::Select_Available_Card(int Card){
int Card_Number = -1;
Card++;
while(Card-->0){
Card_Number++; //go to next card as we have counted one card off
while(Cards_Dealt[Card_Number]){
Card_Number++; //if current card has already been drawn skip to next card
}
}
Cards_Dealt[Card_Number] = true; //set card selected to be shown as drawn
return Card_Number;
}
int main(){
srand(time(NULL));
DECK Poker;
CARD Test = Poker.Deal_Card();
Test.Print_Card();
system("PAUSE");
return 0;
}
|