C++ implementation of shuffling sorting function sample code

  • 2020-05-30 20:53:17
  • OfStack

Simulate a deck of CARDS in memory, and then simulate shuffling, dealing, and so on.

The process goes like this: build a deck and save it in an array -- shuffle -- create a player -- issue the deck to the player and output each player's card.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Define the suit of a poker 
enum Suit{
  heart,
  spade,
  diamond,
  club,
  joker1,
  joker2
};
//1 The number of decks 
#define CARD_COUNT 54
// Definition of poker 
typedef struct Card{
  int value;// The number of CARDS from 1 start 
  enum Suit suit;// Design and color 
}Card;
// Define the player 
typedef struct Player{
  char name[64];// Name of player 
  Card ** cards;// The CARDS the player is dealt. Each item is 1 Two Pointers to the original 1 In the deck array 1 Terms, so we can save space 
  int cardsCount;// The number of CARDS a player is dealt 
}Player;
// The type of function called after the card split is complete 
typedef int (*COMPARE)(Card* ,Card*);
// Function declarations 
char* getCardName(const Card*);
Card** shuffle(const Card*);
void dispatchCards(Player** ,int ,const Card** );
void sort(Card**,int,COMPARE);
int compare1(Card* ,Card*);
int compare2(Card* ,Card*);
void initOnePack();
// The original 1 The array of deck CARDS 
Card pokers[CARD_COUNT];
// The entry function 
int main(void)
{
  // Initialize the 1 Deck of CARDS 
  initOnePack();

    // Shuffle the deck, shuffledPokers Save the shuffled CARDS 
  Card** shuffledPokers = shuffle(pokers);
    // build 3 A player 
  Player player1;
  strcpy(player1.name," Next to the Lao wang ");
  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;
  // the 3 In the 1 Is passed into the deal function 
  Player* players[]={&player1,&player2,&player3};
    // licensing 
  dispatchCards(players,sizeof(players)/sizeof(Player*),shuffledPokers);
    // After shuffling out of CARDS, release it 
  free(shuffledPokers);
  int i;
    // Print out the CARDS in each player's hand 
  for(i=0;i<sizeof(players)/sizeof(Player*);i++){
        // Print the player's name first 
    printf("%s\n",players[i]->name);
        // You need to sort the CARDS in the player's hand 
    sort(players[i]->cards,players[i]->cardsCount,compare1);
        // Print all CARDS in the player's hand 
    int j;
    for(j=0;j<players[i]->cardsCount;j++){
      char * name = getCardName(players[i]->cards[j]);
      printf("%s ",name);
      free(name);
    }
    // Every player needs to change 1 Time line 
    printf("\n");
  }
    // Releases the array of CARDS in the player's hand 
  for(i=0;i<sizeof(players)/sizeof(Player*);i++){
    free(players[i]->cards);
  }
  return 0;
}
// structure 1 Deck of CARDS 
void initOnePack(){
  int i=0;
  // before 52 zhang 
  for(;i<CARD_COUNT-2;i++){
    pokers[i].value=i/4+1;
    pokers[i].suit = i%4;
  }
  // The other two: king and xiao wang 
  //joker1
  pokers[i].value=i/4+1;
  pokers[i].suit=joker1;
  //joker2
  pokers[i+1].value=i/4+2;
  pokers[i+1].suit=joker2;
}
// Shuffle, the parameters are primitive 1 Deck, return the shuffled deck 
Card** shuffle(const Card* pokers){
  int i;
    // The split card returns the memory space of the card array 
  Card** retPokers = malloc(CARD_COUNT*sizeof(Card*));
  // In order not to alter the original 1 Deck. Build another deck 1 An array that holds the pointer to the original card (note that each item is not a card, but a pointer to a card) 
  Card** pokers2 = malloc(CARD_COUNT*sizeof(Card*));
  for(i=0;i<CARD_COUNT;i++){
    pokers2[i] = &pokers[i];
  }
    // Plant a random seed. The seed takes the current time, 
    // Therefore, the random number sequence is guaranteed to be different every time the program is run 
  srand(time(NULL));
    // Get the random number from pokers2 Take the term of the sequence and add it to retPokers In the. 
  for(i=0;i<CARD_COUNT;i++){
    unsigned int index = rand()%CARD_COUNT;
    if(pokers2[index] != NULL){
      retPokers[i] = pokers2[index];
      pokers2[index]=NULL;
    }else{
      i--;
    }
  }
  free(pokers2);

    // Returns the washed array 
  return retPokers;
}
// licensing 
//players It's an array of players 
//playerCount Is the number of players 
//shuffledCards It's after you wash it 1 Deck of CARDS 
void dispatchCards(Player** players,int playerCount,const Card** shuffledCards){
  // Calculates the capacity of each player's array of CARDS if each player's hand is not 1 The sample, 
  // The most difference 1 Zhang, 1 The idea is to make sure that the array has enough space to hold the CARDS. 
  int numberCards = CARD_COUNT/playerCount+1;
  // Allocate space for each player's card array 
  int i;
  for(i=0;i<playerCount;i++){
    Card* cards = malloc(numberCards*sizeof(Card*));
    players[i]->cards = cards;
  }
  // Deal CARDS to each player in turn 
  for(i=0;i<CARD_COUNT;i++){
    // Fetch current player 
    Player *curPlayer = players[i%playerCount];
    // Deal CARDS to players 
    curPlayer->cards[curPlayer->cardsCount] = shuffledCards[i];
    // The actual number of CARDS in a player's hand increases 
    curPlayer->cardsCount++;
  }
}
// Sorting function 
//cards It's the CARDS to sort, each 1 The item is the pointer to the card 
//cardsCount It's the number of CARDS 
//compare_func It's a comparison function 
void 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;
        }
    }
  }
}
// Compare functions, compare points and then compare suits 
int compare1(Card* a,Card* b){
  if(a->value > b->value){
    return 1;
  }else if(a->value < b->value){
    return 0;
  }else{
    if(a->suit > b->suit)
      return 1;
    else
      return 0;
  }
}
// Compare functions, compare points and then compare suits 
int compare2(Card* a,Card* b){
  if(a->value > b->value){
    return 0;
  }else if(a->value < b->value){
    return 1;
  }else{
    if(a->suit > b->suit)
      return 0;
    else
      return 1;
  }
}
// Gets the name of the card 
// Returns the name string of the card that the caller needs when it is used up free() . 
char* getCardName(const Card* card){
  // Storage color name 
  char suitStr[16]={0};//0=='\0'
  switch (card->suit) {
  case heart:
    strcpy(suitStr," Red peach ");
    break;
  case spade:
    strcpy(suitStr," spades ");
    break;
  case diamond:
    strcpy(suitStr," square ");
    break;
  case club:
    strcpy(suitStr," The plum blossom ");
    break;
  }
  // Name of store points 
  char valueStr[16];
  switch(card->value){
  case 1:
    strcpy(valueStr,"A");
    break;
  case 11:
    strcpy(valueStr,"J");
    break;
  case 12:
    strcpy(valueStr,"Q");
    break;
  case 13:
    strcpy(valueStr,"K");
    break;
  case 14:
    strcpy(valueStr," wang ");
    break;
  case 15:
    strcpy(valueStr," The king ");
    break;
  default:
    sprintf(valueStr,"%d",card->value);
    break;
  }
  // Dynamically allocate enough space 
  char * ret = malloc(16);
  // Merge the two names into ret In the 
  sprintf(ret,"%s%s",suitStr,valueStr);
  return ret;
}

Related articles: