/* sort hands */ void sortHands(Hand unsorted[], Hand sorted[], int handSize, int nhands){ int i; for (i = 0; i < nhands; i++) { copyHand(unsorted[i], &sorted[i], handSize); sortHand(&sorted[i], handSize); } }
/** * Takes an array of hands and sorts all of them * @param players * @param numCards * @param hands */ void sortHands(int players, int numCards, struct hand * hands) { int i; for (i = 0; i < players; i++) { sortHand(numCards, &hands[i].cards); } }
void Hand::drawFromDeck(Deck *d){ if(d->getNumCards()>0){ Card c = d->drawCard(); c.flipCard(); if(c.isSelected() == true) c.toggleSelected(); //insert(c); handList.insert(handList.end(), c); numCards++; sortHand(); } }
//This function allocates memory, it is YOUR repsonsibility to free that //memory when you are done in order to prevent a leak. You can use the //freeHands function to free it up if you want, or you can do it //yourself. //This deals out however many hands you need. It's pretty neat. If sort //is nonzero, the hands will be sorted. struct Hand *dealHands(int players, int sort) { //We check how many cards each player gets, and check the math int cardsperplayer = 54 / players; int leftovers = 54 % players; if ((cardsperplayer*players + leftovers) != 54) {fprintf(stderr, "Critical Error, we got more than 54 cards!"); return NULL;}; //IT IS OF THE UTMOST IMPORTANCE THAT THIS HAND IS UN-ALLOCATED AS //SOON AS YOU ARE DONE WITH IT, BECAUSE IT IS AN EXCELLENT WAY TO //LEAK MEMORY. DO NOT FORGET TO FREE EACH HAND BEFORE FREEING THE //POINTER TO IT. struct Hand *hands = (struct Hand*)calloc(players, sizeof(struct Hand)); for (int player = 0; player < leftovers; player++) { //Allocate an extra spot for these players, since they take an //extra card hands[player].cardv = (struct Card*)calloc((cardsperplayer + 1), sizeof(struct Card)); hands[player].cardc = cardsperplayer + 1; } for (int player = leftovers; player < players; player++) { //Allocate the normal amount of cards for the rest of the players hands[player].cardv = (struct Card*)calloc((cardsperplayer), sizeof(struct Card)); hands[player].cardc = cardsperplayer; } //First, we create a deck of cards. struct Card deck[54]; int currentCard = 0; for (int suit = 0; suit < 4; suit++) { for (int value = 0; value < 13; value++) { struct Card newCard = {suit, value}; deck[currentCard] = newCard; currentCard++; } } //Then we add the jokers on the end struct Card blackJoker = {0, 13}; deck[52] = blackJoker; struct Card redJoker = {1, 13}; deck[53] = redJoker; //Now, we shuffle the deck using the Fisher Yates shuffle (the Durstenfeld version) for (int cardsleft = 53; cardsleft > 0; cardsleft--) { swapCards(deck, cardsleft, rand() % cardsleft); } //IT SEEMS TO DROP THE HANDS ARRAY RIGHT HERE...run //Now that the cards are randomly shuffled, we just deal them out to // everyone (it doesn't matter about the order, this is supposed to //be truly random! currentCard = 0; for (int player = 0; player < leftovers; player++) { //Deal an extra card for these players, since they take an extra //card for (int card = 0; card < hands[player].cardc; card++) { hands[player].cardv[card] = deck[currentCard]; currentCard++; } } for (int player = leftovers; player < players; player++) { //Deal the normal amount of cards for the rest of the players for (int card = 0; card < hands[player].cardc; card++) { hands[player].cardv[card] = deck[currentCard]; currentCard++; } } if (sort != 0) { for (int player = 0; player < players; player++) { sortHand(hands[player]); } } //Now that we should have dealt out all the cards, it's time to //return them. return hands; }