Search code examples
carraysoutputblackjack

Missing output from C Blackjack game's int array


So I am trying to write a Blackjack game in C just for the fun of it (something I have never tried before). I have gotten past the first few steps such as how to set up the deck and how to give the cards to the dealer and player. However, the displayed output is not showing what I want it too (showing the dealer only has 1 card when it should be showing 2). Here is a screenshot:

Yes, this was run on my phone, but I have the same results on my computer too.

I did run this on my phone (easier to get a screenshot), but I am having the same results on my computer using Code::Blocks. Anyways, I have two functions to handle setting up the deck and to display the current cards. The dealing of the cards I am handling in the main() function using loops. I know that the makeDeck() function is working correctly, so I am assuming that my error is somewhere else. Have a look at my code and let me know what's going on (also, any suggestions for improvement would be appreciated):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*Function Prototypes*/
int* makeDeck();
    /*Usage - returns an int* to be used for the deck */

void showHands(int[], int[]);
    /*Usage - pass dealerHand[], then playerHand[] */

int main() {
        srand(time(0));     //Make a new rand() seed value
    int  x, count = 0, choice = 1;
    int* cards        = makeDeck();
    int  dealerCards[12];       //cards in hand will never exceed 11
    int  playerCards[12];       //4 A's, 4 2's, 3 3's

    /*Loop to run the game. One iteration per hand */
    while(choice != 0) {
        for(x = 0; x < 12; x++) {
            dealerCards[x] = 0;
            playerCards[x] = 0;
        }

        //Deal 2 cards to dealer and player
        for(x = 0; x < 2; x++) {
            dealerCards[x] = cards[count];
            count++;
        }
        for(x = 0; x < 2; x++) {
            playerCards[x] = cards[count];
            count++;
        }

        showHands(dealerCards, playerCards);

                /*DEBUGGING */
        printf("\nEnter 0 to exit loop: ");
        scanf("%i", &choice);
    }
    return 0;
}

//Declare placeholder variable "bunchOfCards" globally
int bunchOfCards[52];

int* makeDeck(){
    int* deck = bunchOfCards;
    int  x    = 0,
         y    = 0,
         card = 0;
    for(x = 0; x < 52; x++) {           //set all cards to 0
        deck[x] = 0;
    }
    for(x = 0; x < 4; x++) {                //set up deck
        for(y = 1; y < 14; y++) {
             card = (rand() % 52);

             //check if deck position is already used
             while(deck[card] != 0) {
                    card = (rand() % 52);
             }
             deck[card] = y;
        }
    }
    /* DEBUGGING
    for(x = 0; x < 52; x++) {
        printf("%i\t", deck[x]);
    } */
    return deck;
}

void showHands(int* dealer, int* player) {
    int x; char card[3] = { '\0', '\0', '\0' };
    puts("The hands are: \n\nDealer:");

    //Display dealer cards
    for(x = 0; x < 12; x++) {
        if(dealer[x] != 0) {
            if((dealer[x] < 10) && (dealer[x] != 1)) {
                card[0] = (char)(((int)'0') + dealer[x]);
                card[1] = '\0';
            } else if(dealer[x] == 1) {
                card[0] = 'A';
                card[1] = '\0';
            } else if(dealer[x] == 10) {
                card[0] = '1';
                card[1] = '0';
            } else if(dealer[x] == 11) {
                card[0] = 'J';
                card[1] = '\0';
            } else if(dealer[x] == 12) {
                card[0] = 'Q';
                card[1] = '\0';
            } else if(dealer[x] == 13) {
                card[0] = 'K';
                card[1] = '\0';
            }
            printf("\t%s", card);
        }

        //Display player cards
        puts("\nPlayer: ");
        for(x = 0; x < 12; x++) {
            if(player[x] != 0) {
                if((player[x] < 10) && (player[x] != 1)) {
                    card[0] = (char)(((int)'0') + player[x]);
                    card[1] = '\0';
                } else if(player[x] == 1) {
                    card[0] = 'A';
                    card[1] = '\0';
                } else if(player[x] == 10) {
                    card[0] = '1';
                    card[1] = '0';
                } else if(player[x] == 11) {
                    card[0] = 'J';
                    card[1] = '\0';
                } else if(player[x] == 12) {
                    card[0] = 'Q';
                    card[1] = '\0';
                } else if(player[x] == 13) {
                    card[0] = 'K';
                    card[1] = '\0';
                }
                printf("\t%s", card);
            }
        }
    }
}

Solution

  • Your for-loops appear to include each other.

    for(x = 0; x < 12; x++) {
      if(dealer[x] != 0) {
        ...
      }
      puts("\nPlayer: ");
      for(x = 0; x < 12; x++) {
        ...
      }
    }
    

    The first for-loop starts with x = 0, the second for loop goes up to x = 12, then the first for-loop stops before executing a second time because it reached the terminating condition.