card game breaking

Hi all,

Same as most pritty new to c++ making a little text based card game breaks when trying to output the cards in a hand.

It compiles fine but when I run the program i get a Developped with Dev C++ is not responding.
only happends when player1.displayHand(); isn't commented. due to my newness and difficulty with pointers i'll go out on a limb and say that its something to do with the fact that i'm trying to access a member function of a pointer. but i'm not doing anything that doesn't already work else where!

any ideas?

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
#include <cstdlib>
#include <iostream>
#include <string>

/* a e i o u */

using namespace std;


string eSuits[4] = {"Dimonds","Hearts","Spades","clubs"};
string eCardinals[13] = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

/*
A single card 
*/
class CCard
{
private:
      // Variables
      int iCardinal; // Ace = 1; Jack = 11; Queen = 12; King = 13;
      int iSuit; // Dimonds = 1; Spades = 2; Hearts = 3; Clubs = 4;
public:    
      //functions
      CCard(){}
      CCard( int cardinal, int suit )
      {
             this->iCardinal = (int)cardinal;
             this->iSuit = (int)suit;
      }
      
      int getCardinal() { return this->iCardinal; }
      int getSuit() { return this->iSuit; }
      
      string getCardinalName() { return eCardinals[this->iCardinal]; }
      string getSuitName() { return eSuits[this->iSuit]; }
      
      string nameOf( void )
      {
             string of = " of ";
             string name = "";
             name = eCardinals[this->iCardinal];
             name += of;
             name += eSuits[this->iSuit];
             return name;
      }
};
/*
A Pack of cards is a group of 52 cards
we also trace which cards we've already handed out
*/
class CPack
{
      private:
      CCard *cards[52];
      int distributed[52];
      
      int _find(int value, int* array, int size)
      {
          int i;
          for (i=0; i<size; i++)
          {
              if( array[i] == value )
              {
                  return i;
              }
          }
          return -1;
      }
      
      public:
      CPack()
      {
             int i = 0;
             for( int s = 0 ; s < 4 ; s++ ) // for each suit
             {
                  for( int c = 0 ; c < 13 ; c++ ) // for each value in suit
                  {
                       this->cards[i] = new CCard(c,s);
                       this->distributed[i] = -1;
                       i++;
                  }
             }
      }
      
      void shuffle()
      {
           // Declare
           CCard *newCards[52];
           int used[52];
           int use = 0;
           // initialize
           for( int i = 0 ; i < 52 ; i++ )
           {
                newCards[i] = 0;
                used[i] = -1;
           }
           srand ( time(NULL) );
           // randomly add unused cards into newCards
           for( int i = 0 ; i < 52 ; i++ )
           {
                use = rand() % 52;
                while( this->_find( use , used, 52 ) != -1 )
                {
                       use = rand() % 52;
                }
                newCards[i] = this->cards[use];
                used[i] = use;
           }
           for( int i = 0 ; i < 52 ; i++ )
           {
                this->cards[i] = newCards[i];
           }
      }
      
      void order()
      {
      }
      
      CCard * randomCard()
      {
            srand ( time(NULL) );
            // randomly return an unused card
            int use = rand() % 52;
            while( this->_find( use , this->distributed, 52 ) != -1 )
            {
                 use = rand() % 52;
            }
            distributed[use] = use;
            return this->cards[use];
      }
};
/*
A hand is any group of cards
exp: a player's hand, the current hand in play, the pickup pile... 
*/
class CHand
{
private:
      // Variables
      CCard *cards[]; // an array of pointers to this hands cards
      int size; // number of cards in hand
public:
      // functions
      CHand(){ // an empty array
              this->cards[0] = new CCard;
              this->size = 0;
      }
      CHand( CCard *newCards[] )
      {
             for( int i = 0 ; i < sizeof newCards ; i++ )
             {
                  this->cards[i] = newCards[i];
             }
             this->size = sizeof this->cards;
      }
      /* add a card too this array */
      bool addCard( CCard *card ) // add a card from the pack to this hand
      {
           int size = this->size;
           int newSize = this->size+1;
           // make a new temporary array one bigger than the cards currently in the hand
            CCard *newHand[newSize];
            // add each card in the hand to the new hand
            for( int i = 0 ; i < this->size ; i++ )
            {
                 newHand[i] = this->cards[i];
            }
            // in the last position add the new card
            newHand[ newSize ] = card;
            // overwrite the old hand with the new hand
           // this->card = newHand;
            for( int i = 0 ; i <= newSize ; i++ )
            {
                 this->cards[i] = newHand[i];
            }
            this->size = newSize;
            
            return true;
      }
      bool removeCard( CCard *card ) // remove a card from this hand
      { return true; }
      bool moveCard( CCard *card, CHand *hand ) // move card to another hand
      { return true; }
      bool orderCards( int OrderType, int OrderDirection ) // orders the cards in this hand based on the type of order
      { return true; }
      void displayHand()
      {
           cout << "displaying this hand:" << endl;
           for( int i = 0 ; i < this->size ; i++ )
           {
                CCard temp = *this->cards[i];
                cout << i << temp.nameOf() << endl;
           }
      }
      
};

/*
class game()
{
 
};

*/

int main( int argc, char *argv[] )
{
    // test data
    CPack *cpGame = new CPack();
    cout << "First shuffle:" << endl;
    cpGame->shuffle();
    CHand player1;
    cout << "we've made player 1" << endl;
    for( int i = 0 ; i < 10 ; i++ )
    {
        CCard *temp = cpGame->randomCard();
        player1.addCard(temp);
    }
    cout << "We just gave him 10 cards" << endl;
    player1.displayHand();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
1
2
3
4
5
6
class CHand
{
private:
      // Variables
      CCard *cards[]; // Give this array a size and if needed use realloc to resize
      int size; // number of cards in hand 


if you give the ccard array a size you can just simply (or use a vector)

1
2
3
4
5
bool addCard( CCard *card ) // add a card from the pack to this hand
      {
          cards[size++] = card;
          return true;
      }
Last edited on
Thanks for the tip, looking up realloc now :) will that solve the output bug as well as being a good programming tip?

EDIT:

Ok, so I've been working on the project a little, tracing the bug. I've found that the problem is on lines 41 and 43 of the above code. the problem seems to come from accessing the classes properties. As when I output The data in either iCardinal or iSuit I get strange ASCII characters.

like they are not being initialized, set and/or out of the memory allocated to the program!
And way to fix/ find more info on the probleme I'm having?
Last edited on
Topic archived. No new replies allowed.