南京理工大学 C++课程设计 扑克牌游戏多文件.docx
《南京理工大学 C++课程设计 扑克牌游戏多文件.docx》由会员分享,可在线阅读,更多相关《南京理工大学 C++课程设计 扑克牌游戏多文件.docx(26页珍藏版)》请在冰点文库上搜索。
![南京理工大学 C++课程设计 扑克牌游戏多文件.docx](https://file1.bingdoc.com/fileroot1/2023-5/11/23ced232-e0ff-4621-b76c-83869234fe9d/23ced232-e0ff-4621-b76c-83869234fe9d1.gif)
南京理工大学C++课程设计扑克牌游戏多文件
南京理工大学紫金学院C++课程设计报告
课程:
C++课程设计
学院:
计算机学院
班级:
计算机大类四班
学号:
170310441
姓名:
田越
选题名称:
扑克牌游戏
起止时间:
2018.5.22~2018.6.1
指导教师:
杜兰
2018年5月
1问题描述
在提供的源程序基础上,写一个程序,可以进行洗牌等操作.
2基本要求
1)增加大小王.
2)删去一张指定扑克牌后,进行余牌显示.
3)创建一副扑克牌,显示一副扑克牌,洗牌,依次分发给四人并显示.
3需求分析
输入形式与范围是按照提示语句输入相应的数字或是(Y)否(N)的英文字母
输出形式即为提示语句所说明的内容
该程序基本任务是完成扑克洗牌,显示玩家手牌,按花色或牌面整理手牌(排序)
测试数据为54张扑克牌面值以及花色,无需手动设置
正确的输入将使得程序按说明运行,错误输入默认直接退出程序
4概要设计
分为8个部分,也是八个文件
头文件两个:
all.h包含主程序所需的所有头文件.
class.h包含所有的类,并卷入all.h
其余cpp文件,共6个,说明如下:
main函数主调函数
deck包含deck类的函数
perdeck包含perdeck类的函数
playdeck包含playdeck类的函数
playingcard包含playingcard类的函数
main函数调用的函数顾名思义
5详细设计
All.h
#pragmaonce
#include
#include//编译器是VS2017
#include
#include
#include
#include
Class.h
#pragmaonce
#include"all.h"
usingnamespacestd;
classPerDeck;
classPlayingCard
{
private:
intnValue;//扑克牌实际面值
charachFace[3];//扑克牌字符串A,2,......10,J,Q,K
charchSuit;//扑克牌花色
public:
friendclassPerDeck;
PlayingCard();//将对象清零
voidShowCard();//显示面值花色
voidCardMake(int);//给一张扑克牌赋值(面值,花色)
};
classDeck
{
private:
PlayingCardm_cardarray[54];
intnLastDelt;
public:
friendclassPerDeck;
Deck();
voidMakeDeck();//将52张牌清零(没有大,小王)
voidDealOne();//删除开头一张牌
voidShowDeck();//依次逐张显示这副牌
voidDeckShuff(int);//用于洗牌,参数是洗牌次数
voidRemoveCard();//清零删除掉的那张牌
};
classPerDeck
{
private:
PlayingCardmyCard[20];
intnNum;
public:
PerDeck(){}
voidMakePerDeck(Deck&,int);
voidShow();
voidSortFace();
voidSortSuit();
intDelete(intn);
};
classPlayDeck
{
private:
PerDeckmyDeck[4];
public:
PlayDeck(){}
voidPlayCard(Deck&);
voidShow();
intDeleteOne(introw,intcolumn);
intDeleteAll(int,int,int,int);
voidSortDeckFace(int);
voidSortDeckSuit(int);
voids(int);
};
voidProgramInit();//初始化,并输出信息
voidCenterText(charShellText[80]);//居中显示
intGetNumber();//键入整数型
charGetKey();//键入字符串型
voidDeckMakeDiver();//主要算法(生成扑克牌,洗牌,删除等)
intGetRandInt(intmin,intmax);//在min到max间选随机值
voidProgClose();//暂停,供用户查看中间运行结果
main函数.cpp
#include"class.h"
usingnamespacestd;
intmain(void)
{
srand((unsigned)time(NULL));
intnCardNum=0;
ProgramInit();
DeckMakeDiver();
ProgClose();
return1;
}
Playdeck.cpp
#include"class.h"
#include
usingnamespacestd;
voidPlayDeck:
:
PlayCard(Deck&k)
{
inta,b,c,d;
charg[29]={"依次输入四位玩家手里的牌数:
"};
CenterText(g);
cout<cin>>a>>b>>c>>d;
if(a+b+c+d>54)
{
charg[]={"超过牌的张数,退出游戏。
"};
CenterText(g);
exit(0);
}
myDeck[0].MakePerDeck(k,a);
myDeck[1].MakePerDeck(k,b);
myDeck[2].MakePerDeck(k,c);
myDeck[3].MakePerDeck(k,d);
}
voidPlayDeck:
:
Show()
{
chara[6]={"玩家1"};
charb[6]={"玩家2"};
charc[6]={"玩家3"};
chard[6]={"玩家4"};
CenterText(a);
cout<myDeck[0].Show();
CenterText(b);
cout<myDeck[1].Show();
CenterText(c);
cout<myDeck[2].Show();
CenterText(d);
cout<myDeck[3].Show();
}
intPlayDeck:
:
DeleteOne(introw,intcolumn)
{
myDeck[row-1].Delete(column);
return1;
}
intPlayDeck:
:
DeleteAll(inta,intb,intc,intd)
{
myDeck[0].Delete(a);
myDeck[1].Delete(b);
myDeck[2].Delete(c);
myDeck[3].Delete(d);
return1;
}
voidPlayDeck:
:
SortDeckFace(intb)
{
myDeck[b-1].SortFace();
}
voidPlayDeck:
:
SortDeckSuit(intb)
{
myDeck[b-1].SortSuit();
}
voidPlayDeck:
:
s(inta)
{
myDeck[a-1].Show();
}
Deck.cpp
#include"class.h"
#include
usingstd:
:
ostream;
usingnamespacestd;
voidDeck:
:
MakeDeck()//对生成的52张牌逐个赋值
{
nLastDelt=53;//51
while(nLastDelt>-1)
{
m_cardarray[nLastDelt].CardMake(nLastDelt);
nLastDelt--;
}
}
voidDeck:
:
ShowDeck()//显示一副扑克牌
{
chara[13]={"完整的一副牌"};
intnIndex=nLastDelt+1;
intnNewLine=0;
CenterText(a);
cout<while(nIndex<=53)//51
{
if(nNewLine%11==0)cout<m_cardarray[nIndex].ShowCard();
nNewLine++;
nIndex++;
}
}
voidDeck:
:
DeckShuff(inttimes)//洗牌算法
{
chara[11]={"洗牌......"};
intx,nSplit;
CenterText(a);
cout<for(x=0;x<=times;x++)
{
nSplit=GetRandInt(20,35);
DeckTopDeck;
DeckBottomDeck;
inti;
intnBottomNum=1;
intnTopNum=1;
for(i=0;i<=nSplit-1;)
{
TopDeck.m_cardarray[nTopNum]=this->m_cardarray[i];
nTopNum++;
i++;
}
for(i=(nSplit);i<54;)//52
{
BottomDeck.m_cardarray[nBottomNum]=this->m_cardarray[i];
nBottomNum++;
i++;
}
intnOutNum=0;
intnNumToTop;
intnNumToBottom;
intj;
inth=0;
nBottomNum=54-nSplit;//52
nTopNum=nSplit;
while(nOutNum<53)//51
{
nNumToTop=GetRandInt(2,7);
nNumToBottom=GetRandInt(2,7);
for(j=0;j<=nNumToBottom;j++)
{
if(nBottomNum>0)
{
this->m_cardarray[nOutNum]=BottomDeck.m_cardarray[nBottomNum];
nOutNum++;
nBottomNum--;
}
for(h=0;h<=nNumToTop;h++)
{
if((nTopNum>0)&&(nOutNum<=54))//52
{
this->m_cardarray[nOutNum]=TopDeck.m_cardarray[nTopNum];
nOutNum++;
nTopNum--;
}
}
}
}
}
this->nLastDelt=-1;
}
voidDeck:
:
RemoveCard()//将删除的那张牌清零
{
m_cardarray[nLastDelt]=PlayingCard();//清零
}
voidDeck:
:
DealOne()//从一副扑克牌中删除一张牌
{
if(nLastDelt!
=53)//51
{
nLastDelt++;
cout.width(5);
cout<cout<<"删除牌:
";
m_cardarray[nLastDelt].ShowCard();
RemoveCard();
}
else
{
cout<<"这副牌全部删除完了";
ProgClose();
}
}
Deck:
:
Deck()//初始化一副扑克牌,将52张扑克牌均清零
{
intlastdelt=0;
for(inti=0;i<=53;i++)//51
{
m_cardarray[i]=PlayingCard();
}
}
Main调用的函数.cpp
#include"class.h"
#include
usingnamespacestd;
voidProgramInit()//程序一开始的提示信息
{
chara[11]={"扑克牌游戏"};
charb[25]={"单击键开始......"};
CenterText(a);
cout<CenterText(b);
cin.get();
}
charGetKey()//从键盘接收一个字符,将其作为函数返回值
{
charx;
x=cin.get();
cout<returnx;
}
voidCenterText(charShellText[80])//居中算法
{
intnCenter;
intnLength;
nLength=strlen(ShellText);
nCenter=(80-nLength)/2;
for(;nCenter!
=0;nCenter--)
{
cout<<"";
}
cout<}
intGetNumber()//从键盘接收一个整数值,作为函数返回值
{
chara[40]={"请输入一个在0到51之间的整数,输入52退出."};
intnInputInteger=0;
CenterText(a);
cout<cin>>nInputInteger;
returnnInputInteger;
}
voidProgClose()//程序暂停,按任意键继续
{
cout<cout<<"单击键继续......"<cout<GetKey();
cin.get();
}
voidDeckMakeDiver()//主要程序操作部分,调用多个函数,包含完整生成一副牌,洗牌,显示,删除等操作
{
Deckdeck_1;
deck_1.MakeDeck();
deck_1.ShowDeck();
GetKey();
deck_1.DeckShuff(250);
deck_1.ShowDeck();
cout<chary;
PlayDeckF;
F.PlayCard(deck_1);
cout<F.Show();
intg=0;
do
{
chara[9]={"游戏规则"};
charb[23]={"删除一位玩家的牌输入1"};
charc[23]={"删除四位玩家的牌输入2"};
chard[23]={"按牌的面值排序输入3"};
chare[23]={"按牌的花色排序输入4"};
charf[23]={"显示四位玩家手牌输入5"};
charq[23]={"按N(n)退出游戏"};
CenterText(a);
cout<CenterText(b);
cout<CenterText(c);
cout<CenterText(d);
cout<CenterText(e);
cout<CenterText(f);
cout<CenterText(q);
cout<cin>>g;
if(g==1)
{
intj,k;
charh;
cout<<"请输入删除玩家的编号和牌号(从左往右数的第N张)"<cin>>j>>k;
F.DeleteOne(j,k);
cout<"<cin>>h;
h=toupper(h);
if(h=='Y')F.s(j);
cout<}
if(g==2)
{
intw,x,h,z;
chary;
cout<<"请依次输入欲删除各玩家牌的序号。
"<cin>>w>>x>>h>>z;
F.DeleteAll(w,x,h,z);
cout<"<cin>>y;
y=toupper(y);
if(y=='Y')F.Show();
cout<}
if(g==3)
{
inth;
chary;
cout<<"请输入需要排序的玩家"<cin>>h;
F.SortDeckFace(h);
cout<<"是(Y)否(N)显示排序后的牌?
"<cin>>y;
y=toupper(y);
if(y=='Y')F.s(h);
}
if(g==4)
{
intx;
chary;
cout<<"请输入需要排序玩家的序号"<cin>>x;
F.SortDeckSuit(x);
cout<<"是(Y)否(N)显示排序后的牌?
"<cin>>y;
y=toupper(y);
if(y=='Y')F.s(x);
}
if(g==5)
{
F.Show();
}
if(g==78||g==110)
{
gotoloop;
}
cout<<"是(Y)否(N)回到游戏规则菜单?
"<cout<cin>>y;
y=toupper(y);
}while(y=='Y');
loop:
;
}
intGetRandInt(intnMin,intnMax)//返回min至max之间的任意一值(实际上是“洗牌算法”s的一部分)
{
intnNumToReturn;
nNumToReturn=rand();
nNumToReturn=nNumToReturn%(nMax-nMin+1)+nMin;
returnnNumToReturn;
}
Playingcard.cpp
#include"class.h"
#include
usingnamespacestd;
PlayingCard:
:
PlayingCard()//扑克牌对象的构造函数
{
for(inti=1;i<=3;)
{
achFace[i]='';
i++;
}
chSuit='';
nValue=0;
}
voidPlayingCard:
:
ShowCard()//在屏幕上显示一张扑克牌的面值及花色
{
cout<<"";
cout<cout.width
(1);
cout<cout<<"";
}
voidPlayingCard:
:
CardMake(intnNum)//根据参数nNum生成一张扑克牌,nNum不同,生成的面值或花色不同
{
inti=0;
charj;
intnFaceNum=nNum%13;
if(nNum==52)
{
strcpy(achFace,"S");
gotoloop;
}
if(nNum==53)
{
strcpy(achFace,"B");
gotoloop;
}
switch(nFaceNum)
{
case0:
strcpy(achFace,"A");break;
case9:
strcpy(achFace,"10");break;
case10:
strcpy(achFace,"J");break;
case11:
strcpy(achFace,"Q");break;
case12:
strcpy(achFace,"K");break;
default:
j=char(nFaceNum+49);
if(i<3)
{
achFace[i]='';i++;
achFace[i]=j;i++;
achFace[i]=NUL