Creating a Poker Dealer, problem with DECK class

Hey,

I'll just paste all my code so far below. My question is about the bold code in lines 70 and 71. The code executes fine using line 71 (and 70 is commented out), but does not vice versa (the command window crashes at runtime).

I am pretty sure that the line 70 is legal and should do the exact same as line 71 (for now). What is going on?

Thanks
Andy


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;
}
Last edited on
Also, if you have time to read the code. Any suggestions are welcome
remove the int from line 58. Might not solve your problem, but that's a problem on it's own.

What happens at line 88 when card number is 51?
Last edited on
If card number is 51, then none of the cards have been drawn. So line 88 will not be executed.

Also, THANK YOU!! That has solved my problem. Thank you so much for trawling through my code, I know how annoying it can be.

I kept checking through it, but didn't realise that at all

Thanks again
Last edited on
Great :)

Here's something to think about, random_shuffle:
http://www.cplusplus.com/reference/algorithm/random_shuffle/

Never used it myself, I guess it needs #include <algorithm>

So you can make an array with values 0-51, and then shuffle them.

Using a stack or queue might be easier than an array all together as well. Not sure how that will go. The main thing is that "poping" values from these containers removes that value. You don't have to keep track of an array index, just pop from the deck to get your next card.
Last edited on
Topic archived. No new replies allowed.