#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#define BELL '\a'
#define DEALER 0
#define PLAYER 1
#define ACELOW 0
#define ACEHIGH 1
int askedForName = 0;
void dispTitle(void);
void initCardsScreen(int cards[52],
int playerPoints[2],
int dealerPoints[2],
int total[2],
int * numCards);
int dealCard(int *numCards,int cards[52]);
void dispCard(int cardDrawn,int points[2]);
void totalIt(int points[2],int total[2],int who);
void dealerGetsCard(int *numCards,int cards[52],int dealerPoints[2]);
void playersGetsCard(int *numCards,int cards[52],int playerPoints[2]);
char getAns(char mesg[]);
void findWinner(int total[2]);
main ()
{
int numCards;
int cards[52],playerPoints[2],dealerPoints[2],total[2];
char ans;
do
{
initCardsScreen(cards[52],
playerPoints[2],
dealerPoints[2],
total[2],
&numCards);
dealerGetsCard (&numCards,cards,dealerPoints);
printf("\n");
playersGetsCard(&numCards,cards,playerPoints);
playersGetsCard(&numCards,cards,playerPoints);
do
{
ans = getAns("Hit or stand(H/S)?");
if (ans == 'H')
{
playersGetsCard(&numCards,cards,playerPoints);
}
}while (ans != 'S');
totalIt(playerPoints,total,PLAYER);
do
{
dealerGetsCard (&numCards,cards,dealerPoints);
}
while (dealerPoints[ACEHIGH]<17);
totalIt(dealerPoints,total,DEALER);
findwinner(total);
ans = getAns("\nPlay again (Y/N) ? ");
}
while (ans == 'Y');
return ;
}
void initCardsScreen(int cards[52],
int playerPoints[2],
int dealerPoints[2],
int total[2],
int *numCards);
{
int sub,val = 1;
char firstName[15];
*numCards = 52;
for (sub = 0; sub <= 51; sub++)
{
val = (val == 14) ? 1 : val;
cards[sub] = val;
val++;
}
for (sub = 0; sub <= 1; sub++)
{
playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
}
dispTitle();
if (askedForName == 0)
{
printf("\nWhat is your first name?");
scanf("%s",firstName);
askedForName = 1;
printf("OK, %s, get ready for casino action!\n\n",firstName);
getchar();
}
return;
}
/*这个功能给玩家一张牌并更新玩家的点数*/
void playersGetsCard(int *numCards,int cards[52],int playerPoints[2]);
{
int newCard;
newCard = dealCard(numCards,cards);
printf("You draw : ");
dispCard(newCard,playerPoints);
}
/*这个功能给dealer一张牌并更新dealer的点数*/
void dealerGetsCard(int *numCards,int cards[52],int dealerPoints[2]);
{
int newCard;
newCard = dealCard(numCards,cards);
printf("The dealer draws : ");
dispCard(newCard,playerPoints);
}
/*这个功能从桌上得到一张牌并且把它存在庄家或者玩家的分数中*/
int dealCard(int *numCards,int cards[52]);
{
int cardDrawn,subDraw;
time_t t;
srand(time(&t));
subDraw = ( rand() % (*numCards));
cardDrawn = cards[subDraw];
cards[subDraw] = cards[*numCards-1];
(*numCards)-;
return cardDrawn;
}
/*展示最后发出的牌并且更新点数*/
void dispCard(int cardDrawn,int points[2]);
{
switch (cardDrawn)
{
case(11): printf("%s\n,"Jack");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
braeak;
case(12): printf("%s\n,"Queen");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
braeak;
case(13): printf("%s\n,"King");
points[ACELOW] += 10;
points[ACEHIGH] += 10;
braeak;
default : points[ACELOW] += cardDrawn;
if (cardDrawn == 1)
{
printf("%s\n,"ACE");
points[ACEHIGH] += 11;
}
else
{
points[ACEHIGH] += cardDrawn;
printf("%D\n",cardDrawn);
}
}
return ;
}
/*计算出玩家或者庄家的总点数,并判断谁是赢家。考虑到 A 是1或者11.*/
void totalIt(int points[2],int total[2],int who);
{
if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21))
{
total[who] = points[ACELOW];
}
else
{
total[who] = points[ACEHIGH];
}
if (who ==PLAYER)
{
printf("You have a total of %d\n\n",total[PLAYER]);
}
else
{
printf("The house stands with a total of %d\n\n",total[DEALER]);
}
return ;
}
/*打印出赢家*/
void findWinner(int total[2]);
{
if (total[DEALER] == 21)
{
printf("The house wins.\n");
return ;
}
if (total[DEALER] > 21) && (total[PLAYER] > 21)
{
printf("%s","Nobody wins.\n");
return ;
}
if (total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21)
{
printf("The house wins.\n");
return ;
}
printf("%s%c","You win!\n");
return ;
}
/*21 */
char getAns(char mesg[]);
{
char ans;
printf("%s",mesg);
ans = getchar();
getchar();
return toupper(ans);
} /*错误指向这一行*/
/*清除屏幕*/
void dispTitle(void)
{
int i = 0;
while (i < 25)
{
printf("\n");
i++;
}
printf("\n\n*Step right up to the Blackjack tables*\n\n");
return ;
}