程序设计21点扑克牌游戏.docx
《程序设计21点扑克牌游戏.docx》由会员分享,可在线阅读,更多相关《程序设计21点扑克牌游戏.docx(12页珍藏版)》请在冰豆网上搜索。
![程序设计21点扑克牌游戏.docx](https://file1.bdocx.com/fileroot1/2022-11/27/0fa97b90-524f-47ee-a32f-3a5de4e257bd/0fa97b90-524f-47ee-a32f-3a5de4e257bd1.gif)
程序设计21点扑克牌游戏
课程设计
课程名称:
程序设计
课题名称:
21点扑克牌游戏
班级:
计算机学院12-2BJ班
完成人:
曹开明(14122403434)
指导教师:
李宛珊
湖南理工学院计算机学院
2013年12月19日
一、设计目的
《面向对象程序设计课程设计》是计算机科学与技术专业和软件工程专业集中实践性环节之一,是学习完《面向对象程序设计》课程后进行的一次全面的综合练习。
其目的在于加深对面向对象程序设计中基础理论和基本知识的理解,培养学生的实践能力,促进理论与实践的结合。
通过课程设计,让学生能够熟练运用C++进行面向对象编程,建立对象模型,降低软件的复杂性,改善软件的重用性和维护性,提高软件的生产效率,全面掌握面向对象编程技术。
二、系统的功能分析
必须包括:
系统的功能需求,可以画出功能模块图
程序的N-S流程图如下图所示:
三、系统的程序设计与实现
系统的运行环境:
VisualC++6.0(完整绿色版)
开发环境:
C++
系统的详细设计:
类classCCard
该程序只需要一个CCard类,其中有如下步骤:
ClassCCard
Private:
IntmPip[5];(一共有5张牌)
IntmNumber;(牌的张数)
IntmDollar;(一共有多少钱)
IntmGamble;(赌注)
IntmWin;(赢得局数)
IntmLose;(输的局数)
IntmDraw;(平的局数)
Public:
CCard();(构造函数初始化)
VoidFistplayTwo();(第一次出两张牌)
IntGetNumber();(返回牌的数目)
IntGetPip();(显示牌的总点数)
VoidDisplayPip();(一次显示牌的点数)
VoidDisplayPip(int);(除第一张牌,一次显示牌的点数)
VoidTurnplay();(出第一张牌)
VoidWin();(赢了多少钱)
VoidLose();(输了)
VoidDraw();(平了)
IntSetGamble(int);(设置赌注)
IntGetMoney();(返回钱数)
VoidDisplayInfo();(打印必要信息)
IntGetCurrentCard();(返回当前牌)
四、系统的运行结果与分析
主要是开始运行的界面:
当赌注下多了:
玩完游戏:
附录:
系统的源程序
#include
#include
#include
usingnamespacestd;
classCCard
{
private:
intnaPip[5];
intnNumber;
intnDollar;
intnGamble;
intnWin;
intnLose;
intnDraw;
public:
CCard();
voidFirstPlayTwo();
intGetNumber();
intGetPip();
voidDisplayPip();
voidDisplayPip(int);
voidTurnPlay();
voidWin();
voidLose();
voidDraw();
intSetGamble(int);
intGetMoney();
voidDisplayInfo();
intGetCurrentCard();
};
CCard:
:
CCard()
{
nNumber=0;
nDollar=100;
for(inti=0;i<5;i++)
naPip[i]=0;
nGamble=0;
nWin=nLose=nDraw=0;
}
intCCard:
:
GetMoney(void)
{returnnDollar;}
voidCCard:
:
DisplayInfo()
{cout<<"您一共玩了"<\n";
cout<<"你的赌本共计有$"<\n";
}
intCCard:
:
SetGamble(intgamble)
{if(nDollar-gamble<0)
return-1;
if(gamble<0)
{if(nDollar-20<0)
return-1;
nGamble=20;
}
else
nGamble=gamble;
nDollar-=nGamble;
return0;
}
voidCCard:
:
FirstPlayTwo()
{naPip[0]=rand()%13+1;
naPip[1]=rand()%13+1;
nNumber=2;
}
intCCard:
:
GetCurrentCard()
{returnnaPip[nNumber-1];}
intCCard:
:
GetNumber()
{returnnNumber;}
intCCard:
:
GetPip()
{intnPip=0;
for(inti=0;i{
if(naPip[i]>=10)
nPip+=10;
else
nPip+=naPip[i];
}
returnnPip;
}
voidCCard:
:
DisplayPip()
{for(inti=0;icout<cout<}
voidCCard:
:
TurnPlay()
{nNumber++;
naPip[nNumber-1]=rand()%13+1;
}
voidCCard:
:
Win()
{cout<<"赢家牌面:
";
DisplayPip();
cout<<"牌面点数:
"<nDollar=nDollar+2*nGamble;
nWin++;
cout<<"赌本:
$"<cout<}
voidCCard:
:
Lose()
{nLose++;
cout<<"输家牌面:
";
DisplayPip();
if(GetPip()>21)
cout<<"暴了!
\n";
else
cout<<"牌面点数:
"<cout<<"赌本:
$"<cout<}
voidCCard:
:
Draw()
{nDraw++;
nDollar+=nGamble;
cout<<"平局牌面:
";
DisplayPip();
if(GetPip()>21)
cout<<"暴了!
\n";
else
cout<<"牌面点数:
"<cout<<"赌本:
$"<cout<}
voidCCard:
:
DisplayPip(intn)
{cout<<"[*]"<<'\t';
for(inti=1;icout<cout<}
voidDisplayRule(void)
{cout<<"\t欢迎进入曹开明的21点游戏世界!
\n\n";
cout<<"\t游戏规则:
\n";
cout<<"\t1.玩家最多可以要5张牌;\n";
cout<<"\t2.如果牌点的总数超过21点则暴点,自动判输;\n";
cout<<"\t3.赢家可得双倍赌注;\n";
cout<<"\t4.计算机方在牌点大于等于16时不再要牌。
\n\n";
cout<<"祝您好运!
\n";
cout<}
voidJudge(CCard&cpu,CCard&player)
{cout<if((cpu.GetPip()>21&&player.GetPip()>21)||cpu.GetPip()==player.GetPip())
{cout<<"\n\n平局!
\n";
cout<<"计算机数据:
\t";
cpu.DisplayPip();
cout<<"牌面点数:
"<cout<<"\n您的数据:
\t";
player.Draw();
cout<}
elseif((cpu.GetPip()>21)||(player.GetPip()>cpu.GetPip()&&player.GetPip()<=21))
{cout<<"\n\n恭喜您,您赢了!
\n";
cout<<"计算机数据:
\t";
cpu.DisplayPip();
cout<<"牌面点数:
"<cout<<"\n您的数据:
\t";
player.Win();
cout<}
else
{cout<<"\n\n很遗憾,您输了!
\n";
cout<<"计算机数据:
\t";
cpu.DisplayPip();
cout<<"牌面点数:
"<cout<<"\n您的数据:
\t";
player.Lose();
cout<}
}
voidPlayTurn(CCard&cpu,CCard&player)
{charchChoice;
intblCpu=1,blPlayer=1;
cpu.FirstPlayTwo();
player.FirstPlayTwo();
do
{cout<<"您的牌点为:
\t";
player.DisplayPip();
cout<<"计算机的牌点为:
\t";
cpu.DisplayPip
(1);
cout<<"您的牌面点数是:
"<if(blPlayer)
{cout<<"\n\n您是否继续要牌(Y/N)?
";
cin>>chChoice;
if((chChoice=='Y'||chChoice=='y'))
{if(player.GetNumber()<5)
{player.TurnPlay();
cout<<"您要的这张牌是:
"<if(player.GetPip()>21)
blPlayer=0;
}
else
{cout<<"对不起,您已经要了5张牌,不能再要牌了!
";
blPlayer=0;
}
}
}
if((chChoice=='N'||chChoice=='n'))
blPlayer=0;
if(cpu.GetPip()<16&&cpu.GetNumber()<5)
{cpu.TurnPlay();
cout<<"计算机要牌,牌点是:
"<}
else
blCpu=0;
if(blCpu&&player.GetNumber()<5&&player.GetPip()<21)
blPlayer=1;
}while(blCpu||blPlayer);
Judge(cpu,player);
return;
}
intmain(void)
{
srand((unsigned)time(NULL));
CCardcpu,player;
intblLogic;
intnMoney;
DisplayRule();
charchChoice;
cout<<"是否现在开始游戏(Y/N)?
\n";
cin>>chChoice;
while(chChoice=='Y'||chChoice=='y')
{do
{cout<<"您现在有赌本:
$"<cout<<"\n请下注(赌注不能超过赌本):
";
cin>>nMoney;
blLogic=player.SetGamble(nMoney);
if(blLogic)
cout<<"您的赌本不够,请重新下注!
\n";
}while(blLogic);
PlayTurn(cpu,player);
cout<<"是否继续21点游戏(Y/N)?
\n";
cin>>chChoice;
}
player.DisplayInfo();
cout<<"欢迎再次使用本程序!
\n\n\n";
return0;
}
自评
意见
做这个课程设计不但让我在C++编程方面的能力有很大的提高,更让我明白了做一个项目的困难。
这个项目做了两个星期,付出很多,收获很多。
签名:
年 月 日
自评
等级
A
指导
教师
评定
等级
签名:
年月日