• 03-10-2002
collegegal
I have to write a blackjack program in C. I have most of it written and I need a little help. Here's my code so far. Help! Help! Help!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DECKS 312
#define HEARTS 0x03
#define DIAMONDS 0x04
#define CLUBS 0x05

int GetCard();
int ConvertAce(int value);
int DealerAI();
void DisplayCards();
void InputChoice();
void InputChoice2();
void Results();
void SorH();
void Continue();
int draw_card(void);
void shuffle(void);
int rnd(int range);
void seedrnd(void);

int card1, card2, dealerCV, bet, dcard1, dcard2;
int playerCV = 0;
int cash = 500;
int cards[DECKS];
int r=DECKS;
int card;
char c,s;

void main()
{
srand(time(NULL));
DisplayCards();

}
int GetCard()
{

while( r--)
{
card = draw_card();
switch((card % 13) + 1)
{
case(1):
c = 'A';
break;
case(10):
c = 'T';
break;
case(11):
c = 'J';
break;
case(12):
c = 'Q';
break;
case(13):
c = 'K';
break;
default:
c = (card%13) + '1';
}

if(card>=0 && card<=13)
s = HEARTS;
else if(card>=14 && card<=26)
s = DIAMONDS;
else if(card>=27 && card<=39)
s = CLUBS;
else if(card>=40 && card<=52)
else if(card>=53&& card<=65)
s = HEARTS;
else if(card>=66 && card<=78)
s = DIAMONDS;
else if(card>=79 && card<=91)
s = CLUBS;
else if(card>=92 && card<=104)
else if(card>=105&& card<=117)
s = HEARTS;
else if(card>=118 && card<=130)
s = DIAMONDS;
else if(card>=131 && card<=143)
s = CLUBS;
else if(card>=144 && card<=156)
else if(card>=157&& card<=169)
s = HEARTS;
else if(card>=170 && card<=182)
s = DIAMONDS;
else if(card>=183 && card<=195)
s = CLUBS;
else if(card>=196 && card<=208)
else if(card>=209 && card<=221)
s = HEARTS;
else if(card>=222 && card<=234)
s = DIAMONDS;
else if(card>=235 && card<=247)
s = CLUBS;
else if(card>=248 && card<=260)
else if(card>=261 && card<=273)
s = HEARTS;
else if(card>=274 && card<=286)
s = DIAMONDS;
else if(card>=287 && card<=299)
s = CLUBS;
else
return r;

}
}
int draw_card(void)
{
int card;

do
{
card=rnd(DECKS);
}
while(cards[card]);
cards[card] = 1;
return(card+1);
}
void shuffle(void)
{
int x;

seedrnd();
for(x=0; x<DECKS; x++)
cards[x] = 0;
}

int rnd( int range)
{
int d;
d = rand()%range;
return(d);
}
void seedrnd(void)
{
srand((unsigned) time(NULL));
}

int ConvertAce(int value)
{
return 0;
}
void DisplayCards()
{
int flag1, flag2, dflag1, dflag2;

playerCV = 0;
dealerCV = 0;
dflag1 = 0;
dflag2 = 0;
dcard1 = GetCard();
dcard2 = GetCard();
flag1=0;
flag2=0;
card1 = GetCard();
card2 = GetCard();
printf("Cash : \$", cash);
if(cash<=0){
printf("You are bankrupt, GAME OVER!");
cash = 500;
Continue();
DisplayCards();
}
if (card1 == 11){
printf("Your first card is %c\n.", r);
flag1=1;
}
else if (card1 == 1){
printf("Your first card is %c\n.", r);
flag1=1;
}
else
printf("Your first card is %d.\n", r);
printf("Place your bet: \$ ", &bet);
if(bet > cash){
printf("You cannot bet more than what you have.\n");
}
if (dcard1 == 11){
printf("Dealer's first card is %c\n.", r);
dflag1=1;
}
else
printf("Dealer's first card is%c.\n", r);

Continue();
DisplayCards();
}
if (card2==11){
printf("Your second card is %d.\n", r);
flag2=1;

else if (card2==1){
printf("Your second card is %d.\n", r);
flag2=1;
}
else
printf("Your second card is %d.\n", r);
if (flag1==1){
card1=11;
}
if (flag2==11){
card2=11;
}
playerCV = card1 + card2;

if (22<=playerCV && flag1=1){
card1=1;
}
playerCV = card1 + card2;

if (22<=playerCV && flag2=2){
card2=1;
}
playerCV = card1 + card2;
}
void SorH()
char choice;
{
printf("Enter choice (H)it, (S)tand, s(P)lit, (D)ouble, (I)nsuranc.\n");

while (choice = getchar())

switch ( choice ){
case 'H': case 'h':
playerCV = playerCV + GetCard();
if (playerCV > 21 && flag1==1){
playerCV=-10;
else if (playerCV > 21 && flag2==1)
playerCV=-10;
else if (playerCV > 21)
system("CLS");
printf("Your total card value is %d\n.", playerCV);
printf("You bust\n");
cash = cash - bet;
Continue();
DisplayCards();
}
system("CLS");
printf("Your total card value is %d\n.", playerCV);
SorH();
break;
case 'S': case 's':
system("CLS");
Results();
break;
case 'P': case 'p':
split();
break;
case 'D': case 'd':
Double();
break;
case 'I': case 'i':
Insurance();
break;
}
void Results()
{
dealerCV = DealerAI();
printf("Your total card value is \n", playerCV);
if (playerCV > 21){
printf("You bust \n.");
cash = cash = bet;
Continue();
DisplayCards();
}
else
printf("Dealer's total card value is %d.\n", dealerCV);
if (delaerCV > 21){
printf("Dealer busts.\n");
cash = cash + bet;
Continue();
DisplayCards();
}
else {
if (playerCV > dealerCV){
printf("You win.\n");
cash = cash + bet;
}
if ( playerCV=<dealerCV){
printf("You lose.\n");
cash = cash - bet;
}
Continue();
DisplayCards();
}
}
int DealerAI()
{
int c1, c2, cv, f1, f2;
c1 = GetCard();
c2 = GetCard();

if (c1 == 11)
f1 = 1;

if ( c2 == 11)
f2 = 1;
cv = c1 + c2;

while ( cv<= 16)
cv = cv + GetCard();
if (cv > 21 & f1 == 1){
while (cv <= 16)
cv = 1 + c2 + GetCard();

}

if (cv > 21 & f2 == 1){
while (cv <= 16){
cv = c1 + 1 + GetCard();
}
return cv;
}
void Continue()
char decide;
{
printf("Enter (Q)uit, (R)estart.\n");

while ( decide = getchar());
switch ( decide ){
case 'Q': case 'q':
end game;
case 'R': case 'r':
printf("Press R to restart game"\n);
system("CLS");
}
• 03-11-2002
Prelude
>Help! Help! Help!
The first thing I can tell you is that you have a bunch of syntax errors such as not closing or opening braces and using = when you meant ==. I didn't bother going any further in debugging because completely left justified code that is any longer than a page is a pain. Post your code with the code tags and I'll check it more closely since quite a few of your bugs will be more easily found with properly indented code.
http://www.cprogramming.com/cboard/m...bbcode#buttons

-Prelude
• 03-11-2002
You might also want to state exactly what your problem is. Very few of us have enough time to go through reams of unformatted code looking for potential improvements, but most of us will point out errors if we know what you want help with!
• 03-11-2002
Unregistered
Well, at least this compiles.

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

#define DECKS 312
#define HEARTS 0x03
#define DIAMONDS 0x04
#define CLUBS 0x05

int GetCard();
int ConvertAce(int value);
int DealerAI();
void DisplayCards(int &flag1, int &flag2);
void InputChoice();
void InputChoice2();
void Results();
void SorH(int flag1, int flag2);
void Continue();
int draw_card(void);
void shuffle(void);
int rnd(int range);
void seedrnd(void);

int card1, card2, dealerCV, bet, dcard1, dcard2;
int playerCV = 0;
int cash = 500;
int cards[DECKS];
int r=DECKS;
int card;
char c,s;

int main()
{
int flag1, flag2;
srand(time(NULL));
DisplayCards(flag1, flag2);

}
int GetCard()
{

while( r--)
{
card = draw_card();
switch((card % 13) + 1)
{
case(1):
c = 'A';
break;
case(10):
c = 'T';
break;
case(11):
c = 'J';
break;
case(12):
c = 'Q';
break;
case(13):
c = 'K';
break;
default:
c = (card%13) + '1';
}

if(card>=0 && card<=13)
s = HEARTS;
else if(card>=14 && card<=26)
s = DIAMONDS;
else if(card>=27 && card<=39)
s = CLUBS;
else if(card>=40 && card<=52)
else if(card>=53&& card<=65)
s = HEARTS;
else if(card>=66 && card<=78)
s = DIAMONDS;
else if(card>=79 && card<=91)
s = CLUBS;
else if(card>=92 && card<=104)
else if(card>=105&& card<=117)
s = HEARTS;
else if(card>=118 && card<=130)
s = DIAMONDS;
else if(card>=131 && card<=143)
s = CLUBS;
else if(card>=144 && card<=156)
else if(card>=157&& card<=169)
s = HEARTS;
else if(card>=170 && card<=182)
s = DIAMONDS;
else if(card>=183 && card<=195)
s = CLUBS;
else if(card>=196 && card<=208)
else if(card>=209 && card<=221)
s = HEARTS;
else if(card>=222 && card<=234)
s = DIAMONDS;
else if(card>=235 && card<=247)
s = CLUBS;
else if(card>=248 && card<=260)
else if(card>=261 && card<=273)
s = HEARTS;
else if(card>=274 && card<=286)
s = DIAMONDS;
else if(card>=287 && card<=299)
s = CLUBS;
else
return r;

}
}
int draw_card(void)
{
int card;

do
{
card=rnd(DECKS);
}
while(cards[card]);
cards[card] = 1;
return(card+1);
}
void shuffle(void)
{
int x;

seedrnd();
for(x=0; x<DECKS; x++)
cards[x] = 0;
}

int rnd( int range)
{
int d;
d = rand()%range;
return(d);
}
void seedrnd(void)
{
srand((unsigned) time(NULL));
}

int ConvertAce(int value)
{
return 0;
}
void DisplayCards(int &flag1, int &flag2)
{
int dflag1, dflag2;

playerCV = 0;
dealerCV = 0;
dflag1 = 0;
dflag2 = 0;
dcard1 = GetCard();
dcard2 = GetCard();
flag1=0;
flag2=0;
card1 = GetCard();
card2 = GetCard();
printf("Cash : \$", cash);
if(cash<=0){
printf("You are bankrupt, GAME OVER!");
cash = 500;
Continue();
DisplayCards(flag1,flag2);
}
if (card1 == 11){
printf("Your first card is %c\n.", r);
flag1=1;
}
else if (card1 == 1){
printf("Your first card is %c\n.", r);
flag1=1;
}
else
printf("Your first card is %d.\n", r);
printf("Place your bet: \$ ", &bet);
if(bet > cash){
printf("You cannot bet more than what you have.\n");
}
if (dcard1 == 11){
printf("Dealer's first card is %c\n.", r);
dflag1=1;
}
else
printf("Dealer's first card is%c.\n", r);

Continue();
DisplayCards(flag1,flag2);
//}
if (card2==11){
printf("Your second card is %d.\n", r);
flag2=1;
}
else if (card2==1){
printf("Your second card is %d.\n", r);
flag2=1;
}
else
printf("Your second card is %d.\n", r);
if (flag1==1){
card1=11;
}
if (flag2==11){
card2=11;
}
playerCV = card1 + card2;

if (22<=playerCV && flag1==1){
card1=1;
}
playerCV = card1 + card2;

if (22<=playerCV && flag2==2){
card2=1;
}
playerCV = card1 + card2;
}
void SorH(int flag1, int flag2)
{
char choice;
printf("Enter choice (H)it, (S)tand, s(P)lit, (D)ouble, (I)nsuranc.\n");

while (choice = getchar())

switch ( choice ){
case 'H': case 'h':
playerCV = playerCV + GetCard();
if (playerCV > 21 && flag1==1){
playerCV=-10;
}
else if (playerCV > 21 && flag2==1)
playerCV=-10;
else if (playerCV > 21)
{
system("CLS");
printf("Your total card value is %d\n.", playerCV);
printf("You bust\n");
cash = cash - bet;
Continue();
DisplayCards(flag1,flag2);
}
system("CLS");
printf("Your total card value is %d\n.", playerCV);
SorH(flag1,flag2);
//break;
case 'S': case 's':
system("CLS");
Results();
break;
case 'P': case 'p':
//split();
break;
case 'D': case 'd':
//Double();
break;
case 'I': case 'i':
//Insurance();
break;
}
}
void Results()
{
int flag1,flag2;
int dealerCV;
dealerCV = DealerAI();
printf("Your total card value is \n", playerCV);
if (playerCV > 21){
printf("You bust \n.");
cash = cash = bet;
Continue();
DisplayCards(flag1,flag2);
}
else
printf("Dealer's total card value is %d.\n", dealerCV);
if (dealerCV > 21){
printf("Dealer busts.\n");
cash = cash + bet;
Continue();
DisplayCards(flag1,flag2);
}
else {
if (playerCV > dealerCV){
printf("You win.\n");
cash = cash + bet;
}
if ( playerCV<=dealerCV){
printf("You lose.\n");
cash = cash - bet;
}
Continue();
DisplayCards(flag1,flag2);
}
}
int DealerAI()
{
int c1, c2, cv, f1, f2;
c1 = GetCard();
c2 = GetCard();

if (c1 == 11)
f1 = 1;

if ( c2 == 11)
f2 = 1;
cv = c1 + c2;

while ( cv<= 16)
cv = cv + GetCard();
if (cv > 21 & f1 == 1){
while (cv <= 16)
cv = 1 + c2 + GetCard();

}

if (cv > 21 & f2 == 1){
while (cv <= 16){
cv = c1 + 1 + GetCard();
}
}
return cv;
}
void Continue()
{
char decide;
printf("Enter (Q)uit, (R)estart.\n");

while ( decide = getchar())
{
switch ( decide ){
case 'Q': case 'q':
exit(0);//end game
case 'R': case 'r':
printf("Press R to restart game\n");
system("CLS");
}
}
}
• 03-11-2002
collegegal
Thanks, now all I have to come up with is the functions for Split, Double, and Insurance. I really appreciate everybody's input. Sorry I got so nasty. I was up most of the night working on this. I have to turn it in on Wednesday for my final!! I hope I get it all done before then. I had a final exam tonight and another one tomorrow night. I also have to work full time. Well enough complaining! Thanks again. If any of you have some ideas for these other functions let me know. Especially for the Split function.