I fixed the code, which was actually a really stupid easy fix. I made the deal variable global
Here is my updated code. The next thing I'm doing is adding an if-then statement to see if the playerHandTotal > 21. I'm not sure where I should plug in the if-then? I'm thinking at the beginning of the playerChoice function? Before it runs the switch?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DECKSIZE 52
int deck[DECKSIZE];
int card;
int playerHand;
int dealerHand;
int playerHandTotal;
int dealerHandTotal;
int deal = 51;
int shuffle ();
int dealCardDealer ();
int dealCardPlayer ();
int playerChoice ();
int main ()
{
shuffle ();
dealCardDealer ();
printf("Dealer has %d", dealerHandTotal);
dealCardPlayer ();
dealCardPlayer ();
printf("Player has %d", playerHandTotal);
playerChoice ();
}
int shuffle ()
{
int i;
int randomNumber;
srand(time(NULL));
int numberPrinted = 0;
for(i = 0; i < DECKSIZE; i++)
{
randomNumber = rand() %11 + 1;
deck[i] = randomNumber;
printf("%d\t", deck[i]);
if (numberPrinted < 3)
numberPrinted ++;
else
{
printf("\n");
numberPrinted = 0;
}
}
return 0;
}
int dealCardDealer ()
{
int card = deck[deal--];
dealerHand = card;
printf("\nCard Dealt: %d\n", dealerHand);
dealerHandTotal += dealerHand;
return 0;
}
int dealCardPlayer ()
{
int card = deck[deal--];
playerHand = card;
printf("\nCard Dealt: %d\n", playerHand);
playerHandTotal += playerHand;
return 0;
}
int playerChoice ()
{
char choice;
do
{
choice = 0;
while (choice < 1)
{
printf("Would you like to hit (H) or stand (S)?");
scanf(" %c", &choice);
}
switch(choice)
{
case 'H':
dealCardPlayer ();
printf("Player has %d", playerHandTotal);
break;
case 'S':
break;
default:
printf("\nINVALID SELCETION\n");
}
}
while (choice != 'S');
printf("Player has %d", playerHandTotal);
return 0;
}