BlackJack Project: Skip over unused values in 2D Array

Hello, right now I am working on creating a multiplayer blackjack game, and have made a bunch of progress since starting over, however the one problem I am running into is being able to skip over values in the 2d array used to store the current players cards.


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
#include <iostream>
#include <ctime>
#include <iomanip>
#include <windows.h>

using namespace std;
void gotoxy(int, int);
char suit(int);
int value(int);
char faceCard(int);
void updateTotal(int[], int[][8]);
void dealTwoCards(int[], int[][8]);
void dealCard(int[], int[][8], int[], int);  
void updateScreen(int[], int[][8], int[]);   


int main()
{
    int playerCurrentCards[11][8] = { -1 };//2d array that has 8 players with 11 cards, maximimum a player hand can have before busting (A,A,A,A,2,2,2,2,3,3,3) 
    int cards[52], picked[52] = { 0 }, cardIndex, totals[8] = { 0 }, numCardsDealtPerPlayer[8] = { 2 };
    char hitStand = ' ';
    srand(time(NULL));
    for (cardIndex = 0; cardIndex < 52; cardIndex++)
    {
        do
        {
            cards[cardIndex] = rand() % 52;
        } while (picked[cards[cardIndex]] != 0);
        picked[cards[cardIndex]] = 1;
        cout << cards[cardIndex] << endl;
    }
    dealTwoCards(cards, playerCurrentCards);
    int totalCardsDealt = 15;
    updateScreen(cards, playerCurrentCards, totals);
    // loop with a call to a function to deal cards to a player
    /*for (int j = 1; j < 8; j++) {
        gotoxy(0, 19);
        cout << "Would Player " << j << " like another card? (Please enter 'y' or 'n')";
        cin >> hitStand;
        while (hitStand != 'y' && hitStand != 'n') {
            cout << "That is not a valid choice. Please enter 'y' or 'n'). ";
            cin >> hitStand;
        }
        do {
            dealCard(cards, playerCurrentCards, numCardsDealtPerPlayer, j);
            updateScreen(cards, playerCurrentCards, numCardsDealtPerPlayer); 
            numCardsDealtPerPlayer[j]++;
            gotoxy(0,19);
            cout << "Would Player " << j << " like another card? (Please enter 'y' or 'n')";
            cin >> hitStand;
            while (hitStand != 'y' && hitStand != 'n') {
                cout << "That is not a valid choice. Please enter 'y' or 'n'). ";
                cin >> hitStand;
            }
        } while (hitStand == 'y');
        if (hitStand == 'n') {
            continue;
        }
        
    }*/
    // call a function to finish the dealer
    
    // call a function to determine win/lose/tie


    system("pause");
    return 0;
}
void gotoxy(int column, int row)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (INVALID_HANDLE_VALUE != hConsole)
    {
        COORD pos = { column, row };
        SetConsoleCursorPosition(hConsole, pos);
    }
    return;
}
char suit(int i)
{
    if (i == -1) {
        return ' ';
    }
    char c;
    c = char(i / 13 + 3);
    return c;
}
int value(int i)
{
    if (i == -1) {
        return 0;
    }
    int r;
    if (i % 13 == 0)
        r = 1;
    else if (i % 13 > 9)
        r = 10;
    else
        r = i % 13 + 1;
    return r;
}
char faceCard(int i)
{
    if (i == -1) {
        return 'e';
    }
    char c = ' ';
    if (i % 13 == 0)
        c = 'A';
    else if (i % 13 == 10)
        c = 'J';
    else if (i % 13 == 11)
        c = 'Q';
    else if (i % 13 == 12)
        c = 'K';

    return c;
}
void updateTotal(int total[], int playerCurrentCards[][8])
{
    for (int i = 0; i < 8; i++) {
        total[i] = 0;
    }
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 8; j++) {
            total[j] += value(playerCurrentCards[i][j]);
        }
    }
}
void dealTwoCards(int cards[], int playerCurrentCards[][8])
{
    int nextCard = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 8; j++) {
            if (i == 1 && j == 0) {
                continue;
            }
            playerCurrentCards[i][j] = cards[nextCard];
            nextCard++;
        }
    }
}

void dealCard(int cards[], int playerCurrentCards[][8], int numCardsDealtPerPlayer[], int currPlayer) {
    /*numCardsDealtPerPlayer[currPlayer]++;
    playerCurrentCards[numCardsDealtPerPlayer[currPlayer]][currPlayer] = cards[numCardsDealtPerPlayer[currPlayer]]; 
    return;*/
}

void updateScreen(int cards[], int playerCurrentCards[][8], int totals[]) { 
    int row = 0;
    system("cls");
    gotoxy(0, 0);
    cout << setw(8) << "Dealer" << setw(8) << "P1" << setw(8) << "P2" << setw(8) <<
        "P3" << setw(8) << "P4" << setw(8) << "P5" << setw(8) << "P6" << setw(8) << "P7" <<
        endl;
    row = 1;
    gotoxy(0, row);
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 8; j++) {
            if (i == 1 && j == 0) {
                cout << setw(8) << "??";
                continue;
            }
            if (playerCurrentCards[i][j] == -1) {
                continue;
            }
            char cardSuit = suit(playerCurrentCards[i][j]); // Diamond, Heart, Club, Spade
            int cardValue = value(playerCurrentCards[i][j]); // 1-10
            char face = faceCard(playerCurrentCards[i][j]); // A, J, Q, K
            if (playerCurrentCards[i][j] >= 0) {
                if (face != ' ') {
                    cout << setw(7) << face << cardSuit;
                }
                else {
                    cout << setw(7) << cardValue << cardSuit;
                }
            }
            if (j == 7) {
                cout << endl;
            }
        }
    }

    updateTotal(totals, playerCurrentCards); 
    gotoxy(0, 17);
    for (int i = 0; i < 8; i++) {
        cout << setw(8) << "Total";
    }
    cout << endl;
    for (int i = 0; i < 8; i++) {
        cout << setw(8) << totals[i];
    }
    

}


The output:

  Dealer      P1      P2      P3      P4      P5      P6      P7
     10♦      K♠      8♣      4♦      6♥      8♠      5♠     10♣
      ??     10♠      Q♥      6♣      7♥      2♣      6♠      7♠
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥
      A♥      A♥      A♥      A♥      A♥      A♥      A♥      A♥





   Total   Total   Total   Total   Total   Total   Total   Total
      20      29      27      19      22      19      20      26Press any key to continue . . .


I understand that the AHearts are printing because the 0th card of the deck is the AHeart, however I thought I would be able to get around this by initializing the 2d array with -1, and then checking for a -1 when computing the logic but as you can see this does not work apparently. I am also still getting a value of 1 added to the totals when it should skip the values when doing the additions.
Last edited on
I have fixed the part I was asking about, but will leave it up for future reference.
While you can initialize a 2d array like:
int playerCurrentCards[11][8] = { 0 };
and have every object = 0, you can not do the same with -1 like I had. All I had to do was nested loop through and set each [i][j] individually.
Yes - L19 will only initialise the first element to -1. The rest will be set to 0.
you can set a block to any value with a vector...
Topic archived. No new replies allowed.