⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 puke.c

📁 一个模拟扑克比大小的c语言程序.共四个人,随机分拍,比大小
💻 C
字号:
/***************************
作者:林宝尉
学号:计031_03
功能:用堆栈完成四人扑克对战
时间:2006.10.15  0:03
***************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#include <time.h>
#define CURD_SIZE 52
typedef struct node 
{ 
	char data; 
	struct node *next; 
}stackNode,stackNode1,stackNode2,stackNode3,stackNode4; 

void push(char pude,stackNode **stack);
char pop(stackNode **stack);
int isempty(stackNode *stack);

void push1(char pude,stackNode1 **stack);
char pop1(stackNode1 **stack);
int isempty1(stackNode1 *stack);

void push2(char pude,stackNode2 **stack);
char pop2(stackNode2 **stack);
int isempty2(stackNode2 *stack);

void push3(char pude,stackNode3 **stack);
char pop3(stackNode3 **stack);
int isempty3(stackNode3 *stack);

void push4(char pude,stackNode4 **stack);
char pop4(stackNode4 **stack);
int isempty4(stackNode4 *stack);

int ToArray(int Recurd[]);
void ToPush(int p,char pukePlayers);
void ToPop();
void ToSendCurd();
int Tocompare();

stackNode *stack=NULL;
stackNode1 *stack1=NULL;
stackNode2 *stack2=NULL;
stackNode3 *stack3=NULL;
stackNode4 *stack4=NULL;
char pukePlayer1,pukePlayer2,pukePlayer3,pukePlayer4;
char GetPuke1_1[13],GetPuke1_2[13],GetPuke2_1[13],GetPuke2_2[13],GetPuke3_1[13],GetPuke3_2[13],GetPuke4_1[13],GetPuke4_2[13];
char getCompare[2]="";
int main()
{

	int i,j,m,n;
	int p=1;
	char puke1,puke2;	
	int curd[CURD_SIZE],Recurd[CURD_SIZE];	
	srand((unsigned)time(NULL));
	printf("\n                                  PLAY CURDS\n");
	printf("\n            ******************************************************\n");
	for(i=0;i<CURD_SIZE;i++)//得到随机数
	{
		curd[i]=rand();
	}		
	for(i=0;i<CURD_SIZE;i++)//复制
	{
		Recurd[i]=curd[i];
	}
	ToArray (Recurd);//给Recurd排列
	for(i=0;i<CURD_SIZE;i++)//将Recurd整理后存入堆栈
	{
		for(j=0;j<CURD_SIZE;j++)
		{
			if(curd[i]==Recurd[j])
			{				
				if((j+1)<13)
				{
					m=0;
				}
				if(((j+1)>13)&&((j+1)<=26))
				{
					m=1;
				}
				if(((j+1)>26)&&((j+1)<=39))
				{
					m=2;
				}
				if((j+1)>39)
				{
					m=3;
				}
				
				n=(j+1)%13;
				switch(m)
				{
				case 0:puke1='S';break;
				case 1:puke1='H';break;
				case 2:puke1='D';break;
				case 3:puke1='C';break;
				default:puke1=' ';
				}
				switch(n)
				{
				case 1:puke2='A';break;
				case 2:puke2='2';break;
				case 3:puke2='3';break;
				case 4:puke2='4';break;
				case 5:puke2='5';break;
				case 6:puke2='6';break;
				case 7:puke2='7';break;
				case 8:puke2='8';break;
				case 9:puke2='9';break;
				case 10:puke2='+';break;
				case 11:puke2='J';break;
				case 12:puke2='Q';break;
				case 0:puke2='K';break;
				default:puke2=' ';break;
				}
				push(puke2,&stack);
				push(puke1,&stack);
				continue;
			}
		}
	}

	while(!isempty(stack))//把数据从堆栈中取出
	{
		char pukePlayers=pop(&stack);
		ToPush(p,pukePlayers);	
		p++;
	}
	ToPop();
	return 0;
}

void ToPop()
{
	int o;
	while(!isempty(stack1))//把数据从堆栈中取出
	{
		for(o=0;o<13;o++)
		{
			GetPuke1_1[o]=pop(&stack1);
			GetPuke1_2[o]=pop(&stack1);
		}
	}	
	while(!isempty(stack2))//把数据从堆栈中取出
	{
		for(o=0;o<13;o++)
		{
			GetPuke2_1[o]=pop(&stack2);
			GetPuke2_2[o]=pop(&stack2);
		}
	}
	while(!isempty(stack3))//把数据从堆栈中取出
	{
		for(o=0;o<13;o++)
		{
			GetPuke3_1[o]=pop(&stack3);
			GetPuke3_2[o]=pop(&stack3);
		}
	}
	while(!isempty(stack4))//把数据从堆栈中取出
	{
		for(o=0;o<13;o++)
		{
			GetPuke4_1[o]=pop(&stack4);
			GetPuke4_2[o]=pop(&stack4);
		}
	}
	ToSendCurd();
}

void ToSendCurd()
{
	int o,i,j,t;
	int gett[4]={0};
	int gett1[4]={0};
	char trycurd[2]="10";
	char get[8];
	printf("            * player1----player2----player3----player4----winner *\n");
	for(o=0;o<13;o++)
	{
		get[0]=GetPuke1_1[o];get[1]=GetPuke1_2[o];get[2]=GetPuke2_1[o];get[3]=GetPuke2_2[o];
		get[4]=GetPuke3_1[o];get[5]=GetPuke3_2[o];get[6]=GetPuke4_1[o];get[7]=GetPuke4_2[o];
		Tocompare(GetPuke1_1[o],GetPuke1_2[o],GetPuke2_1[o],GetPuke2_2[o],GetPuke3_1[o],GetPuke3_2[o],GetPuke4_1[o],GetPuke4_2[o]);
		//printf("%c%c\n",getCompare[0],getCompare[1]);
		if(GetPuke1_2[o]=='+')//youwenti 
		{	
			printf("            *  %c%c%c        %c %c        %c %c        %c %c",GetPuke1_1[o],trycurd[0],trycurd[1],GetPuke2_1[o],GetPuke2_2[o],GetPuke3_1[o],GetPuke3_2[o],GetPuke4_1[o],GetPuke4_2[o]);
			for(i=0;i<4;i++)
			{
				if((getCompare[0]==get[(2*i)])&&(getCompare[1]==get[(2*i)+1]))
				{
					j=i+1;
					printf("      player%d *\n",j);
					gett[i]+=1;
				}
			}
			continue;
		}
		if(GetPuke2_2[o]=='+')
		{			
			printf("            *  %c %c        %c%c%c        %c %c        %c %c",GetPuke1_1[o],GetPuke1_2[o],GetPuke2_1[o],trycurd[0],trycurd[1],GetPuke3_1[o],GetPuke3_2[o],GetPuke4_1[o],GetPuke4_2[o]);
			for(i=0;i<4;i++)
			{
				if((getCompare[0]==get[(2*i)])&&(getCompare[1]==get[(2*i)+1]))
				{
					j=i+1;
					printf("      player%d *\n",j);					
					gett[i]+=1;
				}
			}
			continue;
		}
		if(GetPuke3_2[o]=='+')
		{	
			printf("            *  %c %c        %c %c        %c%c%c        %c %c",GetPuke1_1[o],GetPuke1_2[o],GetPuke2_1[o],GetPuke2_2[o],GetPuke3_1[o],trycurd[0],trycurd[1],GetPuke4_1[o],GetPuke4_2[o]);
			for(i=0;i<4;i++)
			{
				if((getCompare[0]==get[(2*i)])&&(getCompare[1]==get[(2*i)+1]))
				{
					j=i+1;
					printf("      player%d *\n",j);
					gett[i]+=1;
				}
			}
			continue;
		}
		if(GetPuke4_2[o]=='+')
		{			
			printf("            *  %c %c        %c %c        %c %c        %c%c%c",GetPuke1_1[o],GetPuke1_2[o],GetPuke2_1[o],GetPuke2_2[o],GetPuke3_1[o],GetPuke3_2[o],GetPuke4_1[o],trycurd[0],trycurd[1]);
			for(i=0;i<4;i++)
			{
				if((getCompare[0]==get[(2*i)])&&(getCompare[1]==get[(2*i)+1]))
				{
					j=i+1;
					printf("      player%d *\n",j);
					gett[i]+=1;
				}
			}
			continue;
		}
		else		
			printf("            *  %c %c        %c %c        %c %c        %c %c",GetPuke1_1[o],GetPuke1_2[o],GetPuke2_1[o],GetPuke2_2[o],GetPuke3_1[o],GetPuke3_2[o],GetPuke4_1[o],GetPuke4_2[o]);
		for(i=0;i<4;i++)
		{
		    if((getCompare[0]==get[(2*i)])&&(getCompare[1]==get[(2*i)+1]))
			{
				j=i+1;
				printf("      player%d *\n",j);
				gett[i]+=1;
			}
		}		
	}	
	for(i=0;i<4;i++)
	{
		gett1[i]=gett[i];
	}
	for(i=0;i<4;i++)
	{
		for(j=0;j<4-i-1;j++)
		{
			if(gett1[j]>gett1[j+1])
			{
				t=gett1[j];
				gett1[j]=gett1[j+1];
				gett1[j+1]=t;
			}
		}
	}

		for(j=0;j<4;j++)
		{
			if(gett1[3]==gett[j])
			{
				j++;
				printf("            * ---> The Tatol Winner Is:player%d,Win %d Times.      *\n",j,gett1[3]);
			}
		}
	
	printf("            ******************************************************\n\n\n\n\n");
}

int Tocompare(char P1_1,char P1_2,char P2_1,char P2_2,char P3_1,char P3_2,char P4_1,char P4_2)
{
	int o,x,num[4],numm[16];
	char po[4];
	char pt[4];
	char com,comm;
	int sumpo[4]={0};
	int sumpt[4]={0};
	int allsum[4]={0};
	int t;
	int i,j;
	po[0]=P1_1;
	pt[0]=P1_2;
	po[1]=P2_1;
	pt[1]=P2_2;
	po[2]=P3_1;
	pt[2]=P3_2;
	po[3]=P4_1;
	pt[3]=P4_2;
	
	
	//printf("%c%c%c%c--%c%c%c%c\n",po[0],po[1],po[2],po[3],pt[0],pt[1],pt[2],pt[3]);
	for(x=0;x<4;x++)
	{
		com=pt[x];
		switch(com)
		{		
		case '2':sumpt[x]+=1;break;
		case '3':sumpt[x]+=2;break;
		case '4':sumpt[x]+=3;break;
		case '5':sumpt[x]+=4;break;
		case '6':sumpt[x]+=5;break;
		case '7':sumpt[x]+=6;break;
		case '8':sumpt[x]+=7;break;
		case '9':sumpt[x]+=8;break;
		case '+':sumpt[x]+=9;break;
		case 'J':sumpt[x]+=10;break;
		case 'Q':sumpt[x]+=11;break;
		case 'K':sumpt[x]+=12;break;
		case 'A':sumpt[x]+=13;break;
		default:break;
		}
	}
	for(i=0;i<4;i++)
	{
		for(j=0;j<4-i-1;j++)
		{
			if(sumpt[j]>sumpt[j+1])
			{
				t=sumpt[j];
				com=po[j];
				comm=pt[j];
				sumpt[j]=sumpt[j+1];
				po[j]=po[j+1];
				pt[j]=pt[j+1];
				sumpt[j+1]=t;
				po[j+1]=com;
				pt[j+1]=comm;
			}
		}
	}
	if(sumpt[3]>sumpt[2])
	{
		getCompare[0]=po[3];
		getCompare[1]=pt[3];
	}
	if(sumpt[3]==sumpt[2])
	{
		for(x=0;x<4;x++)
		{			
			com=po[x];
			switch(com)
			{		
			case 'C':sumpo[x]+=100;break;
			case 'D':sumpo[x]+=200;break;
			case 'H':sumpo[x]+=300;break;
			case 'S':sumpo[x]+=400;break;
			default:break;	
			}
		}
		if(sumpo[3]>sumpo[2])
		{
			getCompare[0]=po[3];
			getCompare[1]=pt[3];
		}
		if(sumpo[2]>sumpo[3])
		{
			getCompare[0]=po[2];
			getCompare[1]=pt[2];
		}
	}
	if(sumpt[3]==sumpt[1])
	{
		for(x=0;x<4;x++)
		{			
			com=po[x];
			switch(com)
			{		
			case 'C':sumpo[x]+=100;break;
			case 'D':sumpo[x]+=200;break;
			case 'H':sumpo[x]+=300;break;
			case 'S':sumpo[x]+=400;break;
			default:break;	
			}
		}
		if(sumpo[3]>sumpo[2]&&sumpo[3]>sumpo[1])
		{
			getCompare[0]=po[3];
			getCompare[1]=pt[3];
		}
		if(sumpo[2]>sumpo[3]&&sumpo[2]>sumpo[1])
		{
			getCompare[0]=po[2];
			getCompare[1]=pt[2];
		}
		if(sumpo[1]>sumpo[3]&&sumpo[1]>sumpo[2])
		{
			getCompare[0]=po[1];
			getCompare[1]=pt[1];
		}
	}
	if(sumpt[3]==sumpt[0])
	{
		for(x=0;x<4;x++)
		{			
			com=po[x];
			switch(com)
			{		
			case 'C':sumpo[x]+=100;break;
			case 'D':sumpo[x]+=200;break;
			case 'H':sumpo[x]+=300;break;
			case 'S':sumpo[x]+=400;break;
			default:break;	
			}
		}
		for(x=0;x<4;x++)
		{
			allsum[x]=sumpo[x]+sumpt[x];
		}
		for(i=0;i<4;i++)
		{
			for(j=0;j<4-i-1;j++)
			{
				if(allsum[j]>allsum[j+1])
				{
					t=allsum[j];
					com=po[j];
					comm=pt[j];
					allsum[j]=allsum[j+1];
					po[j]=po[j+1];
					pt[j]=pt[j+1];
					allsum[j+1]=t;
					po[j+1]=com;
					pt[j+1]=comm;
				}
			}
		}
		getCompare[0]=po[3];
		getCompare[1]=pt[3];
	}
	return 0;
}

void ToPush(int p,char pukePlayers)
{
	char pukePlayer_1;
	char pukePlayer_2;
	char pukePlayer_3;
	char pukePlayer_4;
		if(p%8==1)
		{
			pukePlayer1=pukePlayers;
		}
		if(p%8==2)
		{
			pukePlayer_1=pukePlayers;
			push1(pukePlayer_1,&stack1);
			push1(pukePlayer1,&stack1);
		}
		if(p%8==3)
		{
			pukePlayer2=pukePlayers;
		}
		if(p%8==4)
		{
			pukePlayer_2=pukePlayers;
			push1(pukePlayer_2,&stack2);
			push1(pukePlayer2,&stack2);
		}
		if(p%8==5)
		{
			pukePlayer3=pukePlayers;
		}
		if(p%8==6)
		{
			pukePlayer_3=pukePlayers;
			push1(pukePlayer_3,&stack3);
			push1(pukePlayer3,&stack3);
		}
		if(p%8==7)
		{
			pukePlayer4=pukePlayers;
		}
		if(p%8==0)
		{
			pukePlayer_4=pukePlayers;
			push1(pukePlayer_4,&stack4);
			push1(pukePlayer4,&stack4);
		}
}
int ToArray(int Recurd[])
{
	int t;
	int i,j;
	for(i=0;i<CURD_SIZE;i++)
	{
		for(j=0;j<CURD_SIZE-i-1;j++)
		{
			if(Recurd[j]>Recurd[j+1])
			{
				t=Recurd[j];
				Recurd[j]=Recurd[j+1];
				Recurd[j+1]=t;
			}
		}
	}
	for(i=0;i<CURD_SIZE;i++)
	{
		if(Recurd[i]==Recurd[i+1])
		{
			Recurd[i+1]+=1;
		}
	}
	return Recurd;
}




void push(char pude,stackNode **stack)
{
	stackNode *newstack;
	newstack=(stackNode*)malloc(sizeof(stackNode));
	newstack->data=pude;
	newstack->next=*stack;
	*stack=newstack;
}

char pop(stackNode **stack)
{
	stackNode *oldstack;
	char pude;
	pude=' ';
	oldstack=*stack;
	pude=(*stack)->data;
	*stack=(*stack)->next;
	free(oldstack);
	return pude;
}

int isempty(stackNode *stack)
{
	return (stack==NULL);
}

void push1(char pude,stackNode1 **stack)
{
	stackNode *newstack;
	newstack=(stackNode*)malloc(sizeof(stackNode));
	newstack->data=pude;
	newstack->next=*stack;
	*stack=newstack;
}

char pop1(stackNode1 **stack)
{
	stackNode *oldstack;
	char pude;
	pude=' ';
	oldstack=*stack;
	pude=(*stack)->data;
	*stack=(*stack)->next;
	free(oldstack);
	return pude;
}

int isempty1(stackNode1 *stack)
{
	return (stack==NULL);
}

void push2(char pude,stackNode2 **stack)
{
	stackNode *newstack;
	newstack=(stackNode*)malloc(sizeof(stackNode));
	newstack->data=pude;
	newstack->next=*stack;
	*stack=newstack;
}

char pop2(stackNode2 **stack)
{
	stackNode *oldstack;
	char pude;
	pude=' ';
	oldstack=*stack;
	pude=(*stack)->data;
	*stack=(*stack)->next;
	free(oldstack);
	return pude;
}

int isempty2(stackNode2 *stack)
{
	return (stack==NULL);
}

void push3(char pude,stackNode3 **stack)
{
	stackNode *newstack;
	newstack=(stackNode*)malloc(sizeof(stackNode));
	newstack->data=pude;
	newstack->next=*stack;
	*stack=newstack;
}

char pop3(stackNode3 **stack)
{
	stackNode *oldstack;
	char pude;
	pude=' ';
	oldstack=*stack;
	pude=(*stack)->data;
	*stack=(*stack)->next;
	free(oldstack);
	return pude;
}

int isempty3(stackNode3 *stack)
{
	return (stack==NULL);
}

void push4(char pude,stackNode4 **stack)
{
	stackNode *newstack;
	newstack=(stackNode*)malloc(sizeof(stackNode));
	newstack->data=pude;
	newstack->next=*stack;
	*stack=newstack;
}

char pop4(stackNode4 **stack)
{
	stackNode *oldstack;
	char pude;
	pude=' ';
	oldstack=*stack;
	pude=(*stack)->data;
	*stack=(*stack)->next;
	free(oldstack);
	return pude;
}

int isempty4(stackNode4 *stack)
{
	return (stack==NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -