人机互动的出牌怎么出牌啊,求助各位大神,void cp()部分的代码
规则:一个人为用户,另一个为计算机,用户先出,轮流出牌,每次只能出一张并且要比别人大,如:用户出3,计算机要出比3大的数,没有则跳过,求出
代码:#include
#include // 产生随机数
#include
#include
#include
using namespace std;
class PlayingCards
{
private:
int card1[10][4];//标记发牌
int card2[5][2];//标记出牌
public:
int b[5];
char bhuase[5];
int c[5];
char chuase[5];
PlayingCards(){};
void yxsm(); //游戏说明
void fp();//随机发牌
void cp();//按顺序出牌,并显示
void xswj();//显示获得的牌
void xsdn();//显示获得的牌
void qk();//清空标记数组的记录
};
PlayingCards a;
typedef struct node
{
int data;
struct node * LChild;
struct node * RChild;
}node;
class tree
{private:
int data;
struct node * LChild;
struct node * RChild;
public:
void chushihua(node *t);
node * charu(node *t , int key);
node * jianlib(node *t);
node * jianlic(node *t);
void paixu1(node * t);
void paixu2(node * t,int *p);
void paixu3(node * t,int *p);
};
tree tr;
void PlayingCards::yxsm()
{
cout<<" "<<"每轮每人各发5张牌,各自以五张牌建立二叉树,由用户先出,轮流出牌,"<<endl;
cout<<" "<<"每次只能出一张并且要比别人出的大,"<<endl;
cout<<" "<<"如:用户出3,计算机则要出比3大的牌,没有则选择不出;"<<endl;
cout<<" "<<"最先出完的人获胜。"<<endl;
}
void PlayingCards::fp()
{int l,e;//临时储存随机牌数
int f;
for(int i = 0;i < 5;i++)
{
l = rand()%9 + 2;//玩家得到牌的点数
cout<<"发到的牌"<<l<<" ";
f = rand()%4+3;
while(card1[l-1][f-3] == 1)
{l = rand()%9 + 2;f= rand()%4+3;}
card1[l-1][f-3] = 1;//标记哪些牌被发出
b[i] = l ;
bhuase[i] = f;
e = rand()%9 + 2;//电脑得到牌的点数
f = rand()%4+3;
while(card1[e-1][f-3] == 1)
{e = rand()%9 + 2;f = rand()%4+3;}
card1[e-1][f-3] = 1;//标记哪些牌被发出
c[i] = e;
chuase[i] = f;
}
}
void PlayingCards::cp()
{
}
void PlayingCards::xswj()
{cout<<"玩家: "<<endl;
int i = 0;
cout<<" "<<bhuase[0]<<setw(2)<<b[0]<<" "<<" "<<bhuase[1]<<setw(2)<<b[1]<<" "<<" "<<bhuase[2]<<setw(2)<<b[2]<<" "<<" "<<bhuase[3]<<setw(2)<<b[3]<<" "<<" "<<bhuase[4]<<setw(2)<<b[4]<<" "<<endl;
}
void PlayingCards::xsdn()
{cout<<"电脑 "<<endl;
cout<<" "<<chuase[0]<<setw(2)<<c[0]<<" "<<" "<<chuase[1]<<setw(2)<<c[1]<<" "<<" "<<chuase[2]<<setw(2)<<c[2]<<" "<<" "<<chuase[3]<<setw(2)<<c[3]<<" "<<" "<<chuase[4]<<setw(2)<<c[4]<<" "<<endl;
}
void PlayingCards::qk()
{
for(int i = 0;i < 5;i++)
for(int j = 0;j < 2;j++)
{
card2[i][j] = 0;
}
for( i = 0;i < 10;i++)
for(int j = 0;j < 4;j++)
{
card1[i][j] = 0;//标记发牌
}
}
void tree::chushihua(node *t)
{
t = NULL;
}
node * tree::charu(node *t , int key)
{
if(t == NULL)
{
node * p;
p = (node *)malloc(sizeof(node));//其中(Node *)为强制转换,把返回类型void *转换为Node *,sizeof(Node)为获取Node类型占据空间的大小
p->data = key;
p->LChild = NULL;
p->RChild = NULL;
t = p;
}
else
{
if(key < t->data)
t->LChild = charu(LChild, key);
else
t->RChild = charu(t->RChild, key);
}
return t; //important!
}
node * tree::jianlib(node *t)//建立玩家的树
{
int i,key;
for(i = 0; i < 5; i++)
{
key = a.b[i];
t = charu(t, key);
}
return t;
}
node * tree::jianlic(node *t)//建立电脑的数
{
int i,key;
for(i = 0; i < 5; i++)
{
key = a.c[i];
t = charu(t, key);
}
return t;
}
void tree::paixu1(node * t) //中序遍历输出
{
if(t != NULL)
{
paixu1(t->LChild);
cout<< t->data<<" ";
paixu1(t->RChild);
}
}
void tree::paixu2(node * t,int *p)
{
if( t == NULL)
return;
else
{
paixu2(t->LChild,p);
a.b[(*p)++] = t->data;
paixu2(t->RChild,p);
}
}
void tree::paixu3(node * t,int *p)
{
if( t == NULL)
return;
else
{
paixu3(t->LChild,p);
a.c[(*p)++] = t->data;
paixu3(t->RChild,p);
}
}
int main()
{int k =0;
srand((unsigned)time(NULL));//调用系统时间为随机函数赋初值
int n = 0;
while(k != -1)
{
cout<<endl<<endl<<endl;
cout<<"\t\t\t"<<" **************纸牌游戏****************"<<endl;
cout<<" ******************************************"<<endl;
cout<<"\t\t\t"<<" *************1.游戏说明 ******** "<<endl;
cout<<"\t\t\t"<<" *************2.开始游戏********* "<<endl;
cout<<"\t\t\t"<<" *************3.开始出牌 ************* "<<endl;
cout<<"\t\t\t"<<" *************4.游戏结束 ********** "<<endl;
cout<<" *************************************"<<endl;
cout<<"\t\t\t"<<" 请输入(1、2、3、4) : "<<endl;
cin>>k;
switch(k)
{case 1: a.yxsm(); break;
case 2:
{a.qk();
a.fp();
node * t1 = NULL; t1 = tr.jianlib(t1);
tr.paixu1(t1);//中序遍历输出
node * t2 = NULL; t2 = tr.jianlic(t2);
n = 0;//初始化
tr.paixu2(t1,&n);
n = 0;
tr.paixu3(t2,&n);
cout<<endl;
a.xswj();
a.xsdn();
break;}
case 3: {
a.cp(); a.qk(); break;
}
case 4: k = -1; break;
}
}
return 0;
}