纸牌游戏—接竹竿(顺序栈和循环队列)

本文介绍了如何使用顺序栈和循环队列实现纸牌游戏—接竹竿。游戏中,栈的容量限制为14,队列的容量为52,整个解决方案涉及四个数组、一个栈、两个队列以及一个标记数组,展现了数据结构在解决实际问题中的应用。

纸牌游戏代码来了,逻辑上来说栈和队列不能采用链式结构,栈的最大容量为14,队列的最大容量为52,全题四个数组,一个栈,两个队列,一个标记数组

#include<bits/stdc++.h>
#include<stdlib.h>
#define MAXSIZE 55
#define SMAXSIZE 15
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef int ElemType;
typedef int Status;
typedef struct{
	ElemType *base;
	ElemType *top;
	int StackSize;
	
}SqStack;

Status InitStack(SqStack &S){
	S.base=(ElemType *)malloc(SMAXSIZE*sizeof(ElemType));
	if(!S.base){
		return OVERFLOW;
	}
	S.top=S.base;
	S.StackSize=SMAXSIZE;
	return OK;
}

Status Push(SqStack &S,ElemType e){
	*S.top++=e;
	return OK;
}

Status Pop(SqStack &S,ElemType &e){
	e=*--S.top;
	return OK;
}

void PrintStack(SqStack S)
{
    int *i;
    if( S.top == S.base )
    {
        printf("空栈!\n");
    }
    else
    {
        for(i=S.base; i<S.top; i++)
        {
            printf("%d ",*i);
        }
 
        printf("\n");
    }
}


typedef struct{
	ElemType *base;
	int front;
	int rear;
}SqQueue;


Status InitQueue(SqQueue &Q){
	Q.base=new ElemType[MAXSIZE];
	if(!Q.base) exit(OVERFLOW);
	Q.front=Q.rear=0;
	return OK;
}


Status EnQueue(SqQueue &Q,ElemType e){
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%MAXSIZE;
	return OK;
}

Status OutQueue(SqQueue &Q,ElemType &e){
	e=Q.base[Q.front];
	Q.front=(Q.front+1)%MAXSIZE;
	return OK;
}

SqStack S;
SqQueue Q1,Q2;
void FAPAI(){

	InitStack(S);
	InitQueue(Q1);
	InitQueue(Q2);
	int value[14];
	for(int i=1;i<=13;i++){
		value[i]=4;
	}
	srand((unsigned int)time(NULL));
	for(int i=0;i<26;i++){
		int ret = (rand()%13)+1;
		if(value[ret]!=0){
			EnQueue(Q1,ret);
			value[ret]--;
			printf("%d ",ret);
		}
		else{
			i--;
		}
	}
	printf("\n");
	for(int i=0;i<26;i++){
		int ret = (rand()%13)+1;
		if(value[ret]!=0){
			EnQueue(Q2,ret);
			value[ret]--;
			printf("%d ",ret);
		}
		else{
			i--;
		}
	}
	printf("\n");
}
bool LastV[15];
int main(){
	
	FAPAI();
	ElemType temp=-1,temp2=-1;
	int a=26,b=26;
	while(1){
		OutQueue(Q1,temp);
		Push(S,temp);
		if(LastV[temp]==true){
			Pop(S,temp);
			EnQueue(Q1,temp);
			while(temp2!=temp){
				Pop(S,temp2);
				EnQueue(Q1,temp2);			
				LastV[temp2]=false;
				a=a+1;
			}
		}
		else{
			LastV[temp]=true;
			a=a-1;
		}
		PrintStack(S);
		if(a==0){
			printf("A输",a);
			break;
		} 
		
		
		temp=-1,temp2=-1;
		OutQueue(Q2,temp);
		Push(S,temp);
		if(LastV[temp]==true){
			Pop(S,temp);
			EnQueue(Q2,temp);			
			while(temp2!=temp){
				Pop(S,temp2);
				EnQueue(Q2,temp2);
				LastV[temp2]=false;
				b=b+1;
			}
		}
		else{
			LastV[temp]=true;
			b=b-1;
		}
		PrintStack(S);
		if(b==0){
			printf("B输",b);
			break;
		}
	}	
	
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值