Solved

black jack

Posted on 2000-03-01
10
319 Views
Last Modified: 2010-04-15
how to write a text mode black jack programe using standard C ?
0
Comment
Question by:adrianmak
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2575426
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2576890
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
 

Author Comment

by:adrianmak
ID: 2578655
Wvn,

Do you try before ?
Becuase there is a compliation error.
I am using Visual C++ 6.0
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2579083
o.
Sorry,just FYI,I find this code and paste here.I havent try it.
I'v withdrawed .
0
 

Author Comment

by:adrianmak
ID: 2579309
wvn,

where did you find this code ? On the Internet ?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 18

Expert Comment

by:deighton
ID: 2579372
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
 
LVL 18

Accepted Solution

by:
deighton earned 20 total points
ID: 2579408
/*#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
 
LVL 18

Expert Comment

by:deighton
ID: 2579554
Needless to say you don't need the square at the end.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2580560
Aint the same ?

0
 
LVL 18

Expert Comment

by:deighton
ID: 2586835
I fixed the  code and ran it under Turbo C successfully.  There were some errors.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now