求21点游戏的程序设计的c++程序源代码
考试要求,但不会,我知道有点长,希望高手能#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
void replay(char &ans); //询问是否继续玩牌
void hit(int &total); //要一张牌
void deal(int &player,int &cpu,int &playerturns,int &cputurns); //为计算机和玩家各发两张牌
void BET(int &bet,int &money); //下注
void print(int wins,int lose,int draw,int money); //输出最后结果
void rules(); //输出规则
void results(int player,int cpu,int bet,int &money,int &draw,int &win,int &lose); //判断输赢
int random(long hi,long lo) //取两个参数之间的随机数
{
int ran;
srand((unsigned)time(NULL));
ran = rand()%(hi-(lo-1))+lo;
return(ran);
}
void wait(int milli) //暂停milli毫秒
{
clock_t start;
start = =clock();
while((clock()-start) < milli);
}
void pause() //暂停,任按一键继续
{
cout<<"任安一键继续"<<endl;
getch();
}
int main()
{
int player = 0,cpu = 0,win = 0,lose = 0,draw = 0,playerturns = 0,cputurns = 0,money = 0,bet;
char ans;
system("cls"); //执行系统命令,清屏
rules();
cout<<"\t\t\t请问是否玩牌:";
cin>>ans;
if((ans == 'y') || (ans == 'Y')) //检查输入是否为Yes
{
cout<<"\t\t\t你的赌本为100美元"<<endl; //起始的赌本
money = 100;
cout<<"\t\t\t";
pause(); //暂停,任安一键继续
}
else
{
return(0);
}
do
{
system("cls");
if(money<0) //看看有无赌本
{
cout<<"对不起,你没赌本了"<<endl; //若赌本输光,结束程序
return(0);
}
BET(bet,money); //下注的程序
deal(player,cpu,playerturns,cputurns); //给玩家和计算机各发两张牌
do
{
cout<<"\t\t\t你是想要牌(H)还是等待(S):"; //询问是否要牌
cin>>ans;
if((ans == 'h') || (ans == 'H')) //如果玩家要牌
{
playerturns++; //玩家手中的牌数增加一张
if(playerturns > 5) //判断玩家手中的牌数是否超过5张
cout<<"\t\t\t你手中不能超过5张牌"; //若超过,则不能继续要牌
}
if((playerturns < 6)&&(ans == 'h')) //判断是否符合玩家要牌的条件
{
cout<<endl;
hit(player); //调用发牌程序
}
}
while((ans == 'h') || (ans == 'H')); //继续询问玩家是否要牌
for(;(cpu < 16)&&(cputurns < 6);cputurns++) //计算机开始要牌的条件
{
cout<<endl;
cout<<"\t\t\t计算机要了一张牌"<<endl;
hit(cpu); //调用发牌程序
}
cout<<endl<<endl;
cout<<"\t\t\t计算机的牌面为:"<<cpu<<endl; //输出计算机的牌面的总点数
cout<<"\t\t\t你的牌面为:"<<player<<endl; //输出玩家牌面的总点数
cout<<endl;
results(player,cpu,bet,money,draw,win,lose); //判断输赢
replay(ans); //询问是否继续玩牌
}
while((ans == 'y') || (ans == 'Y'));
print(win,lose,draw,money); //游戏结束,输出结果
cout<<endl;
cout<<"\t\t\t\t";
return(0);
}
void rules() //游戏规则
{
cout<<"\t\t WELCOME TO THE GAME"<<endl;
cout<<"\t\t\t HERE ARE SOME SIMPLE RULES"<<endl;
cout<<"\t\t\t1:You Can only have a max of 5 cards."<<endl;
cout<<"\t\t\t2:If you bust you automatically lose."<<endl;
cout<<"\t\t\t3:If you win you win double what you bet."<<endl;
cout<<"\t\t\t4: The Dealer stops at or after 16."<<endl;
cout<<"\t\t\t\t";
pause();
}
void BET(int &bet,int &money) //接收玩家下注
{
system("cls");
cout<<"\t\t\t你现在有:$"<<money<<endl;
cout<<"\t\t\t你要下的赌注是:";
cin>>bet;
if(bet < 0)
bet = bet*-1;
money = money -bet;
}
void deal(int &player,int &cpu,int &playerturns,int &cputurns) //为计算机和玩家各发两张牌
{
int playercard1,playercard2,cpucard1,cpucard2;
playerturns = 2;
cputurn = 2;
playercard1 = random(13,1); //取得13与1之间的随机数
cout<<"\n\t\t\tDEALING HAND"<<endl;
wait(350); //等待350ms
playercard2 = random(13,1);
wait(150);
cpucard1 = random(13,1);
wait(350);
cpucard2 = random(13,1);
if(playercard1 >10) //若牌面的点数大于10,按半点来计点数
{
playercard1 = 0.5;
}
if(playercard2 > 10)
{
playercard = 0.5;
}
if(cpucard1 > 10)
{
cpucard1 = 0.5;
}
if(cpucard2 > 10)
{
cpucard2 = 0.5;
}
player = playercard1 + playercard2; //玩家两张牌的总点数
cpu = cpucard1 + cpucard 2; //计算机两张牌的总点数
cout<<"\t\t\t现在你的牌面点数是:"<<player <<endl;
cout<<"["<<playercard1<<"]";
cout<<"["<<playercard2<<"]";
cout<<endl;
cout<<endl;
cout<<"\t\t\t计算机有一张"<<cpucard1<<"显示"<<endl;
cout<<endl;
cout<<"[*]"<<"["<<cpucard1<<"]"; //计算机隐藏一张牌面
}
void hit(int &total) //要一张牌
{
int card;
card = random(13,1);
if(card >= 10)
{
card = 10;
}
total = total + card; //牌点总数
cout<<"\t\t\t牌面是:"<<card<<endl; //输出牌面
cout<<"\t\t\t总的牌面是:"<<total<<endl; //输出总点数
}
void results(int player,int cpu,int bet,int &money,int &draw,int &win,int &lose) //判断一局的结果
{
if(cpu == player) //平局
{
cout<<"\t\t\t平局"<<endl;
draw++;
}
if(player > 21) //玩家超过21点
{
cout<<"\t\t\t很遗憾,你输了"<<endl;
lose++;
}
else
{
if(cpu < player) //玩家赢了
{
cout<<"\n\t\t\t恭喜你,你赢了";
money = money + (bet * 2); //赌本加上双倍的赌注
win++;
}
}
if(cpu > 21) //计算机超过21点
{
cout<<"\t\t\t计算机输了"<<endl;
if(player < 21)
{
cout<<"\n\t\t\t恭喜你,你赢了";
win++;
money = money + (bet * 2);
}
}
else
{
if(cpu > player) //计算机赢了
{
cout<<"\t\t\t很遗憾,你输了"<<endl;
lose++;
}
}
}
void replay(char &ans) //询问是否再玩一局
{
cout<<"\n\t\t\t你是否想再玩一局:";
cin>>ans;
}
void print (int wins,int lose,int draw,int money) //输出最后结果
{
cout<<"\t\t\t\t赢的次数:"<<wins<<endl;
cout<<"\t\t\t\t输的次数:"<<lose<<endl;
cout<<"\t\t\t\t平局的次数:"<<draw<<endl;
cout<<"\t\t\t\t你的赌本:"<<money<<endl;
}
帮我一下,这里是关键代码,只需修改下,能运行就行。
[[it] 本帖最后由 zhujiancom 于 2008-11-17 22:42 编辑 [/it]]