C language implements the cat fishing algorithm of card game

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

The example of this paper shares the specific code of C language to implement the cat fishing algorithm for your reference, the specific content is as follows

On Sunday, Xiao Hum and Xiao Ha played a board game. They were playing a very strange poker game called "Cat fishing". The rules of the game are as follows: divide a pack of playing CARDS equally into two parts, each taking one. He takes the first card in his hand and puts it on the table. Then He takes the first card in his hand and puts it on the top of the card that He has just played. When playing a card, if a person plays the same card as a card on the table, the two same CARDS and the CARDS in the middle can be taken out, and in turn placed at the end of his hand. When all the CARDS in any one hand are played, the game is over and the opponent wins. (Done with two queues and one stack)

The code is as follows:


#define _CRT_SECURE_NO_WARNINGS 1
 
#include <stdio.h>
#include <stdlib.h>
 
/*
*  Here's the game, will 1 The deck of playing CARDS is divided equally into two parts, each one taking 1 Copies. Xiao Hum first take the first hand 1 A playing card was placed on the table, and Harper returned with the first of his shots 1 A playing card, and put it on top of the card that Little Hum just played, like this two people alternate. When playing a card, if a person plays the same card as a card on the table, the two same CARDS and the middle of the card can be taken away, and in turn put in their hands at the end of the card. When any 1 When all the CARDS in one's hand have been played, another 1 Personal victory 
*  Wen-feng guo 
* 2018/9/29
*/
 
struct queue
{
 int data[1000];
 int head;
 int tail;
};
 
struct stack
{
 int data[10];
 int top;
};
 
int main(void)
{
 struct queue q1, q2;
 struct stack s;
 int i = 0;
 int t = 0;
 int book[10];
 
 // Initialize queue 
 q1.head = 1;
 q1.tail = 1;
 q2.head = 1;
 q2.tail = 1;
 
 // Initialize the stack 
 s.top = 0;
 
 for (i = 0; i < 10; i++)
 {
 book[i] = 0;
 }
 
 // In turn, insert into the queue 6 The number of 
 // For small hum 6 card 
 for (i = 1; i <= 6; i++)
 {
 scanf("%d", &q1.data[q1.tail]);
 q1.tail++;
 }
 
 // In turn, insert into the queue 6 The number of 
 // For small, 6 card 
 for (i = 1; i <= 6; i++)
 {
 scanf("%d", &q2.data[q2.tail]);
 q2.tail++;
 }
 
 // The loop executes when the queue is not empty 
 while (q1.head < q1.tail && q2.head < q2.tail)
 {
 t = q1.data[q1.head];// Little hum 1 card 
 // Judge whether a little hum can win a card played 
 if (book[t] == 0)// Indicates that there are no CARDS on the table t  The CARDS 
 {
 q1.head++;
 s.top++;
 s.data[s.top] = t;
 book[t] = 1;
 }
 else
 {
 q1.head++;
 q1.data[q1.tail] = t;
 q1.tail++;
 while (s.data[s.top] != t)
 {
 book[s.data[s.top]] = 0;
 q1.data[q1.tail] = s.data[s.top];
 q1.tail++;
 s.top--;
 }
 }
 
 t = q2.data[q2.head];// Hargreaves out 1 card 
 // Judge whether a player can win a card he has played 
 if (book[t] == 0)// Indicates that there are no CARDS on the table t  The CARDS 
 {
 q2.head++;
 s.top++;
 s.data[s.top] = t;
 book[t] = 1;
 }
 else
 {
 q2.head++;
 q2.data[q2.tail] = t;
 q2.tail++;
 while (s.data[s.top] != t)
 {
 book[s.data[s.top]] = 0;
 q2.data[q2.tail] = s.data[s.top];
 q2.tail++;
 s.top--;
 
 }
 
 }
 }
 
 if (q2.head == q2.tail)
 {
 printf(" Little hum WIN\n");
 printf(" The remaining CARDS in Xiao Hum's hand are: ");
 for (i = q1.head; i <= q1.tail - 1; i++)
 {
 printf(" %d", q1.data[i]);
 }
 
 if (s.top > 0)
 {
 printf("\n The CARDS on the table were: ");
 for (i = 1; i <= s.top; i++)
 {
 printf(" %d", s.data[i]);
 }
 }
 else
 {
 printf(" There are no CARDS on the table! \n");
 }
 }
 else
 {
 printf(" hargreaves WIN\n");
 printf(" The remaining CARDS in Xiao Ha's hand are: ");
 for (i = q2.head; i <= q2.tail - 1; i++)
 {
 printf(" %d", q2.data[i]);
 }
 
 if (s.top > 0)
 {
 printf("\n The CARDS on the table were: ");
 for (i = 1; i <= s.top; i++)
 {
 printf(" %d", s.data[i]);
 }
 }
 else
 {
 printf(" There are no CARDS on the table! \n");
 }
 }
 
 system("pause");
 
 return 0;
}

Related articles: