信息对抗小游戏编程代码
#include<iostream.h>#include<math.h>
#include<stdlib.h>l/定义杂项函数及内存分配函数#include<time.h>ll定义关于时间的函数
int flag;
class CCard
static int nloop;/l出牌的次数,游戏结束时给出的总的出牌次数int nscore;l/总分
int nwin,nlose;
static int ndraw;ll赢,输,平的次数int nbet;l/赌注
public:
int ncard1,ncard2; l/有2张牌int bet;
CCard();l/构造函数
void setcard();!/随机产生2张牌
int addbet(int); l/增加赌注,失败返回0免费游戏代码大全
friend void compare(CCard &,CCard &);//友元函数,比较牌点的大小并显示结果friend voidshowresult(CCard &,CCard &);//友元函数,输出输赢结果及最后的积分};
int CCard::ndraw=O;
int CCard;:nloop=0;CCard::CCard(){
nscore=26;
bet=nscore;nwin=O;nlose=O;
nbet=O;}
void CCard:setcard(){
l/srand(time(NULL));ncard1=(rand()%13+2);ncard2=(rand()%13+2);
}
int CCard::addbet(int a)
{
nbet=a;
if(nbet>nscore][nbet<1)
{
return 0;
}
return 1;
}
void compare(CCard &user,CCard &computer)
{
if((ard2)>(ard2))
{
user.nscore+=computer.nbet;
computer.nscore-=computer.nbet;user.nloop++;
computer.nloop++;
user.nwin++;computer.nlose++;user.bet=user.nscore;
computer.bet=computer.nscore;if(computer.nscore<=O)
{
showresult(user,computer);
return;
}
cout<<"这局玩家赢"<<endl;}
else if((ard2)<(ard2)){computer.nscore+=user.nbet;
user.nscore-=user.nbet;user.nloop++;
computer.nloop++;computer.nwin++;user.nlose++;
user.bet=user.nscore;
computer.bet=computer.nscore;if(user.nscore<=O)
{
showresult(user,computer);return;
}
cout<<"这局计算机赢"<<endl;
}
else
{
cout<<"和局"<<endl;
user.nloop++;
computer.ndraw++;
}
void showresult(CCard& user,CCard& computer)
{
cout<<"---------------游戏结束!结果:----------"<<endl;
if(user.nscore<=O) cout<<"计算机胜出,玩家要加油咯!"<<endl;
else cout<<"恭喜玩家胜出,再接再厉"'<<endl;
cout<<"出牌数:"<<user.nloop<<endl;
cout<<"玩家积分:"<<user.nscore<<endl;cout<<"玩家胜的次数: "<<user.nwin<<endl;cout<<"玩家负的次数: "<<user.nlose<<endl;cout<<"玩家平的次数: "<<user.ndraw<<endl;cout<<"计算机积分: "<<computer.nscore<<endl;
cout<<"计算机胜的次数: "<<computer.nwin<<endl;
cout<<"计算机负的次数: "<<computer.nlose<<endl;
cout<<"计算机平的次数: "<<computer.ndraw<<endl;
cout<<"-
"<<endl;
flag=1;
user.nloop=0;
user.ndraw=O;
cout<<"游戏结束,按任意键退出"<<endl; system("pause");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论