纸牌游戏—数据结构实训代码

2020-04-21 06:22

纸牌游戏——数据结构实训代码

实训要求:

题目十三、纸牌游戏 基本要求:

? 一副没有花牌(J、Q、K、A、大小王)的扑克牌,两个人进行纸牌游戏,其中一

个人为用户,另一个人为计算机;

? 每轮每人各发5张牌,各自以这5张牌建立二叉排序树;

? 由用户先出,轮流出牌,每次只能出一张并且要比别人出的大,如:用户出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张牌,各自以五张牌建立二叉树,由用户先出,轮流出牌,\ cout<<\每次只能出一张并且要比别人出的大,\

cout<<\如:用户出3,计算机则要出比3大的牌,没有则选择不出;\ cout<<\最先出完的人获胜。\}

void PlayingCards::fp()

{int l,e;//临时储存随机牌数 int f;

for(int i = 0;i < 5;i++) {

l = rand()%9 + 2;//玩家得到牌的点数 cout<<\发到的牌\ 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<<\玩家出牌\ 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<<\电脑出牌\ 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<<\玩家: \int i = 0;

cout<<\<

<

void PlayingCards::xsdn() {cout<<\电脑 \

cout<<\

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)


纸牌游戏—数据结构实训代码.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《陈情表》知识点(2)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: