C language for Card Games (Cat Fishing)
- 2020-11-25 07:26:50
- OfStack
The C language USES queues and stacks to implement the card game, including cat fishing. The details are as follows
C language:
// A game of CARDS -- The kitten fishing -- The queue The stack --( The so-called pull train)
#include <stdio.h>
struct queue // The queue
{
int data[1000];
int head;
int tail;
};
struct stack // The stack
{
int data[10];
int top;
};
int main(void)
{
struct queue q1, q2; // Little hum q1 And small, q2 The queue
struct stack s; // The stack
int book[10]; // Record, determine whether or not to regulate 2 time
int i, t;
q1.head = 1, q1.tail = 1; // Initialize queue
q2.head = 1, q2.tail = 1;
s.top = 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++) { // Here is a given 1 personal 6 card
scanf("%d", &q1.data[q1.tail]);
q1.tail++;
}
for(i = 1;i <= 6;i++) {
scanf("%d", &q2.data[q2.tail]);
q2.tail++;
}
while(q1.head < q1.tail && q2.head < q2.tail ) { // The loop executes when the queue is not empty
t = q1.data[q1.head]; // Play the CARDS (first)
if(book[t] == 0){ // When the card is not on the table
q1.head++; // Line up the card
//s.top++;
s.data[++s.top] = t; // Push the card played on the stack
book[t] = 1; // Mark that the card is already on the table
}else{ // It's already on the table, and Humph can win
q1.head++; // The card to be played will be played by the team
q1.data[q1.tail] = t; // Place this card at the end of the line
q1.tail++;
while(s.data[s.top] != t) { // Take back the winning CARDS from the table , There is no end here 1 Root brand t
book[s.data[s.top]] = 0; // unmark
q1.data[q1.tail] = s.data[s.top]; // Put them at the end of the line
q1.tail++;
s.top--; // The stack 1 A card, so minus 1
}
// Take back on the table t card
book[t] = 0;
q1.data[q1.tail] = t;
q1.tail++;
s.top--;
}
if(q1.head == q1.tail ) // If the humph is finished, the game is over
break;
// It's Ha's turn to play , And the little hum 1 Sample to judge
t = q2.data[q2.head];
if(book[t] == 0) {
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--;
}
book[t] = 0;
q2.data[q2.tail] = t;
q2.tail++;
s.top--;
}
}
if(q2.head == q2.tail ) {
printf(" Little hum win \n");
printf(" Humph's current hand is ");
for(i = q1.head;i < q1.tail;i++)
printf(" %d",q1.data[i]);
if(s.top) { // If there are CARDS on the table
printf("\n The card of the table is ");
for(i = 1;i <= s.top;i++)
printf(" %d",s.data[i]);
printf("\n");
}
else
printf("\n There are no CARDS on the table ");
} else {
printf(" hargreaves win \n");
printf(" Xiao Ha's current hand is ");
for(i = q2.head;i <= q2.tail-1;i++)
printf(" %d", q2.data[i]);
if(s.top) { // If there are CARDS on the table
printf("\n The card of the table is ");
for(i = 1;i <= s.top;i++)
printf(" %d",s.data[i]);
printf("\n");
} else
printf("\n There are no CARDS on the table ");
}
return 0;
}
/*Code Running Results
1 2 3 4 5 6
3 2 1 5 2 6
hargreaves win
Xiao Ha's current hand is 5 6 2 3 1 3 2 5 2
The card of the table is 4 6 1
*/
The program USES a queue to implement the CARDS in the player's hand (the player's CARDS can only be played in the first place, and the winning CARDS can be placed in the second place), and a stack to implement the CARDS on the table (the CARDS are placed at the end, and the winning CARDS are taken away from the end).