C language to achieve automatic licensing procedures
Problem description
There are 52 cards in a deck of poker, and the cards should be divided among four people when playing bridge. Please design a program to complete the automatic licensing work. Requirements: Spades are represented by S (Spaces), Hearts by H (Hearts), Diamonds by D (Diamonds), Clubs by C (Clubs).
Problem analysis
Personal thoughts:
This problem is relatively simple to implement, just define two multidimensional character arrays, the first used to store the number of the poker, the second used to store the hands of 4 players.
52 cards are issued to 4 people, need to issue 13 rounds, each round according to the player's number order to deal them, when dealing cards, use a random function to generate the card number to be dealt (random function introduction can refer to my exercise on day 61), if the card has not been issued before (issued cards with '\0' mark), then the card number is stored in the player's hand array. Also assign the card number to '\0'. If the card corresponding to the generated random number has already been dealt, a new random number is continued to be generated. When all 52 cards have been dealt, the result of playing the printed cards.
poker array char poker[4][13]; The 4 means that there are four kinds of suits, 13 means that each has 13 (13 numbers), the number (name) of the playing card is represented by characters, respectively: {' 2 ', '3', '4', '5', '6', '7', '8', '9', '0', 'J' and 'Q' and 'K', 'A'} (' 0 '10)
The player's hand array is a three-dimensional character array, char players[PLAYER_NUMBER][4][13] = {0}; (PLAYER_NUMBER is the number of players, 4 means there are 4 suits of cards, and 13 means there are up to 13 cards of a certain suit)
When initialized, all values of the hand array are set to '\0', indicating that there are no cards in the hand.
Code implementation
#include <stdio.h> #include <stdlib.h> //srand()/rand() #include <time.h> //time() #define CARD_NUMBER 52 // Total number of cards (currently only 52 is supported) 4 / / # define PLAYER_NUMBER player quantity / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @ brief Deal cards to a player * @param player_id Player number * @param players Player hand array * @param left_num Number of cards left to deal * @param poker Poker array * @return Return 0 to indicate successful deal, Return 1 said no card can send * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / int Distribute_Card (int player_id, char players[][4][13], int *left_num, char poker[][13]) { if(*left_num <= 0) return -1; // No cards can be issued int card_id = 0; // Card number int card_index = 0; // Hand array subscript do {/* Randomly obtains the number of a playing card (0~52) */ card_id = rand() % CARD_NUMBER; }while(poker[card_id / 13][card_id % 13] == '\0'); // If the card is already issued, continue to get the number (*left_num)--; // The remaining number of cards to be dealt is reduced by 1 while(players[player_id][card_id / 13][card_index]! = '\0') { card_index++; // Reaches the next index of the valid value of the player's hand array ('\0' means invalid value)} /* Assigns a value to the player's hand array (add one card) */ players[player_id][card_id / 13][card_index] = poker[card_id / 13][card_id % 13]; /* Marks the card as invalid (issued) */ poker[card_id / 13][card_id % 13] = '\0'; return 0; } / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @ brief, a result issued by the brand * @ param players Players hand array * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / void Print_Result (char players[][4][13]) { int i = 0, j = 0, k = 0; / * brand category * / char card_name [] [8] = {" spade ", "red peach", "square", "plum blossom"}; for(i = 0; i < PLAYER_NUMBER; i++) {printf("\n player %d:\n", i+ 1); for(j = 0; j < 4; j++) { printf("%s: ", card_name[j]); // Print card type // Print a player's hand of that type for(k = 0; players[i][j][k]! = '\0' && k < 13; k++) { if(players[i][j][k] == '0') printf("10 "); //'0' corresponds to 10 else printf("%c ", players[i][j][k]); } printf("\n"); }}} int main () {/ * * / char poker CARDS array [4] [13] = {{' 2 ', '3', '4', '5', '6', '7', '8', '9', '0', 'J' and 'Q' and 'K', 'A'},\ {'2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A'},\ {'2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A'},\ {'2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A'}}; int left_num = sizeof(poker); // Number of cards left to issue (should be equal to CARD_NUMBER) char players[PLAYER_NUMBER][4][13] = {0}; // Player hand array int i = 0, j = 0, k = 0; // Initialize random number seed srand((unsigned)time(NULL)) with system seconds; /* Deal cards to each player */ for(i = 0; i < CARD_NUMBER/PLAYER_NUMBER + 1; i++) // When the player is odd, +1 is required (this value can only be more or less) {for(j = 0; j < PLAYER_NUMBER; j++) {// Deal to a player if(! Distribute_Card(j, players, &left_num, poker)) k++; // Deal success +1}} printf("\n Deal success %d times! \n", k); // Total number of deals Print_Result(players); // The printed result is return 0; }
Running result
Online reference
The idea is similar to mine, but there are many differences in details, and he also ranks each person's hand (from largest to smallest) at the end of the deal. (However, the random number seed of his random function is fixed, which results in the same result every time it is run.)
#include<stdlib.h> #include<stdio.h> int comp(const void *j, const void *i); void p(int b[], char n[]); int main(void) { static char n[]={'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'}; int a[53], b1[13], b2[13], b3[13], b4[13]; int b11=0, b22=0, b33=0, b44=0, t=1, m, flag, i; while(t<=52) /* Control deal 52 cards */ {m=rand()%52; /* Generates random numbers between 0 and 51 */ for(flag=1,i=1; i<=t&&flag; i++) /* Finds if the newly generated random number already exists */ if(m==a[i]) flag=0; /*flag=1 indicates that a new random number is generated, and flag=0 indicates that a newly generated random number already exists. */ if(flag) {a[t++]=m; /* if a new random number is generated, it is stored in an array */ /* According to the modulus of t, determine which array the current card should be stored in */ if(t%4==0) b1[b11++]= A [t-1]; else if(t%4==1) b2[b22++]=a[t-1]; else if(t%4==2) b3[b33++]=a[t-1]; else if(t%4==3) b4[b44++]=a[t-1]; } } qsort(b1, 13, sizeof(int), comp); /* Sort everyone's cards */ qsort(b2, 13, sizeof(int), comp); qsort(b3, 13, sizeof(int), comp); qsort(b4, 13, sizeof(int), comp); p(b1, n); /* Print each person's card separately */ p(b2, n); p(b3, n); p(b4, n); return 0; } void p(int b[], char n[]) { int i; printf("\n\006 "); /* Print the spades mark */ for(i=0; i<13; I++) / * converts an array of values to the corresponding color * / if (b [I] 13 = = 0) / / find the corresponding brand of design and color * * / printf (" % c ", n [13] [I] % b); printf("\n\003 "); /* Print the hearts mark */ for(i=0; i<13; i++) if((b[i]/13)==1) printf("%c ", n[b[i]%13]); printf("\n\004 "); /* Print box mark */ for(i=0; i<13; i++) if(b[i]/13==2) printf("%c ", n[b[i]%13]); printf("\n\005 "); /* Print a plum mark */ for(i=0; i<13; i++) if(b[i]/13==3 || b[i]/13==4) printf("%c ", n[b[i]%13]); printf("\n"); } int comp(const void *j, const void *i) /* Sorting function called by qsort */ {return(*(int*)i-*(int*)j); }
The above is the C language to achieve the details of the automatic licensing program, more information about the C language automatic licensing program, please pay attention to the script home other related articles!
Related article
-
C++ OpenCV read and write XML or YAML file method detailed explanation
XML is a meta-markup language. The meta tag is that developers can define their own tags according to their own needs. YAML is a highly readable format for expressing data sequences. This article will use C++ and OpenCV to achieve these two kinds of files to read and write, you can refer to the need2022-05-05 -
Gaussian random number generation for C++ VTK instances
This paper mainly introduces the generation of Gaussian random numbers for an instance of VTK. This paper demonstrates the random generation of 3 random numbers from a Gaussian distribution with an average of 0.0 and a standard deviation of 2.2. If you are interested, you can study it2021-11-11 -
C language program structure sample analysis
This article mainly explains the C language program structure, here provides a simple example to explain the structure of the C language program, which is conducive to the students who just learned the C language to understand the program structure2016-08-08 -
C language to draw cosine curve
This article mainly introduces in detail the relevant knowledge of C language to draw cosine curve, the example code in the article explains in detail, interested partners can follow the small series together to learn2024-01-01 -
C language game essential: cursor positioning and color setting method
This article is a detailed analysis and introduction of the method of cursor positioning and color setting in c language, and needs a friend's reference2013-05-05 -
C language file operation analysis details and example code
This article mainly introduces the detailed analysis of the C language file operation and the relevant information of the example code, and the friends who need it can refer to the next2016-11-11 -
C/C++ wide and narrow character conversion and output of a variety of implementation methods
This article mainly introduces C/C++ wide and narrow character conversion and output of a variety of implementation methods, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it2022-08-08 -
Talk about C++ operator overloading
Operator overloading is a form of C++ polymorphism, overloaded operators can make the code look more natural, the following examples to introduce the C++ operator overloading knowledge, interested friends take a look at it2021-11-11 -
C++ four cases detailed introduction summary
This article mainly introduces the detailed introduction of C++ four cases summary, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to study together2022-05-05 -
C language data structure binary linked list to create binary tree
This article mainly introduces the C language data structure of the binary linked list to create a binary tree, below we in order to more convenient use of binary tree structure, you can use typedef to name the structure, the specific content needs a little partner can refer to2022-02-02
Latest comments