纸牌游戏—数据结构实训代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
纸牌游戏——数据结构实训代码
实训要求:
基本要求:
一副没有花牌(J、Q、K、A、大小王)的扑克牌,两个人进行纸牌游戏,其中一个人为用户,另一个人为计算机;
每轮每人各发 5 张牌,各自以这 5 张牌建立二叉排序树;
由用户先出,轮流出牌,每次只能出一张并且要比别人出的大,如:用户出3,计算机则要出比 3 大的牌,没有则选择不出;
最先出完的人获胜。
#include <iostream>
#include<stdlib.h> // 产生随机数
#include<time.h>
#include <Windows.h>
#include <iomanip>
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<<" cout<<" cout<<" } "<<"每次只能出一张并且要比别人出的大,"<<endl;
"<<"如:用户出3,计算机则要出比3 大的牌,没有则选择不出;"<<endl; "<<"最先出完的人获胜。
"<<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()
{ int e = 0; int f = 0;
int n1 = 0;int n2 = 0;//记录牌打出几张
for(int m = 0;m < 5;m++)
{
for(int i = 0;i < 5;i++)
{
for(int j = 0;j < 5;j++)
{
if(b[j] > f&&card2[j][0] == 0)//有牌且比电脑的大
{
cout<<"玩家出牌"<<bhuase[j]<<b[j]<<endl;
card2[j][0] = 1; //标记b(玩家)的第i 张牌已经打出
e = b[j]; Sleep(800);
break;
}
}
n1 = 0;n2 = 0;
for(int i = 0;i < 5; i++)
{
n1 = n1 + card2[i][0];//玩家出牌总数
n2 = n2 + card2[i][1];//电脑出牌总数
}
if(n1 == 5)
{cout<<"玩家赢了";
break;
}
for( j = 0;j < 5;j++)
{
if(c[j] > e && card2[j][1] == 0)
{
cout<<"电脑出牌"<<chuase[j]<<c[j]<<endl;
card2[j][1] = 1;f = c[j]; Sleep(800);//把密码转换成ANSCII 码
break;
}
}
n1 = 0;n2 = 0;
for( i = 0;i < 5; i++)
{
n1 = n1 + card2[i][0];//玩家出牌总数
n2 = n2 + card2[i][1];//电脑出牌总数
}
if(n2 == 5)
{cout<<"电脑赢了";
break;
}
}
n1 = 0;n2 = 0;
for( i = 0;i < 5; i++)
{
n1 = n1 + card2[i][0];
n2 = n2 + card2[i][1];
}
if(n1 == 5 || n2 == 5)
break;
if(f > e)
{e = 0;
}
if(e > f)
{f = 0;
}//有一方没牌可出了, 由牌点数大的那一方继续出牌
}
}
void PlayingCards::xswj()
{cout<<"玩家: "<<endl;
int i = 0;
cout<<"
"<<bhuase[1]<<setw(2)<<b[1]<<" "<<bhuase[0]<<setw(2)<<b[0]<<"
"<<" "<<bhuase[2]<<setw(2)<<b[2]<<"
"<<"
"<<"
"<<bhuase[3]<<setw(2)<<b[3]<<"
"<<endl;
"<<" "<<bhuase[4]<<setw(2)<<b[4]<<" }
void PlayingCards::xsdn()
{cout<<"电脑"<<endl;
cout<<"
"<<chuase[1]<<setw(2)<<c[1]<<" "<<chuase[3]<<setw(2)<<c[3]<<" "<<chuase[0]<<setw(2)<<c[0]<<"
"<<" "<<chuase[2]<<setw(2)<<c[2]<<"
"<<" "<<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.游戏说明******** cout<<"\t\t\t"<<" *************2.开始游戏********* "<<endl; "<<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;
}。