#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include<alloc.h>
#include<stddef.h>
#include<mem.h>
#include<conio.h>
int s[13]={1,2,3,4,5,6,7,8,9,10,10,10,10};
char *t[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
char *p[5],*q[5];
int s1,s2,z1,z2,u=1;
int ctrl1,ctrl2,ctrl,mark1,mark2;
int s2tmp;
void DELAY(long dtime)
{
long d;
for(d=0;d<=dtime;d++)
;
}
int acer(int acer)
{
if(acer==0)
return 1;
else
return 0;
}
void S1S2(int s1,int s2,int z1,int z2)
{
if(z1+z2==0)
;
else if(z1==0&&z2==1)
s2=s2+10<=21?s2+10:s2;
else if(z1==1&&z2==0)
s1=s1+10<=21?s1+10:s1;
else if(z1==1&&z2==1)
{
s1=s1+10<=21?s1+10:s1;
s2=s2+01<=21?s2+10:s2;
}
else if(z1==1&&z2==2)
{
s1=s1+10<=21?s1+10:s1;
s2=s2+10+10<=21?s2+20:s2+10<=21?s2+10:s2;
}
else if(z1==2&&z2==1)
{
s1+20<=21?s1+20:s1+10<=21?s1+10:s1;
s2+10<=21?s1+10:s1;
}
else if(z1==2&&z2==2)
{
s1+20<=21?s1+20:s1+10<=21?s1+10:s1;
s2=s2+10+10<=21?s2+20:s2+10<=21?s2+10:s2;
}
else
;
}
int result1(int s1,int s2)
{
if(s1>21)
u=1;
else if(s2>21)
u=0;
else if(s1<=s2)
u=1;
else
u=0;
return u;
}
int result(int s1,int s2,int ii,int jj,int z1,int z2)
{
if(ii==5&&jj==5)
u=s1>s2?1:0;
else if(jj==5&&ii!=5&&s2<=21)
u=1;
else if(ii==5&&jj!=5&&s1<=21)
u=0;
else
{
S1S2(s1,s2,z1,z2);
u=result1(s1,s2);
}
return u;
}
void GiveCard(int ctr)
{
if(ctr==0)
{
int y;
y=random(13);
q[ctrl2++]=t[y];
z2+=acer(y);
s2+=s[y];
}
if(ctr==2)
{
int y;
y=random(13);
q[ctrl2++]=t[y];
z2+=acer(y);
s2+=s[y];
printf("\nComputer got a card '%s'...\n",t[y]);
}
else if(ctr==1)
{
int x;
x=random(13);
p[ctrl1++]=t[x];
z1+=acer(x);
s1+=s[x];
printf("\nYou got a card '%s'...\n",t[x]);
}
else if(ctr==-1)
{
s1=0;
s2=0;
z1=0;
z2=0;
mark1=2;
mark2=2;
ctrl1=0;
ctrl2=0;
}
DELAY(1024);
}
void YouGetCard()
{
char ch;
if(ctrl1<5)
{
printf("Do You Want Another Card?[yn]");
YON:
ch=getche();
switch(ch)
{
case 'y' : ;
case 'Y' : {
GiveCard(1);
mark1++;
}break;
case 'N' : ;
case 'n' : ctrl1=5;break;
default : goto YON;
}
}
else
;
}
void ComputerGetCard(void)
{
s2tmp=s2;
S1S2(s1,s2,z1,z2);
if(ctrl2<5)
{
if(s2>=s1)
{
if(ctrl1==5)
ctrl2=5;
else
if(s2tmp+random(10)+1<=21)
{
s2=s2tmp;
GiveCard(2);
mark2++;
}
}
else
{
if(s2tmp+random(10)+1<=21)
{
s2=s2tmp;
GiveCard(2);
mark2++;
}
else
ctrl2=5;
}
}
}
main()
{
char choice;
unsigned long int dely;
int temp;
char name[100];
clrscr();
START:printf("A small game...BlackJack\nPress 1 if you want to play the game...\nPress 2 if you want to exit...\n");
choice=getch();
if(choice=='2')
exit(1);
else if(choice=='1')
{
clrscr();
printf("Welcome to the BlackJack Game...\n");
do
{
printf("Please Input Your Name:");
gets(name);
}
while(*name=='\x0');
printf("Please Press Any Key To Start The Game...");
getch();
clrscr();
GiveCard(-1);
randomize();
printf("Welcom %s to the game...\n\n",name);
printf("Turn 1:\n");
GiveCard(1);
GiveCard(0);
printf("\nTurn 2:\n");
GiveCard(1);
GiveCard(2);
printf("\n");
temp=s1;
for(ctrl=3;ctrl<=5&&ctrl1*ctrl2!=25;ctrl++)
{
printf("\nTurn %d:\n",ctrl);
s1=temp;
if(ctrl1<5)
YouGetCard();
if(s1>21)
break;
temp=s1;
if(ctrl2<5)
ComputerGetCard();
if(s2>21)
break;
}
}
else
{
clrscr();
goto START;
}
printf("\n\nIn order you get:\n");
for(ctrl1=0;ctrl1<mark1;ctrl1++)
printf("%s\t",p[ctrl1]);
printf("\n\nIn order computer get:\n\n");
for(ctrl2=0;ctrl2<mark2;ctrl2++)
printf("%s\t",q[ctrl2]);
u=result(s1,s2,mark1,mark2,z1,z2);
if(u==1)
printf("\n\n\tYou Lost!\n\n");
else
printf("\n\n\tYou Win!\n\n");
printf("\nPlease Press Any Key To Continue...");
getch();
clrscr();
goto START;
}