An interesting C + +-based simulation licensing program

Source: Internet
Author: User

In memory to simulate a deck of cards, and then simulate shuffle, licensing and other actions.

The process is this: build a deck of cards to save in an array-shuffle-create players-play cards to players – output each player's card.

#include <stdio.h>#include <stdlib.h>#include <time.h>Define the color of pokerEnum suit{heart, Spade, Diamond, Club, Joker1, joker2};The quantity of a deck of cards#define CARD_COUNT 54Define Pokertypedefstruct card{int value;Number of cards starting from 1Enum Suit Suit;Color}card;Define Playerstypedefstruct player{Char name[64];Player's name Card * * CARDS;The card the player points to. Each item is a pointer to an item in the original deck array, which saves spaceint cardscount;The number of cards the player has}player;Type of function that is called after the token has been completedtypedefInt (*compare) (card*, card*);function declarationschar* Getcardname (Const card*); card** Shuffle (Const card*);void Dispatchcards (player**,IntConst card**);void sort (card**,Int,compare);int Compare1 (card*, card*);int Compare2 (card*, card*);void Initonepack ();An array of original decks card pokers[card_count];Entry functionint main (void) {Initialize a deck of Cards initonepack ();Shuffle, shuffledpokers save after washing the cards card** shuffledpokers = Shuffle (pokers);Build three player player Player1;strcpy (Player1.name,"The Old King Next Door"); Player1.cards=null; Player1.cardscount=0; Player Player2;strcpy (Player2.name,"Xiao Ming"); Player2.cards=null; Player2.cardscount=0; Player Player3;strcpy (Player3.name,"Tanaka Turtle Sun"); Player3.cards=null; Player3.cardscount=0;Put three into an array to pass in the player* players[]={&player1,&player2,&player3}; of the card functionLicensing Dispatchcards (Players,sizeof (players)/sizeof (player*), shuffledpokers);After the wash, the cards are exhausted, the release ofFree (shuffledpokers);int i;Print out the cards in each player's handsfor (i=0;i<sizeof (players)/sizeof (player*); i++) {Print the player's name firstprintf"%s\n", players[i]->name);Need to sort (players[i]->cards,players[i]->cardscount,compare1) the cards in the hands of the player;Print all the cards in the player's handsInt J;for (j=0;j<players[i]->cardscount;j++) {char * name = Getcardname (Players[i]->cards[j]);printf"%s", name);Free (name); }Each player needs to change the line one timeprintf"\ n"); }Releasing an array of players ' handsfor (i=0;i<sizeof (players)/sizeof (player*); i++) {Free (players[i]->cards); }Return0;}Construct a deck of cardsvoid Initonepack () {int i=0;Top 52 Photosfor (; i<card_count-2;i++) {pokers[i].value=i/4 +1; Pokers[i].suit = i%4; }The two remaining: King and Xiao WangJoker1 pokers[i].value=i/4 +1; Pokers[i].suit=joker1;Joker2 pokers[i+1].value=i/4 +2; pokers[i+1].suit=joker2;}Shuffle, parameter is the original a deck of cards, returned after washing the card card** shuffle (Const card* pokers) {int i;Card returns the memory space of the array card** retpokers =malloc (card_count*sizeof (card*));In order not to change the original deck of cards, create an array, save the original hand pointer (note that each item is not a card, but the hands of the hand) card** pokers2 =malloc (card_count*sizeof (card*));for (i=0;i<card_count;i++) {Pokers2[i] = &pokers[i];}Plant random seeds. The seed takes the current time,This ensures that each time the program is run, the sequence of random numbers generated is different srand ((NULL));Get the random sequence number, take the item that the serial number refers to from the pokers2, and add it to the retpokers in turn.for (i=0;i<card_count;i++) {Unsignedint index = rand ()%card_count;if (Pokers2[index]! = NULL) {Retpokers[i] = Pokers2[index]; pokers2[index]=null;}else{i--;}}Free (POKERS2);Returns the array after washingreturn retpokers;}LicensingPlayers is a player arrayPlayercount is the number of playersShuffledcards is a deck of cards after washing.void Dispatchcards (player** players,int Playercount,Const card** shuffledcards) {Calculates the capacity of the array of each player's hands, if the cards are different in the hands of each player,A maximum of one, plus 1 is to ensure that the array allocated enough space to accommodate the cards.int numbercards = card_count/playercount+1;Allocate space for each player's array of cardsint i;for (i=0;i<playercount;i++) {card* cards =malloc (numbercards*sizeof (card*)); Players[i]->cards = cards; }Turn the cards to each playerfor (i=0;i<card_count;i++) {Take current player player *curplayer = Players[i%playercount];Licensing Curplayer->cards[curplayer->cardscount] = Shuffledcards[i];The actual number of cards in the player's hands increased curplayer->cardscount++; }}Sort functionCards are the cards to be sorted, each of which is a hands-on handCardscount is the number of cardsCompare_func is a comparison functionvoid sort (card** cards,int Cardscount,compare compare_func) {int i;for (i=0;i<cardscount-1;i++) {Int J;for (j=0;j<cardscount-i-1;j++) {if (Compare_func (cards[j],cards[j+1])) {int TMP=CARDS[J]; cards[j]=cards[j+1]; cards[j+1]=tmp; } } }}Comparison function, compare the points and compare the suits firstint Compare1 (card* a,card* b) {if (A->value > B->value) {Return1; }Elseif (A->value < B->value) {Return0; }else{if (A->suit > B->suit)Return1;ElseReturn0; }}Comparison function, compare the points and compare the suits firstint Compare2 (card* a,card* b) {if (A->value > B->value) {Return0; }Elseif (A->value < B->value) {Return1; }else{if (A->suit > B->suit)Return0;ElseReturn1; }}Get the name of the cardReturns the name string of the card that the caller needs when it is exhausted.char* Getcardname (Const card* Card) {Store Suit nameChar suitstr[16]={0};0== ' + 'Switch (card->suit) {Case Heart:strcpy (SUITSTR,"Red Peach");BreakCase SPADE:strcpy (SUITSTR,"Spades");BreakCase Diamond:strcpy (SUITSTR,"Block");BreakCase Club:strcpy (SUITSTR,"Plum Blossom");Break }Store the number of dot namesChar valuestr[16];Switch (card->value) {Case1:strcpy (VALUESTR,"A");BreakCase11:strcpy (VALUESTR,"J");BreakCase12:strcpy (VALUESTR,"Q");BreakCase13:strcpy (VALUESTR,"K");Breakcase 14: strcpy (ValueStr,break; case 15: strcpy (ValueStr,break; default: sprintf (Valuestr, "%d" , Card->value); break;} //dynamically allocate enough space char * ret = malloc ( Span class= "Hljs-number" >16); //merges two names into a ret sprintf (Ret, "%s %s ", SUITSTR,VALUESTR); return ret;}            

An interesting C + +-based simulation-licensing program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.