• C

black jack

how to write a text mode black jack programe using standard C ?
adrianmakAsked:
Who is Participating?
 
deightonConnect With a Mentor progCommented:
/*#include <iostream.h> */
                   #include <conio.h>
                   #include <stdlib.h>
                   #include <time.h>

               #define Size_Array  52  /*/Amount of cards!*/

                   const char *Card_Name[] =
                   {
                   "Ace",
                   "Two",
                   "Three",
                   "Four",
                   "Five",
                   "Six",
                   "Seven",
                   "Eight",
                   "Nine",
                   "Ten",
                   "Jack",
                   "Queen",
                   "King"
               }; /*Const used for displaying card by name!*/

                   const char *Suite_Name[] =
                   {
                   "Hearts",
                   "Spades",
                   "Diamonds",
                   "Spades"
                   };

                   int Deck[Size_Array];
                   char Suite[Size_Array];
               /*Deck is global!*/

               void swap(int *n1, int *n2)
                   {
                   int temp;
               temp = *n1;
               *n1 = *n2;
               *n2 = temp;
                   }

                   void Shuffle_Array(int *array)
                   {
                   int ctr;
                   int n1,n2;
               /*Swaps random cards around 1000 times!*/
               /*Shuffle :) */
                   for (ctr=0;ctr<1000;ctr++)
                   {
                   n1 = rand()%Size_Array;
                   n2 = rand()%Size_Array;
               swap(&array[n1],&array[n2]);
                   }
                   }

                   void New_Game(void)
                   {
                   int ctr;
                   for (ctr=0;ctr<Size_Array;ctr++)
                   {
               Deck[ctr]=ctr/4; /*cards 0-12*/
               Suite[ctr]=ctr%4; /*suites 0-3*/
                   }
               Shuffle_Array(Deck); /*Shuffle deck.*/
                   }

                   void Show_Computer(char *hand, char many)
                   {
                   int ctr;
                   gotoxy(1,1);
               /*Goto first box on screen for printing!*/
               puts("Computer has one you can't see and:");
               for (ctr=1;ctr<many;ctr++) /*Start at 1 to miss first card!*/
               printf("%s%s%s\n",Card_Name[Deck[hand[ctr]]]," of ",Suite_Name[Suite[hand[ctr]]]);
                   }

                   void Show_Computer2(char *hand, char many, int total)
                   {
                   int ctr;
                   gotoxy(1,1);
               puts("Computer has:");
                   for (ctr=0;ctr<many;ctr++)
               printf("%s%s%s\n",Card_Name[Deck[hand[ctr]]]," of " ,Suite_Name[Suite[hand[ctr]]]);
               printf("\nTotal %i\n",total);
                   }

                   void Show_Player(char *hand, char many, int total)
                   {
                   int ctr;
               printf("\n\n\n\n\n");
               puts("You have:");
                   for (ctr=0;ctr<many;ctr++)
               printf("%s of %s\n",Card_Name[Deck[hand[ctr]]],Suite_Name[Suite[hand[ctr]]]);
               printf("\nTotal %i\n",total);
                   }

                   void Comp_Won(void)
                   {
               puts("Computer wins!");
                   }

                   void Play_Won(void)
                   {
               puts("You won!");
                   }

                   void Nobody_Won(void)
                   {
               puts("Bust!");
                   }

                   int Get_Total(char *hand, char many)
                   {
                   int ctr;
                   int total=0;
                   int aces=0;
                   int temp;
                   for (ctr=0;ctr<many;ctr++)
                   {
                   temp = Deck[hand[ctr]]+1;
               if (temp>10) temp=10; /*Check for jack+... all =10*/
                   total+=temp;
                   if (Deck[hand[ctr]]==0)
                   {
                   aces++;
                   total+=10;
                   }
                   }
                   while (total>21 && aces>0)
                   {
                   total-=10;
                   aces--;
                   }
                   return total;
                   }

                   int main (void)
                   {
               char P_Cards[7],C_Cards[7]; /*Can't get more than 7 cards without busting!*/
                   int P_Amount, C_Amount;
                   int P_Total;
                   int C_Total;
                   int Loc_In_Deck;
                   char done=0;
                   char key;
                   srand ( time(NULL));
                   while (!done)
                   {
                   New_Game();
               /*Initialize cards again.*/

                   P_Cards[0]=0;
                   C_Cards[0]=1;
                   P_Cards[1]=2;
                   C_Cards[1]=3;
               /*Deal first four cards out.*/
                   P_Amount=2;
                   C_Amount=2;
               /*Player and computer have 2 cards each so far*/

               Loc_In_Deck = 4; /*4 cards dealt already!*/

               clrscr(); /*Clear the screen for displaying*/

               /*Get totals*/
                   C_Total = Get_Total(C_Cards,C_Amount);
                   P_Total = Get_Total(P_Cards,P_Amount);

                   Show_Computer(C_Cards,C_Amount);
                   Show_Player(P_Cards,P_Amount, P_Total);

                   if (C_Total!=21 && P_Total!=21)
                   {
                   while (P_Total<21)
                   {
               printf("\n\n\n(H)it or (S)tay?");
                   key = getch();
                   if (key=='h' || key=='H')
                   {
                   P_Cards[P_Amount]=Loc_In_Deck;
                   P_Amount++;
                   Loc_In_Deck++;
                   }
                   else
                   break;
               /*Get totals*/
                   C_Total = Get_Total(C_Cards,C_Amount);
                   P_Total = Get_Total(P_Cards,P_Amount);

                   clrscr();

                   Show_Computer(C_Cards,C_Amount);
                   Show_Player(P_Cards,P_Amount, P_Total);
                   }
                   while (C_Total<16 && P_Total<22)
                   {
                   C_Cards[C_Amount]=Loc_In_Deck;
                   C_Amount++;
                   Loc_In_Deck++;

               /*Get totals*/
                   C_Total = Get_Total(C_Cards,C_Amount);
                   P_Total = Get_Total(P_Cards,P_Amount);

                   clrscr();

                   Show_Computer2(C_Cards,C_Amount,C_Total);
                   Show_Player(P_Cards,P_Amount, P_Total);
               printf("\n\n\nComputer hits");
                   getch();
                   }
                   }
                   else
                   {
               if (C_Total == 21 && P_Total<21) /*Make sure not a tie*/
               puts("Computer has Black Jack!");
               else if (C_Total == 21) /*If C_total was 21 and P_Total was =21*/
               printf("You both got Black Jack!");
                   else
               printf("You got Black Jack!");
                   }
                   clrscr();
                   Show_Computer2(C_Cards,C_Amount,C_Total);
               Show_Player(P_Cards,P_Amount, P_Total);
               /*LOL*/
               printf("\n\n\n\n");
                   if (P_Total<22)
                   if (P_Total>C_Total)
                   Play_Won();
                   else if (C_Total<22)
                   Comp_Won();
                   else
                   Play_Won();
                   else
                   Comp_Won();
               printf(" Play again?");
                   key=getch();
                   if (key=='n' || key=='N') done=1;
                   }
                   return 0;
                   } 
0
 
WynCommented:
All can offer:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

const Size_Array = 52; //Amount of cards!

const char *Card_Name[] =
{
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"
}; //Const used for displaying card by name!

const char *Suite_Name[] =
{
"Hearts",
"Spades",
"Diamonds",
"Spades"
};

int Deck[Size_Array];
char Suite[Size_Array];
//Deck is global!

void swap(int &n1, int &n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}

void Shuffle_Array(int *array)
{
int ctr;
int n1,n2;
//Swaps random cards around 1000 times!
//Shuffle :)
for (ctr=0;ctr<1000;ctr++)
{
n1 = rand()%Size_Array;
n2 = rand()%Size_Array;
swap(array[n1],array[n2]);
}
}

void New_Game(void)
{
int ctr;
for (ctr=0;ctr<Size_Array;ctr++)
{
Deck[ctr]=ctr/4; //cards 0-12
Suite[ctr]=ctr%4; //suites 0-3
}
Shuffle_Array(Deck); //Shuffle deck.
}

void Show_Computer(char *hand, char many)
{
int ctr;
gotoxy(1,1);
//Goto first box on screen for printing!
cout << "Computer has one you can't see and:" << endl;
for (ctr=1;ctr<many;ctr++) //Start at 1 to miss first card!
cout << Card_Name[Deck[hand[ctr]]] << " of " << Suite_Name[Suite[hand[ctr]]] << endl;
}

void Show_Computer2(char *hand, char many, int total)
{
int ctr;
gotoxy(1,1);
cout << "Computer has:" << endl;
for (ctr=0;ctr<many;ctr++)
cout << Card_Name[Deck[hand[ctr]]] << " of " << Suite_Name[Suite[hand[ctr]]] << endl;
cout << endl << "Total " << total << endl;
}

void Show_Player(char *hand, char many, int total)
{
int ctr;
cout << "\n\n\n\n\n"; //\n = endl
cout << "You have:" << endl;
for (ctr=0;ctr<many;ctr++)
cout << Card_Name[Deck[hand[ctr]]] << " of " << Suite_Name[Suite[hand[ctr]]] << endl;
cout << endl << "Total " << total << endl;
}

void Comp_Won(void)
{
cout << "Computer wins!";
}

void Play_Won(void)
{
cout << "You won!";
}

void Nobody_Won(void)
{
cout << "Bust!";
}

int Get_Total(char *hand, char many)
{
int ctr;
int total=0;
int aces=0;
int temp;
for (ctr=0;ctr<many;ctr++)
{
temp = Deck[hand[ctr]]+1;
if (temp>10) temp=10; //Check for jack+... all =10
total+=temp;
if (Deck[hand[ctr]]==0)
{
aces++;
total+=10;
}
}
while (total>21 && aces>0)
{
total-=10;
aces--;
}
return total;
}

int main (void)
{
char P_Cards[7],C_Cards[7]; //Can't get more than 7 cards without busting!
int P_Amount, C_Amount;
int P_Total;
int C_Total;
int Loc_In_Deck;
char done=0;
char key;
srand ( time(NULL));
while (!done)
{
New_Game();
//Initialize cards again.

P_Cards[0]=0;
C_Cards[0]=1;
P_Cards[1]=2;
C_Cards[1]=3;
//Deal first four cards out.
P_Amount=2;
C_Amount=2;
//Player and computer have 2 cards each so far

Loc_In_Deck = 4; //4 cards dealt already!

clrscr(); //Clear the screen for displaying

//Get totals
C_Total = Get_Total(C_Cards,C_Amount);
P_Total = Get_Total(P_Cards,P_Amount);

Show_Computer(C_Cards,C_Amount);
Show_Player(P_Cards,P_Amount, P_Total);

if (C_Total!=21 && P_Total!=21)
{
while (P_Total<21)
{
cout << "\n\n\n(H)it or (S)tay?";
key = getch();
if (key=='h' || key=='H')
{
P_Cards[P_Amount]=Loc_In_Deck;
P_Amount++;
Loc_In_Deck++;
}
else
break;
//Get totals
C_Total = Get_Total(C_Cards,C_Amount);
P_Total = Get_Total(P_Cards,P_Amount);

clrscr();

Show_Computer(C_Cards,C_Amount);
Show_Player(P_Cards,P_Amount, P_Total);
}
while (C_Total<16 && P_Total<22)
{
C_Cards[C_Amount]=Loc_In_Deck;
C_Amount++;
Loc_In_Deck++;

//Get totals
C_Total = Get_Total(C_Cards,C_Amount);
P_Total = Get_Total(P_Cards,P_Amount);

clrscr();

Show_Computer2(C_Cards,C_Amount,C_Total);
Show_Player(P_Cards,P_Amount, P_Total);
cout << "\n\n\nComputer hits";
getch();
}
}
else
{
if (C_Total == 21 && P_Total<21) //Make sure not a tie
cout << "Computer has Black Jack!";
else if (C_Total == 21) //If C_total was 21 and P_Total was =21
cout << "You both got Black Jack!";
else
cout << "You got Black Jack!";
}
clrscr();
Show_Computer2(C_Cards,C_Amount,C_Total);
Show_Player(P_Cards,P_Amount, P_Total);
cout << "\n\n\n\n";
if (P_Total<22)
if (P_Total>C_Total)
Play_Won();
else if (C_Total<22)
Comp_Won();
else
Play_Won();
else
Comp_Won();
cout << " Play again?";
key=getch();
if (key=='n' || key=='N') done=1;
}
return 0;
}


0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
adrianmakAuthor Commented:
Wvn,

Do you try before ?
Becuase there is a compliation error.
I am using Visual C++ 6.0
0
 
WynCommented:
o.
Sorry,just FYI,I find this code and paste here.I havent try it.
I'v withdrawed .
0
 
adrianmakAuthor Commented:
wvn,

where did you find this code ? On the Internet ?
0
 
deightonprogCommented:
The code is c++, needs porting downwards to c.  Alot of it is the // comment marker, but also some operations on arrays and cout
0
 
deightonprogCommented:
Needless to say you don't need the square at the end.
0
 
WynCommented:
Aint the same ?

0
 
deightonprogCommented:
I fixed the  code and ran it under Turbo C successfully.  There were some errors.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.