Link to home
Start Free TrialLog in
Avatar of adrianmak
adrianmak

asked on

black jack

how to write a text mode black jack programe using standard C ?
Avatar of ozo
ozo
Flag of United States of America image

Avatar of Wyn
Wyn

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;
}


Avatar of adrianmak

ASKER

Wvn,

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

where did you find this code ? On the Internet ?
The code is c++, needs porting downwards to c.  Alot of it is the // comment marker, but also some operations on arrays and cout
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Needless to say you don't need the square at the end.
Aint the same ?

I fixed the  code and ran it under Turbo C successfully.  There were some errors.