21点游戏编程.docx

上传人:b****6 文档编号:6315387 上传时间:2023-01-05 格式:DOCX 页数:8 大小:16.96KB
下载 相关 举报
21点游戏编程.docx_第1页
第1页 / 共8页
21点游戏编程.docx_第2页
第2页 / 共8页
21点游戏编程.docx_第3页
第3页 / 共8页
21点游戏编程.docx_第4页
第4页 / 共8页
21点游戏编程.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

21点游戏编程.docx

《21点游戏编程.docx》由会员分享,可在线阅读,更多相关《21点游戏编程.docx(8页珍藏版)》请在冰豆网上搜索。

21点游戏编程.docx

21点游戏编程

/*Filename:

ThisprogramplaysagameofBlackjackwithyou.Thecomputeristhedealerandyouarethevictim-er,Imeanplayer.Thedealergetsacardthatyoucansee.Thedealerthenasksifyouwantanothercardbyasking“Hit”or“Stand.”Ifyouchoosetohit,thedealergivesyouanothercard.Ifyouchoosetostand,thedealerdrawsorstands,andthegameisplayedoutaccordingtothecardsyouandthedealerhave.AswithrealBlackjack,thedealerstandson17.Thewinnerisannouncedonlyafterboththeplayer’sandthedealer’shandsarefinished.*/

/******************************************************

ANSICstandardheaderfilesappearnext*/

#include<>

#include<>

#include<>

#include<>

/******************************************************

Definedconstantsappearnext*/

#defineBELL‘\a’

#defineDEALER0

#definePLAYER1

/*Mustkeeptwosetsoftotalsfordealerandforplayer.ThefirstsetcountsAcesas1andthesecondcountsAcesas“realworld”Blackjack,thisprogramdoesn’tallowsomeAcestobe1whileothersAcesare11inthesamehand.*/

#defineACELOW0

#defineACEHIGH1

/*Onlyoneglobalvariableisusedinthisentireprogram.Thevariableholds0,whichmeansfalseinitially.OncetheuserentershisorhernameininitCardsScreen(),thisvariableissetto1(fortrue),sothenameisneveraskedforagaintherestoftheprogram.*/

/******************************************************

Thisprogram’sspecificprototypes*/

voiddispTitle(void);

voidinitCardsScreen(intcards[52],intplayerPoints[2],

intdealerPoints[2],inttotal[2],

int*numCards);

intdealCard(int*numCards,intcards[52]);

voiddispCard(intcardDrawn,intpoints[2]);

voidtotalIt(intpoints[2],inttotal[2],intwho);

voiddealerGetsCard(int*numCards,intcards[52],

intdealerPoints[2]);

chargetans(charmesg[]);

voidfindWinner(inttotal[2]);

/******************************************************

C’sprogramexecutionalwaysbeginsatmain()here*/

Main()

{

intnumCards;/*Equals52atbeginningofeachgame*/

intcards[52],playerPoints[2],dealerPoints[2],total[2];

charans;/*Foruser’sHit/StandorYes/Noresponse*/

do{initCardsScreen(cards,playerPoints,dealerPoints,total,&numCards);

dealerGetsCard(&numCards,cards,dealerPoints);

printf(“\n”);/*Printsablankline*/

playerGetsCard(&numCards,cards,playerPoints);

playerGetsCard(&numCards,cards,playerPoints);

do{

ans=getAns(“Hitorstand(H/S)?

”);

if(ans==‘H’)

{playerGetsCard(&numCards,cards,

playerPoints);

}

}while(ans!

=‘S’)

totalIt(playerPoints,total,PLAYER);

/*Player’stotal*/

Do{

dealerGetsCard(&numCards,cards,dealerPoints);

}while(dealerPoints[ACEHIGH]<17);

/*17:

Dealerstops*/

totalIt(dealerPoints,total,DEALER);

/*第296页*/

/*Dealer’stotal*/

findWinner(total);

ans=getAns(“\nPlayagain(Y/N)?

“);

}while(ans==’Y’);

retutn;

}

/******************************************************

Thisfunctioninitializesthefacevaluesofthedeckofcardsbyputtingfoursetsof1-13inthe52-cardclearsthescreenanddisplaysatitle.*/

voidinitCardsScreen(intcards[52],intplayerPoints[2],intdealerPoints[2],inttotal[2],int*numCards)

{

intsub,val=1;/*Thisfunction’sWorkvariables*/

charfirstName[15];/*Holdsuser’sfirstname*/

*numCards=52;/*holdsrunningtotalofnumberofcards*/

For(sub=0;sub<=51;sub++){/*Countsfrom0to51*/

val=(val==14)?

1:

val;/*Ifvalis14,resetto1*/

cards[sub]=val;

val++;}

for(sub=0;sub<=1;sub++)/*Countsfrom0to1*/

{playerPoints[sub]=dealerPoints[sub]=total[sub]=0;}

dispTitle();

if(askedForName==0)/*Nameaskedforonlyonce*/

{printf(“/nWhatisyourfirstname?

“);

scanf(“%s”,firstName);

askedForName=1;/*Don’taskpromptagain*/

printf(“Ok,%s,getreadyforcasinoaction!

\n\n”,firstname);

getchar();/*Discardsnewline.Youcansafely*/

}/*ignorecompilerwarninghere.*/

Return;

}

/******************************************************

Thisfunctiongetsacardfortheplayerandupdatestheplayer’spoints.*/

voidplayerGetsCard(int*numCards,intcards[52],intplayerPoints[2])

{

intnewCard;

newCard=dealCard(numCards,cards);

printf(“Youdraw:

“);

dispCard(newCard,playerPoints);

}

/******************************************************

Thisfunctiongetsacardforthedealerandupdatesthedealer’spoints.*/

/*第297页*/

voiddealerGetsCard(int*numCards,intcards[52],intdealerPoints[2])

{

intnewCard;

newCard=dealCard(numCards,cards);

printf(“Thedealerdraws:

“);

dispCard(newCard,dealerPoints);

}

/******************************************************

Thisfunctiongetsacardfromthedeskandstoresitineitherthedealer’sortheplayer’sholdofcard.*/

intdealCard(int*numCards,intcards[52])

{

intcardDrawn,subDraw;

time_tt;/*Getstimeforarandomvalue*/

srand(time(&t));/*Seedsrandom-numbergenerator*/

subDraw=(rand()%(*numCards));/*From0tonumcards*/

cardDrawn=cards[subDraw];

cards[subDraw]=cards[*numCards-1];/*Putstopcard*/

(*numCards)-;/*inplaceofdrawnone*/

returncardDrawn;

}

/******************************************************

Displaysthelast-drawncardandupdatespointswithit.*/

voiddispCard(intcardDrawn,intpoints[2])

{

switch(cardDrawn){

case(11):

printf(“%s\n”,“Jack”);

points[ACELOW]+=10;

points[ACEHIGH]+=10;

break;

case(12):

printf(“%s\n”,“Queen”);

points[ACELOW]+=10;

points[ACEHIGH]+=10;

break;

case(13):

printf(“%s\n”,“King”);

points[ACELOW]+=10;

points[ACEHIGH]+=10;

break;

default:

points[ACELOW]+=cardDrawn;

if(cardDrawn==1)

{printf(“%s\n”,“Ace”);

points[ACEHIGH]+=11;

}

else

{points[ACEHIGH]+=cardDrawn;

printf(“%d\n”,cardDrawn);}

}

return;

}

/*第298页*/

/******************************************************

FiguresthetotalforplayerordealertoseewhofunctiontakesintoaccountthefactthatAceiseither1or11.*/

voidtotalIt(intpoints[2],inttotal[2],intwho)

{

/*ThefollowingroutinefirstlookstoseeifthetotalpointscountingAcesas1isequaltothetotalpointscountingAcesas11.Ifso,orifthetotalpointscountingAcesas11ismorethan21,theprogramusesthetotalwithAcesas1only.*/

if((points[ACELOW]==points[ACEHIGH])&&

(points[ACEHIGH]>21))

{total[who]=points[ACELOW];}/*KeepsallAcesas1*/

else

{total[who]=points[ACEHIGH];}/*KeepsallAcesas11*/

if(who==PLAYER)/*Determinesthemessageprinted*/

{printf(“Youhaveatotalof%d\n\n”,total[PLAYER]);}

else

{printf(“Thehousestandswithatotalof%d\n\n”,total[DEALER]);}

return;

}

/******************************************************

Printsthewinningplayer.*/

voidfindWinner(inttotal[2])

{

if(total[DEALER]==21)

{printf(“Thehousewins.\n”);

return;}

if((total[DEALER]>21)&&(total[PLAYER]>21))

{printf(“%s”,“Nobodywins.\n”);

return;}

if((total[DEALER]>=total[PLAYER])&&(total[DEALER]<21))

{printf(“Thehousewins.\n”);

return;}

if((total[PLAYER]>21)&&(total[DEALER]<21))

{printf(“Thehousewins.\n”);

return;}

printf(“%s%c”,“Youwin!

\n”,BELL);

return;

}

/******************************************************

Getstheuse’suppercase,single-characterresponse.*/

chargetAns(charmesg[])

{

charans;

printf(“%s”,mesg);/*Printsthepromptmessagepassed*/

ans=getchar();

getchar();/*Discardsnewline.Youcansafely*/

/*ignorecompilerwarninghere.*/

returntoupper(ans);

}

/******************************************************

Clearseverythingoffthescreen.*/

voiddispTitle(void)

{

inti=0;

while(i<25)/*Clearsscreenbyprinting25blank*/

{printf(“\n”);/*linesto“pushoff”stuffthat*/

i++;}/*mightbeleftoveronthescreen*/

/*beforethisprogram*/

printf(“\n\n*SteprightuptotheBlackjacktables*\n\n”);

return;

}

(完)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1