C language implements the cat fishing game

  • 2020-06-15 09:47:18
  • OfStack

The example of this article shares the C language to achieve the cat fishing game specific code for your reference, the specific content is as follows


#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
 
 
typedef struct
{
 int data[3600];
 int col[3600];
 int top;
} stack;
 
typedef struct
{
 int data[3600];
 int col[3600];
 int head;
 int tail;
} queue;
 
queue p1, p2;
stack table;
 
void printmenu();
void deal();
void printp1();
void printtable();
void printzzz();
void judge(int a, int player);
void game();
void ask();
 
int main()
{
 while(1)
 {
  char ch;
  printmenu();
  scanf("%c",&ch);
  switch(ch)
  {
  case '1':
   game();
   break;
 
  case '2':
   exit(0);
 
  default:
   system("cls");
   break;
  }
 }
 
 return 0;
}
 
void printmenu()
{
 printf("*****************************\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*    Gold fish    *\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*****************************\n");
 printf("\n\n\n");
 printf(" Game rules: \n");
 printf(" Each take 1 Half a card, and then you put it 1 CARDS, \n");
 printf(" He put 1 A card, such as the one above 1 The sample, \n");
 printf(" You can eat all the CARDS in the middle ,\n");
 printf(" Until the opponent has no CARDS. \n\n\n");
 printf(" The input 1 Start the game \n");
 printf(" The input 2 Exit the program \n");
}
 
void deal()
{
 p1.head = 1;
 p1.tail = 1;
 p2.head = 1;
 p2.tail = 1;
 table.top = 0;
 int i, card, book[60];
 srand((unsigned)time(0));
 memset(book, 0, sizeof(book));
 for(i = 1; i <= 27;)
 {
  card = rand()%55;
  if(card == 0)
   continue;
  else if(card >= 1 && card <= 13 && book[card] == 0)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 1;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 14 && card <= 26 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 13;
   p1.col[p1.tail] = 2;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 27 && card <= 39 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 26;
   p1.col[p1.tail] = 3;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 40 && card <= 52 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 39;
   p1.col[p1.tail] = 4;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 53)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 5;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 54)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 5;
   p1.tail++;
   book[card]=1;
   i++;
  }
 }
 for(i = 1; i <= 27;)
 {
  card = rand()%55;
  if(card == 0)
   continue;
  else if(card >= 1 && card <= 13 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 1;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 14 && card <= 26 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 13;
   p2.col[p2.tail] = 2;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 27 && card <= 39 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 26;
   p2.col[p2.tail] = 3;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 40 && card <= 52 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 39;
   p2.col[p2.tail] = 4;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 53 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 5;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 54 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 5;
   p2.tail++;
   book[card]=1;
   i++;
  }
 }
 
 return;
}
 
void printp1()
{
 int i;
 printf(" You have : \n");
 for(i = p1.tail-1; i >= p1.head; i--)
 {
  if(p1.col[i] == 1)
  {
   printf(" The piece of ");
  }
  else if(p1.col[i] == 2)
  {
   printf(" Red peach ");
  }
  else if(p1.col[i] == 3)
  {
   printf(" spades ");
  }
  else if(p1.col[i] == 4)
  {
   printf(" The plum blossom ");
  }
  if(p1.data[i] == 11)
  {
   printf("J ");
  }
  else if(p1.data[i] == 12)
  {
   printf("Q ");
  }
  else if(p1.data[i] == 13)
  {
   printf("K ");
  }
  else if(p1.data[i] == 53)
  {
   printf(" wang  ");
  }
  else if(p1.data[i] == 54)
  {
   printf(" The king  ");
  }
  else
   printf("%d ", p1.data[i]);
 }
 printf("\n\n");
 return;
}
 
void printtable()
{
 int i;
 printf(" The CARDS on the table are: \n");
 if(table.top == 0)
 {
  printf(" (not) \n");
 }
 for(i = 1; i <= table.top; i++)
 {
  if(table.col[i] == 1)
  {
   printf(" The piece of ");
  }
  else if(table.col[i] == 2)
  {
   printf(" Red peach ");
  }
  else if(table.col[i] == 3)
  {
   printf(" spades ");
  }
  else if(table.col[i] == 4)
  {
   printf(" The plum blossom ");
  }
  if(table.data[i] == 11)
  {
   printf("J ");
  }
  else if(table.data[i] == 12)
  {
   printf("Q ");
  }
  else if(table.data[i] == 13)
  {
   printf("K ");
  }
  else if(table.data[i] == 53)
  {
   printf(" wang  ");
  }
  else if(table.data[i] == 54)
  {
   printf(" The king  ");
  }
  else
   printf("%d ", table.data[i]);
 }
 printf("\n\n\n");
 return;
}
 
void printzzz()
{
 system("cls");
 Sleep(400);
 printf(" is  ");
 Sleep(400);
 printf(" in  ");
 Sleep(400);
 printf(" Out of the  ");
 Sleep(400);
 printf(" card  ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(800);
 judge(table.data[table.top], 1);
 system("cls");
 printf(" The player 2  Start the round!! \n\n\n\n");
 printtable();
 table.top++;
 table.data[table.top] = p2.data[p2.head];
 table.col[table.top] = p2.col[p2.head];
 p2.head++;
 Sleep(600);
 printf(" play  ");
 Sleep(400);
 printf(" home  ");
 Sleep(400);
 printf("2 ");
 Sleep(400);
 printf(" is  ");
 Sleep(400);
 printf(" in  ");
 Sleep(400);
 printf(" Out of the  ");
 Sleep(400);
 printf(" card  ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(1000);
 judge(table.data[table.top], 2);
 return;
}
 
void judge(int a, int player)
{
 int i, j, sum = 2;
 if(player == 1)
 {
  for(i = table.top-1; i >= 1; i--, sum++)
  {
   if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54)))
   {
    for(j = table.top; j >= i; j--)
    {
     p1.data[p1.tail] = table.data[table.top];
     p1.col[p1.tail] = table.col[table.top];
     p1.tail++;
     table.top--;
    }
    break;
   }
  }
 
  if(i != 0)
  {
   system("cls");
   Sleep(400);
   printf(" play  ");
   Sleep(400);
   printf(" home  ");
   Sleep(400);
   printf("1 ");
   Sleep(400);
   printf(" win  ");
   Sleep(400);
   printf(" the  ");
   Sleep(400);
   printf("%d ", sum);
   Sleep(400);
   printf(" zhang ");
   Sleep(400);
   printf(" card  ");
   Sleep(400);
   printf(" !  ");
   Sleep(400);
   printf(" !  ");
   Sleep(400);
   printf(" !  ");
   Sleep(2000);
   system("cls");
  }
 }
 else if(player == 2)
 {
  for(i = table.top-1; i >= 1; i--, sum++)
  {
   if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54)))
   {
    for(j = table.top; j >= i; j--)
    {
     p2.data[p2.tail] = table.data[table.top];
     p2.col[p2.tail] = table.col[table.top];
     p2.tail++;
     table.top--;
    }
    break;
   }
  }
 
  if(i != 0)
  {
   system("cls");
   Sleep(400);
   printf(" play  ");
   Sleep(400);
   printf(" home  ");
   Sleep(400);
   printf("2 ");
   Sleep(400);
   printf(" win  ");
   Sleep(400);
   printf(" the  ");
   Sleep(400);
   printf("%d ", sum);
   Sleep(400);
   printf(" zhang ");
   Sleep(400);
   printf(" card  ");
   Sleep(400);
   printf(" !  ");
   Sleep(400);
   printf(" !  ");
   Sleep(400);
   printf(" !  ");
   Sleep(2000);
   system("cls");
  }
 }
 return;
}
 
void game()
{
 deal();
 getchar();
 
 while(p1.head != p1.tail && p2.head != p2.tail)
 {
  system("cls");
  int i;
  printf(" The player 1  Start the round!! \n\n\n\n");
  printp1();
  printtable();
  printf(" Press enter to play \n");
  getchar();
  table.top++;
  table.data[table.top] = p1.data[p1.head];
  table.col[table.top] = p1.col[p1.head];
  p1.head++;
  printzzz();
 }
 
 if(p1.head == p1.tail)
 {
  printf(" Congratulations on your victory!! \n");
  printf(" Thank you for using this program \n");
  system("pause");
 }
 else
 {
  printf(" You're out of CARDS! ");
  printf(" Sorry, you lost... \n");
  printf(" Don't lose heart, you can come again 1 Bureau!!!!! \n");
  system("pause");
 }
 return;
}
 
void ask()
{
 char ch;
 
 system("cls");
 printf(" Are you sure you want to quit? \n");
 printf(" (input y Confirm and verify) \n");
 printf("Y/N\n");
 scanf("%c", &ch);
 
 if(ch == 'y' || ch == 'Y')
 {
  exit(0);
 }
 else
 {
  return;
 }
}

Related articles: