2-D Array of cards

Pages: 12
This is a program I just wrote where you can pick out 10 card random and then get a new deck. That should give you some idea how it can be done. Hope this help.
Ask if you have any question's.
Per

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
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

#define ESC 27

/* Prototypes */

void Randomize(void);
void ClearTable(void);

/* Globals */

const char szDeck[] = "AKQJ98765432111";

char szTable[100];
int  iCardTable = 0;

/*****************************************************************************/
void main() {

    ClearTable();
    printf("Pick a card by press a key or ESC to quit\r\n");

    while (getch() != ESC) {
        Randomize();
        if (iCardTable == 10) {
            iCardTable = 0;
            printf("New deck\r\n");
            ClearTable();
        }
    }
}
/*****************************************************************************/
void Randomize()
{

static int iPtr;
char cCard;
int  iOk = 0;

    do {
        cCard = (rand() % 52);
        if (cCard > 51) {
            cCard = 51;
        }

       /* Search table to see of same card on the table if is then redo */
       for (iPtr = 0; iPtr <= 51; iPtr++) {

           if (cCard == szTable[iPtr]) {
               break;
           }
       }
      /* Card is different from table */
    } while (iPtr <= 10);

    if (iPtr >= 10) {
      // Place card at table
       szTable[iCardTable++] = cCard;
    }

   // Card 0 - 12
    if (cCard <= 12) {
        printf("D of %c. Card #: %d\r\n", szDeck[cCard], iCardTable);
   // card 13 - 26
    } else if (cCard <= 26) {
        printf("S of %c. Card #: %d\r\n", szDeck[cCard - 13], iCardTable);
   // card 27 - 40
    } else if (cCard <= 40) {
        printf("H of %c. Card #: %d\r\n", szDeck[cCard - 27], iCardTable);
   // card 41 - 52
    } else {
        printf("C of %c. Card #: %d\r\n", szDeck[cCard - 40], iCardTable);
    }
}
/*****************************************************************************/
/* Clear deck to zero */
void ClearTable()
{

int iPtr;

   /* Setup a random seed number from computer time */

    srand(time(NULL));

    for (iPtr = 0; iPtr <= 52; iPtr++) {
        szTable[iPtr] = 0;
    }
}
/*****************************************************************************/
Last edited on
By using the above code you can easily change it into a blackjack game with a dealer and a player. Have fun.
@whitenite1.

You need made a new seed for rand() by using srand() every time you run it else rand will return the same number.
Sorry about that. You are right.
Here is a game of BlackJack I created from the use of rand(). Check it out and use block of it for your own purpose. Works fine. Just copy and paste it into your IDE and compile it. I only tried compiling under Borland C++ but I will tried it on VSC++. Good luck.
The code is a little long but should explain itself. If any questions then ask.
Per


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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

#define ESC 27
#define NEW_GAME 0
#define PLAYER_1 1
#define DEALER   2
#define DONE     3
#define FALSE 0
#define TRUE 1

/* Prototypes */

void RandomCards(void);
void ClearTable(void);
void NewGame(void);
void Player_1(void);
void Dealer(void);
void Done(void);
void Status(void);
void TestAce(int iTotal);

/* Globals */

char aTable[100];    // Cards on the table. 0 - 51
char szCardType[10]; // Diamond,Spades, Heart and Clovers
char szCardValue[20];// 2 to 10 and Jack, Queen, King and Ace
int  iCardValue;     // Card value of current card
int  iCardValueP1;   // Total card value of player 1
int  iCardValueD;    // Tolal card value of dealer
int  iCardTable;     // Number of card from the deck
int  iCard;          // Card picked 0 - 51
int  iStatus;        // Player, Dealer or Done

/*****************************************************************************/
void main() {

int iCh;

    iCardTable = 52;
    ClearTable();
    iStatus = NEW_GAME;

   // Do forever until ESC or 'p'
    for (;;) {
        printf("press 'p' to play BlackJack by press a key or ESC to quit\r\n");
        iCh = getch();
        if (iCh == 'p') {
            Status();
        } else if (iCh == ESC) {
            break;
        }
    };
}
/*****************************************************************************/
void RandomCards()
{

int iPtr;
int iEquel;

    do {
        iCard = (rand() % 52);
        iEquel = FALSE;

      /* Search table to see of same card on the table if is then redo */
       for (iPtr = 0; iPtr <= 51; iPtr++) {

           if (iCard == aTable[iPtr]) {
               iEquel = TRUE;
               break;
           }
       }
   /* jump out of loop if card is different from table */
    } while (iEquel == TRUE);

    // Place card at table
    aTable[iCard] = iCard;
    iCardTable++;
   // Card 0 to 12
    if (iCard <= 12) {
        strcpy(szCardType, "Diamonds.");
        iCardValue = iCard + 2;
   // Card 13 - 26
    } else if (iCard <= 25) {
        strcpy(szCardType, "Spades.");
        iCardValue = iCard - 11;
   // Card 27 - 40
    } else if (iCard <= 38) {
        strcpy(szCardType, "Hearts.");
        iCardValue = iCard - 24;
   // Card 41...
    } else {
        strcpy(szCardType, "Clovers.");
        iCardValue = iCard - 37;
    }

    if (iCardValue <= 10) {
        sprintf(szCardValue, "%d of ", iCardValue);
    } else if (iCardValue <= 11) {
        iCardValue = 10;
        strcpy(szCardValue, "Jack of ");
    } else if (iCardValue <= 12) {
        iCardValue = 10;
        strcpy(szCardValue, "Queen of ");
    } else if (iCardValue <= 13) {
        iCardValue = 10;
        strcpy(szCardValue, "King of ");
    } else {
        iCardValue = 1;
        strcpy(szCardValue, "Ace of ");
    }

    printf("%s%s\r\n", szCardValue, szCardType);
}

/*****************************************************************************/
void Status()
{
    switch(iStatus) {
        case NEW_GAME:
            NewGame();
            break;

        case PLAYER_1:
            Player_1();
            break;

        case DEALER:
            Dealer();
            break;

        case DONE:
            Done();
            break;

        default:{
            iCardTable = 52;
            iStatus = NEW_GAME;
        }
    }
}

/*****************************************************************************/
void NewGame(void)
{
    iCardValueP1 = 0;
    iCardValueD  = 0;
    printf("\r\nDealing Cards. Player:\r\n");
    RandomCards();
    TestAce(iCardValueP1);
    iCardValueP1 += iCardValue;
    RandomCards();
    TestAce(iCardValueP1);
    iCardValueP1 += iCardValue;
    printf("Player have: %d\r\n", iCardValueP1);
  /*
    printf("\r\nDealing Cards. Dealer:\r\n");
    RandomCards();
    TestAce(iCardValueD);
    iCardValueD += iCardValue;
    RandomCards();
    TestAce(iCardValueD);
    iCardValueD += iCardValue;
    printf("Dealer have: %d\r\n", iCardValueD);
  */
    iStatus = PLAYER_1;
    Status();

}

/*****************************************************************************/
void Player_1(void)
{
int iCh;
    printf("\r\nGet a card or stay: c = Get a card. s = stay. Player total: %d\r\n", iCardValueP1);

    do {
        iCh = getch();
        if (iCh == 'c') {
            RandomCards();
            ClearTable();
            TestAce(iCardValueP1);
            iCardValueP1 += iCardValue;
            printf("Player total: %d\r\n", iCardValueP1);
            if (iCardValueP1 > 21) {
                printf("**** BUSTED Player loss **** New Game\r\n\n", iCardValueP1);
                iStatus = NEW_GAME;
            }
        } else if (iCh == 's') {
            printf("Player stand at: %d\r\n\n", iCardValueP1);
            iStatus = DEALER;
        }
    } while ((iCh != 'c') && (iCh != 's'));

    Status();
}

/*****************************************************************************/
void Dealer(void)
{

   // If card > than 17 dealer stand
    if (iCardValueD > 17) {
        printf("Dealer stand at: %d\r\n", iCardValueD);
        iStatus = DONE;
    } else {
        RandomCards();
        ClearTable();
        TestAce(iCardValueP1);
        iCardValueD += iCardValue;
        printf("Dealer total: %d\r\n\n", iCardValueD);
        if (iCardValueD > 21) {
            printf("**** DEALER loss **** New Game\r\n\n", iCardValueP1);
            iStatus = NEW_GAME;
        }
    }
    Status();
}

/*****************************************************************************/
void Done(void)
{
    if (iCardValueP1 >= iCardValueD) {

        printf("\r\n\n\n*** PLAYER WON *** Player %d Dealer %d\r\n\n", iCardValueP1, iCardValueD);
    } else {

        printf("\r\n\n\n*** DEALER WON *** Player %d Dealer %d\r\n\n", iCardValueP1, iCardValueD);
    }
    iStatus = NEW_GAME;
}

/*****************************************************************************/
void TestAce(int iTotal)
{

   // Test for an Ace
    if (iCardValue == 1) {
        if ((iTotal + 11) <= 21) {
            iCardValue = 11;
        }
    }
}

/*****************************************************************************/
/* Clear deck to -1 */
void ClearTable()
{

int iPtr;

    if (iCardTable == 52) {
        iCardTable = 0;
        iCard = 0;
        printf("\r\nNew deck\r\n");

       /* Setup a random seed number from computer time */
        srand(time(NULL));
       // Fill table with -1. Can not use zero because zero is a valid number
        for (iPtr = 0; iPtr < 60; iPtr++) {
            aTable[iPtr] = -1;
        }
    }
}

/*****************************************************************************/
Last edited on
Tested and recompiled above code under Microsoft Visual C++ Express (free C++ compiler).
A few changes need to be done to get it to work.
Add
#include <stdlib.h>
#define FALSE 0
#define TRUE 1

That's it.
I made the changes in above code.
You will get a bunch of warnings but don't worry about it. You can change it to what the warnings say but it works fine with the warnings.

Good luck.
Last edited on
Topic archived. No new replies allowed.
Pages: 12