程序如下:
#include<iostream.h>
#include<stdio.h>
#include<assert.h>
#include<conio.h>
#include<stdlib.h>
class wanjia
{
public:
wanjia()//默认构造函数
{
a=new int[21];
for(int j=0;j<21;j++)
{
a[j]=0;
}
s=0;
w=0;
}
~wanjia()//析构函数
{
delete []a;
}
void fuzhi()
{
if(w==0)
{
a[w]=rand()%21+1;
}
else
{
a[w]=a[w-1]+rand()%21;
}
}
int wansum()
{
return a[w];
}
void jishu()//记录下注了多少把
{
w=w+1;
}
void show()//显示已经下的点
{
for(int i=0;i<=w;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void showsum()//显示总共的点数
{
cout<<a[w]<<endl;
}
bool operator >(const wanjia &other)
{
if(s>other.s)
{
return true;
}
else
return false;
}
bool operator <(const wanjia &other)
{
if(s<other.s)
{
return true;
}
else
return false;
}
bool operator ==(const wanjia &other)
{
if(s==other.s)
{
return true;
}
else
return false;
}
private:
int *a;
int w;
int s;
};
void main(void)
{
while(1)
{
cout<<"是否开始21点游戏?"<<endl;
cout<<"y or n:"<<endl;
if(getche()=='y'||getche()=='Y')
{
cout<<"游戏开始"<<endl;
}
else
{
system("pause");
}
wanjia wanjia1;
wanjia computer;
while(1)
{
wanjia1.fuzhi();
computer.fuzhi();
cout<<"庄稼:";
computer.show();
cout<<"玩家:";
wanjia1.show();
cout<<"庄家的总点数为:";
computer.showsum();
cout<<"玩家的总点数为:";
wanjia1.showsum();
if((computer.wansum()!=21&&wanjia1.wansum()==21)||(computer.wansum()>21)&&(wanjia1.wansum()<=21))
{
cout<<"玩家赢!"<<endl;
cout<<"是否再玩一次?"<<endl;
if(getche()=='y'||getche()=='Y')
{
break;
}
else
{
exit(1);
}
}
else if((computer.wansum()==21&&wanjia1.wansum()!=21)||(computer.wansum()<=21&&wanjia1.wansum()>21))
{
cout<<"庄家赢!"<<endl;
cout<<"是否再玩一次?"<<endl;
if(getche()=='y'||getche()=='Y')
{
break;
}
else
{
exit(1);
}
}
else if(computer.wansum()<21&&wanjia1.wansum()<21)
{
cout<<"是否接着尝试?"<<endl;
if(getche()=='y'||getche()=='Y')
{
wanjia1.jishu();
computer.jishu();
continue;
}
else
{
break;
}
}
else
{
cout<<"游戏结束!"<<endl;
cout<<"是否再玩一次?"<<endl;
if(getche()=='y'||getche()=='Y')
{
break;
}
else
{
exit(1);
}
}
}
cout<<endl;
cout<<"是否确认再玩一次游戏?"<<endl;
if(getche()=='y'||getche()=='Y')
{
continue;
}
else
{
break;
}
}
}
改程序的随即函数每次运行的时候总是那几个数 请各位高手指教一下怎么用随即函数 好象用到<time.h>但是我不知道为什么
请指点指点小弟吧 另外给这个小程序一点评价 第一次做 希望能得到各位高手的指点和赐教了 谢谢大家了