C language to implement the core algorithm of dou landlord
- 2020-04-02 03:00:09
- OfStack
The data structure only chooses the sequence table, does not choose the linked list, the flexibility and the abstraction is insufficient, cannot be universal.
Head. H
#ifndef __HEAD_H__
#define __HEAD_H__
#define MAXLEVEL 15
typedef struct CARD{
int number;
int level;
char *flower;
char point;
}card;//card
typedef struct DECK{
int top;
int arr[55];
}deck;//card
typedef struct PLAYERS{
int id;
int status;
card handcard[21];
int size;
}players;//The player
typedef struct GAMES{
int type;
int level;
int sum;
int who;
int count;
int arr[16];
}games;//desktop
typedef struct BUFFERS{
int arr[16];
int brr[20];
int sum;
}buffers;//Play buffer
void game_init();
void turning();
void handcard_sort();
void print();
int win();
void turn_switch();
#endif
Op. C
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
#include<string.h>
static int type_buffer();
static char point[]={'0','3','4','5','6','7','8','9','X','J','Q','K','A','2','w','W'};
static char *farr[]={" The piece of "," The plum blossom "," Red peach "," spades "};
static char* type_arr[]={" A waiver ","A single","pairs","The king of Fried","The skeleton","The skeleton single ","Fried play ","The skeleton right ","Fried With a single ","shunza","Fried With a pair of "," The plane without a ","Even for","The plane single"," The plane to take on "};
static char* sta_arr[2]={"farmers","The landlord"};
static players player[3];//The player
static games game;
static deck* deck_init(){
int i,j;
srand(time(0));
deck *p_deck=(deck*)malloc(sizeof(deck));
if(!p_deck){
printf(" Memory allocation failure n");
return NULL;
}
for(i=1;i<=54;i++){
p_deck->arr[i]=rand()%54;
for(j=1;j<i;j++){
if(p_deck->arr[i]==p_deck->arr[j]){
i--;
break;
}
}
}
p_deck->top=54;
return p_deck;
}
static void player_init(){
int i,j;
for(j=0;j<3;j++){
for(i=1;i<=20;i++){
player[j].handcard[i].number=100;
player[j].handcard[i].level =0;
}
}
deck *p=deck_init();
if(!p){
printf(" No card n");
return ;
}
int which=0;
which=rand()%3;
game.who=which;
for(i=0;i<3;i++){
player[i].id=i;
if(i==which){//The landlord
player[i].status=1;
for(j=1;j<=20;j++){
player[i].handcard[j].number=p->arr[(p->top)--];
}
player[i].size=20;
}
else{//farmers
player[i].status=0;
for(j=1;j<=17;j++){
player[i].handcard[j].number=p->arr[(p->top)--];
}
player[i].size=17;
}
}
free(p);
p=NULL;
}
static void handcard_init(){
int i,j;
for(i=0;i<3;i++){
for(j=1;j<=20;j++){
int number=player[i].handcard[j].number;
int *p_level=&(player[i].handcard[j].level);
char **pp_flower=&(player[i].handcard[j].flower);
char *p_point=&(player[i].handcard[j].point);
if(number>=0&&number<=51){
*p_level=number/4+1;
*p_point=point[number/4+1];
*pp_flower=farr[number%4];
}
else if(number==52){
*p_level=14;
*p_point='w';
*pp_flower=" wang ";
}
else if(number==53){
*p_level=15;
*p_point='W';
*pp_flower=" The king ";
}
else {
*p_level=0;
*p_point=' ';
*pp_flower=" ";
}
}
}
}
void print(){
int i,j;
for(i=0;i<3;i++){
if (i!=game.who) continue;
for(j=1;j<=player[i].size;j++){
//printf("======");
if(player[i].handcard[j].number == 100){
printf(" ");
}
else {
char *p_tmp=player[i].handcard[j].flower;
printf("%s ",p_tmp);
}
}
printf("n");
for(j=1;j<=player[i].size;j++){
if(player[i].handcard[j].number == 100){
printf(" ");
}
else {
printf(" %c ",player[i].handcard[j].point);
}
}
}
printf("n");
for(j=1;j<=player[game.who].size;j++){
if(! (j>9))
printf("(%d) ",j);
else
printf("(%d) ",j);
}
printf("n");
}
void game_init(){
game.count=2;//Waiver number
player_init();//Shuffling CARDS
handcard_init();//Hand information completed
}
int fcmp(const void *pa,const void *pb){//ascending
return *(int*)pa-*(int*)pb;
}
static void rehandcard_sort(players *p_player,int* p_number){//The real sort function
int *base=p_number;
size_t nmemb=p_player->size;
size_t size= sizeof(card);
qsort(base,20,size,fcmp);
}
void handcard_sort(){//Shell sort function
rehandcard_sort(&player[0],&(player->handcard[1].number));
rehandcard_sort(&player[1],&((player+1)->handcard[1].number));
rehandcard_sort(&player[2],&((player+2)->handcard[1].number));
}
static int require(){ //A 1 is a play and a 0 is a forfeit
if(game.type == 3 ){
if(game.count != 2){
printf(" Can't afford to to !n");
return 0;
}
else
return 1;
}
if(game.count !=2){
printf(" The card? ?(y To show the CARDS, n A waiver ):");
char choice;
scanf("%c",&choice);
scanf("%*[^n]");
scanf("%*c");
if(choice == 'n' || choice == 'N'){
return 0;
}
else return 1;
}
else {
printf(" Continue to play n");
return 1;
}
}
buffers buffer={};//Play buffer
static void reset(){
int a;
for(a=0;a<16;a++)
buffer.arr[a]=0;
for(a=0;a<20;a++)
buffer.brr[a]=0;
buffer.sum=0;
}
static void buffer_put(){
char intput[40]={};//Converts a string to a number
int brr[20]={};
int i=0;
int j;
int sum;
int flag=0;
while(1){
reset();
sig: printf(" Please enter the card you want to play ...:");
fgets(intput,40,stdin);
if(strlen(intput)==39&&intput[38]!='n'){
scanf("%*[^n]");
scanf("%*c");
}
for(j=0,i=0,sum=0;i<strlen(intput);i++){//Record the index of a card
if(intput[i]>='0'&&intput[i]<='9'){
sum=sum*10+(intput[i]-'0');
flag=1;
}
else {
if(flag)
brr[j] = sum;
sum=0;
j++;
flag=0;
}
}
int k;
printf(" To be a : ");
for(k=0;brr[k];k++)
printf("%d ",brr[k]);
printf(" plate n");
int who = game.who;
players* p_player=&(player[who]);
int index;
for(i=0;brr[i];i++){//What are the CARDS recorded
index=brr[i];
if(index>(p_player->size) || index<=0 ){//The string range entered is incorrect
printf(" Input is out of range. Retype n");
goto sig;
}
else{
int level=p_player->handcard[index].level;
++(buffer.arr[level]);
buffer.brr[i] =brr[i];
}
}
for(i=1;i<=15;i++)//How many CARDS are played
buffer.sum+=buffer.arr[i];
char aff= 'N';
int type = type_buffer();
if(type != -1)
printf(" The type to play out is :%snn",type_arr[type]);
else {
printf(" This type of card does not exist nn");
reset();
return;
}
printf(" Are you sure you want to do that ?( Determine the input y, Otherwise press other )");
scanf("%c",&aff);
scanf("%*[^n]");
scanf("%*c");
if(aff == 'y' || aff =='Y')
break;
}
}
static void turnstart(){
char u;
printf("nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn============================================================== bucket The landlord======================================================nnnnn");
printf(" Turn to the next ");
scanf("%c",&u);
int i;
printf("nnnnnnn The number of CARDS is zero %d zhang n",game.sum);
printf(" The face type is : %s%cn",type_arr[game.type],point[game.level]);
printf("=============================================================%s The round ==========================================================nn",sta_arr[player[game.who].status]);
printf(" Now it is up to The player%d",game.who+1);
printf(" The player1(%s) hand %d ",sta_arr[player[0].status],player[0].size);if(game.who==0) printf("<=====n");else printf("n");
printf(" The player2(%s) hand %d ",sta_arr[player[1].status],player[1].size);if(game.who == 1) printf("<=====n"); else printf("n");
printf(" The player3(%s) hand %d ",sta_arr[player[2].status],player[2].size);if(game.who == 2) printf("<=====n"); else printf("n");
}
static int continuum(int num,int total){
int i,count=0;
int flag=0;//If there is a value, mark it as 1
int sig=0;//From there to there is no change marked as 1
for(i=1;i<=15;i++){
if(buffer.arr[i]==num){
if(sig)
return 0;//discontinuous
count++;
if(count==total)
return 1;//continuous
flag=1;
}else {
if (flag)
sig=1;
}
}
}
static int type_buffer(){
int i, one=0,pair=0,triple=0,quattuor=0,zero=0;
for(i=1;i<=15;i++){//Statistical leaflets, pairs, three - same, four - same each number
if(buffer.arr[i] == 1)
one++;
else if(buffer.arr[i] == 2)
pair ++;
else if(buffer.arr[i] == 3)
triple ++;
else if(buffer.arr[i] == 4)
quattuor ++;
else zero++;
}
//Printf (" % d to % d d d d, zero percent forty percent thirty percent sum = = = % d n ", one of the pair, triple, quattuor, zero, buffer, sum);
if(!(buffer.sum)){
return -1;//illegal
}
else if(buffer.sum<=5){//1~5
if(one == 1 && !pair && !triple && !quattuor)//A single
return 1;
else if(pair == 1 && !one && !triple && !quattuor)//pairs
return 2;
else if(one == 2 &&buffer.arr[14]&&buffer.arr[15])//The king of Fried
return 3;
else if(triple == 1 && !one && !pair && !quattuor) //The skeleton
return 4;
else if(one ==1 && !pair && triple == 1 && !quattuor )//The skeleton single
return 5;
else if(!one && !pair && !triple && quattuor == 1)//Fried
return 6;
else if(!one && pair == 1 && triple == 1 && !quattuor)//The skeleton right
return 7;
else if(one == 1 && !pair && !triple && !quattuor){//Fried With a single
return 8;
}
else if(!pair && !triple && !quattuor && (!buffer.arr[14] && !buffer.arr[15])&& buffer.sum == 5){//shunza
if( continuum(1,one))// all 1continuous
return 9;
else {
return -1;
}
}
else return -1;
}
else if(buffer.sum>=6){
if((!one) && (pair == 1) && (!triple) && (quattuor == 1) )//Fried Take on
return 10;
else if(!one && !pair && !quattuor){//There are only two or more identical planes without three
if(continuum(3,triple))// all 3continuous
return 11;
else return -1;
}
else if(!one && !triple && !quattuor){//Even for
if(continuum(2,pair))
return 12;
else return -1;
}
else if(buffer.sum == 4*triple){//The plane single
if(continuum(3,triple))
return 13;
else return -1;
}
else if((buffer.sum == 5*triple) && (triple == pair)){//The plane to
if(continuum(3,triple))
return 14;
else return -1;
}
else if(!pair && !triple && !quattuor &&(!buffer.arr[14] && !buffer.arr[15])){
if(continuum(1,one))
return 9;
else return -1;
}
else return -1;
}
}
static int maxindex(int count){
int i;
for (i=15;i>=1;i--){
if(buffer.arr[i] == count)
return i;
}
}
static int level_buffer(int type){
switch(type){
case 1:
return maxindex(1);
break;
case 2:
return maxindex(2);
break;
case 3:
return 15;
break;
case 4:
return maxindex(3);
break;
case 5:
return maxindex(3);
break;
case 6:
return maxindex(4);
break;
case 7:
return maxindex(3);
break;
case 8:
return maxindex(4);
break;
case 9:
return maxindex(1);
break;
case 10:
return maxindex(4);
break;
case 11:
return maxindex(3);
break;
case 12:
return maxindex(2);
break;
case 13:
return maxindex(3);
break;
case 14:
return maxindex(3);
break;
}
}
static void annealation(){
int i=1;
int j=0;
int who=game.who;
for(i = 1,j=0;buffer.brr[j];i++,j++){
int index = buffer.brr[j];
player[who].handcard[index].number = 100;
player[who].size = player[who].size - 1;
}
game.sum=buffer.sum;
game.count=0;
handcard_sort();
//Printf (" card played successfully n");
}
//This module has a lot of room to change things like return to break...
void turning(){
turnstart();
handcard_sort();
print();
int level= 0;
while(1){
if(!require()){
printf("nnnnnnnn Don't n");
game.count++;
if(game.count == 2){
game.type=0;
game.level=0;
game.sum=0;
}
return ;
}
buffer_put();
int type = type_buffer();
int level=level_buffer(type);
if(type == -1){
printf(" Card type illegal!-----n");
continue;
}
if(type == 3){//The king of Fried
printf("nnnnnThe king of Fried!!n");
annealation();
game.type=3;
game.level=MAXLEVEL;
return ;
}
else if(type == 6){//Fried play
if(game.type != 6){
printf("nnnnFried play n");
annealation();
game.type = 6;
game.level = level_buffer(type);
return;
}
else {
if(level > game.level){
printf("nnnnn crushing n");
annealation();//Cut the hand
game.type = 6;
game.level = level;
return;
}
else if(level < game.level||level == game.level){
printf(" The CARDS are not big enough n");
continue;
}
}
}
else if(game.count == 2 ){//Two abstained
annealation();
game.type = type;
game.level = level;
return ;
}
else {// In addition to Fried play Two abstained The king of Fried illegal Other than the reasonable card class
if(type != game.type){ //No corresponding
printf(" type No correspondingn");
continue;
}
else { //The corresponding
if(buffer.sum != game.sum){
printf(" The number of No correspondingn");
continue;
}
if(level < game.level|| level==game.level){
printf(" The CARDS are not big enough n");
continue;
}
else if(level > game.level){
printf("nnnnn crushing n");
annealation();
game.type = type;
game.level = level;
return ;
}
}
}
}
}
int win(){
int now = game.who;
if(!player[now].size)
return now;
else return 0;
}
void turn_switch(){
int who = game.who;
who++;
game.who = who%3;
}
The main. C
#include<stdio.h>
#include"head.h"
int main(){
while(1){
int which;
game_init();//Initialize the game
while(1){
turning();//In the round
printf("nnn");
if(which=win())//Whether or not there is a winner
break;
turn_switch();//Switch sides
}
printf(" The game is up! The winner is the player %dn",which+1);
printf(" Whether to replay ?(y To continue, other exit procedures ):");
char choice;
scanf("%c",&choice);
scanf("%*[^n]");
scanf("%*c");
if(choice == 'y'||choice =='Y')
continue;
break;
}
printf(" Thank you for your demo n");
}
All of the above is the content of this article, I hope to help you proficient in the application of C language.