Thank you Adak for replying to my post. I'm still having ridiculous troubles with this program. I've tried what feels like a million different loop structures and still it doesn't work. I don't want to annoy with a long post of code, but I'm starting to get desperate for help. Thank you again to anyone who responds I really appreciate it!
There are two of my tries in user_turn which is still in the code one is commented out. I've tried numerous different bracket placements and as I said loop structures but cannot seem to get it. I keep getting an error in User_Turn that says " [Warning] control reaches end of non-void function"
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define true 1
#define false 0
/* function prototypes */
void clean (void);
char GetChoice();
void Initialize_Deck(int deck[]);
int Random_Number();
void Shuffle_Deck(int deck[]);
int Draw_One_Card(int deck[]);
void Display_Card(int card);
int Score_Card(int card);
int Users_Turn(int deck[]);
int Continue();
int Computer_Turn(int deck[]);
#define QUIT 'Q'
#define PLAY_GAME 'P'
#define DECK_SIZE 53
int deck[DECK_SIZE];
/*********************************************************************
* main()
*
*********************************************************************/
int main()
{
int choice;
printf(" Welcome! I'm happy to have someone to play blackjack with!\n\n");
do
{
choice = GetChoice();
switch (choice)
{
case PLAY_GAME:
printf("Here are the rules of the game:\n 1. Ace is worth 1 point\n 2. Cards 2 through 10 are worth their face value.\n");
printf(" 3. Jack, Queens and Kings are worth 10 points each\n 4. Your goal is to obtain 21 points, you get one turn only to draw cards\n");
printf(" 5. You get a total of 10 peanuts, each player bets one peanut per game\n 6. Whoever scores closer to 21 gets the others peanut\n");
int users_peanuts=10;
int computers_peanuts=10;
int users_score;
int dealers_score;
Initialize_Deck(deck);
printf("\n\nLet me grab the deck and then we can play!\n\n");
Shuffle_Deck(deck);
printf("The deck has been shuffled.\n\nLet's begin, you go first\n\n\n");
users_score=Users_Turn(deck);
dealers_score=Computer_Turn(deck);
if(dealers_score>users_score)
{
users_peanuts--;
computers_peanuts++;
}
if (users_peanuts==0)
{
printf("\nYou have no more peanuts! I have them all! YOU LOSE!\n");
GetChoice();
}
else
{
computers_peanuts--;
users_peanuts++;
}
if (computers_peanuts==0)
{
printf("\n I can't believe I lost....I demand a rematch!\n");
GetChoice();
}
/*
users_score=Users_Turn(int deck[]);
dealers_score
*/
break;
default:
if (choice != QUIT)
printf("\n That is NOT a valid choice. Please try again...\n");
break;
} /* end switch */
} while (choice != QUIT);
printf("Finishing the program.\n");
system("PAUSE");
return 0;
}
void clean (void)
{
char dummy;
do
{
scanf("%c",&dummy); /* read a single char */
} while(dummy != '\n'); /* if it was the NEWLINE we are done */
}
char GetChoice()
{
char choice;
printf("\n\n Would you like to continue?\n");
printf("%c: Quit the game\n", QUIT);
printf("%c: Play the game\n", PLAY_GAME);
scanf("\n%c", &choice);
clean(); /* empty the input buffer */
printf("\n\n");
return choice;
}
void Initialize_Deck(int deck[])
{
int i;
for (i=0; i < DECK_SIZE; i++)
{
deck[i]=i;
}
}
void Shuffle_Deck(int deck[])
{
int i;
int num;
int temp;
for (i=51; i > 0; i--)
{
srand ( (unsigned)time ( NULL ) );
num=Random_Number(i);
temp=deck[num];
deck[num]=deck[i];
deck[i]=temp;
}
}
int Users_Turn(int deck[])
{
int card;
int totalscore=0;
int score;
int choice;
choice=Continue();
/*
while(totalscore<21 && choice==1)
card=Draw_One_Card(deck);
Display_Card(card);
score=Score_Card(card);
totalscore=totalscore+score;
printf("\n\nYour total score so far in this hand is: %i\n\n", totalscore);
if (totalscore>21)
printf("Sorry you lose this round you owe me a peanut!HAHAHA");
totalscore=0;
return totalscore;
else
choice=Continue();
while(choice==2)
return totalscore;
*/
if (choice==2)
return totalscore;
if (choice==1&&totalscore<21)
{
card=Draw_One_Card(deck);
Display_Card(card);
score=Score_Card(card);
totalscore=totalscore+score;
printf("/n/nYour total score so far this hand is:%i\n\n",totalscore);
if(totalscore>21)
totalscore=0;
return totalscore;
}
}
int Computer_Turn(int deck[])
{
int card;
int totalscore=0;
int score;
if(totalscore<21)
card=Draw_One_Card(deck);
Display_Card(card);
score=Score_Card(card);
totalscore=totalscore+score;
printf("\n\n My total score so far in this hand is: %i\n\n", totalscore);
if (totalscore>21)
printf("I lost this round here's your peanut!\n");
return totalscore;
}
int Random_Number(int upper)
{
return rand()%(upper+1);
}
int Draw_One_Card(int deck[])
{
if (deck[52]==0)
{
Shuffle_Deck(deck);
}
deck[52]--;
return (deck[deck[52]]);
}
void Display_Card(int card)
{
int suit = card/13;
int rank = card%13;
rank=rank+1;
switch(rank)
{
case 1:
printf("The card drawn is the Ace");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
printf("\n The card drawn is the %i", rank);
break;
case 11:
printf("\n The card drawn is the J");
break;
case 12:
printf("\n The card drawn is the Q");
break;
case 13:
printf("\n The card drawn is the K");
break;
}
switch(suit)
{
case 0:
printf(" of SPADES\n");
break;
case 1:
printf(" of HEARTS\n");
break;
case 2:
printf(" of DIAMONDS\n");
break;
case 3:
printf(" of CLUBS\n");
break;
}
}
int Score_Card(int card)
{
int rank=card%13 + 1;
switch(rank)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return rank;
break;
case 11:
case 12:
case 13:
return 10;
break;
default:
if (rank>10)
printf("\n That is NOT a valid score....\n");
return 0;
break;
}
}
int Continue()
{
int choice;
printf("Would you like to draw a card?\n Enter 1 to draw:\n Enter 2 to not draw:\n");
scanf("%i", &choice);
clean();
return choice;
}