纸牌游戏代码来了,逻辑上来说栈和队列不能采用链式结构,栈的最大容量为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;
}
}
}

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

被折叠的 条评论
为什么被折叠?



