SoFunction
Updated on 2025-03-10

C language realization of card game (kitten fishing)

C language uses queues and stacks to implement card games – kitten fishing for your reference. The specific content is as follows

C language:

// Card game - kitten fishing - queue stack - (so-called pulling train)#include <>

struct queue   //queue{
 int data[1000];
 int head;
 int tail;
}; 
struct stack   //Stack{
 int data[10];
 int top;
};

int main(void)
{
 struct queue q1, q2; //The queues of Xiaoheng q1 and Xiaohan q2 struct stack s;  //Stack int book[10];   //Record, determine whether it appears the second time int i, t;
 
  = 1,  = 1;  //Initialize the queue  = 1,  = 1; 
 
  = 0;     //Initialize the stack 
 for(i = 1;i <= 9;i++)  //The number of initialization occurrences is 0 book[i] = 0;
 
 for(i = 1; i <= 6; i++) {  //Give a person here 6 cards scanf("%d", &[]);
 ++;
 }
 for(i = 1;i <= 6;i++) {
 scanf("%d", &[]);
 ++;
 } 
 
 while( <  &&  <  ) {  //Execute loop when the queue is not empty t = [];   //Xiaohen (first) plays the cards if(book[t] == 0){    //When there is no such card on the table ++;    //Get this card in team //++;
 [++] = t;  //Put the played cards into the stack book[t] = 1;    //Tag this table already has }else{      //It's already available on this table, Xiaohen can win ++;    //The card you will play will be played to team [] = t; //Put this card to the end of the team ++;
 
 while([] != t) { //Retract the winning cards on the table, and there is no last card here. book[[]] = 0;   //Cancel mark [] = []; //Put it at the end of the team in turn ++;
 --;       //A card is missing in the stack, so--1 }
 //Retract the t card on the table book[t] = 0;
 [] = t;
 ++;
 --; 
 }
 
 if( ==  )   //If Xiaoheng is finished playing, the game is over break; 
 
 //It's Xiao Ha's turn to play cards, and make a judgment like Xiao Heng t = [];
 if(book[t] == 0) {
 ++;
 ++;
 [] = t;
 book[t] = 1;
 } 
 else {
 ++;
 [] = t;
 ++;
 
 while([] != t) {
 book[[]] = 0;
 [] = [];
 ++;
 --;
 } 
 
 book[t] = 0;
 [] = t;
 ++;
 --;
 } 
 } 
 
 if( ==  ) {
 printf("Small Hum win \n");
 printf("Xiaohen's current card is");
 for(i = ;i < ;i++)
 printf(" %d",[i]);
 
 if() {  //If there is a card on the table printf("\nThe card on the table is");
 for(i = 1;i <= ;i++)
 printf(" %d",[i]);
 printf("\n"); 
 }
 else
 printf("\nThere are no cards on the table");
 } else {
 printf("Xiao Ha win \n");
 printf("Xiao Ha's current card is");
 for(i = ;i <= -1;i++)
 printf(" %d", [i]);
 
 if() {  //If there is a card on the table printf("\nThe card on the table is");
 for(i = 1;i <= ;i++)
 printf(" %d",[i]);
 printf("\n"); 
 } else
 printf("\nThere are no cards on the table");
 } 
 
 return 0;
} 
/*Code Running Results
 1 2 3 4 5 6
 3 2 1 5 2 6
 Little Ha win
 Xiao Ha's current card is 5 6 2 3 1 3 2 5 2
 The card on the table is 4 6 1
 */

This program uses a queue to realize the cards in the player's hand (the player's cards can only play cards in front and put them in the back in order), and uses the stack to realize the cards on the table (playing cards at the end, and winning cards are also taken away from the end).

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.