bool genCards(pokerCard *deck, pokerCard * cards, int requestNum) { if (deck->dealt) { return false; } int i = 0; int available = CARD_COUNT; for (i=0; i<CARD_COUNT; i++) { if ((deck+i)->dealt) { available = i+1; break; } } if (available<requestNum) { return false; } for (i=0; i<requestNum; i++) { (deck+i)->dealt = 1; copyCard(deck+i, cards+i); swapCards(&deck[i], &deck[available-1]); available--; } return true; }
bool genRandomCards(pokerCard *deck, pokerCard *cards, int requestNum) { if (deck->dealt) { return false; } int i = 0; int available = CARD_COUNT; for (i=0; i<CARD_COUNT; i++) { if ((deck+i)->dealt) { available = i+1; break; } } if (available<requestNum) { return false; } // generate a random index and copy to cards // swap this card with the last card that is not dealt on the deck for (i=0; i<requestNum; i++) { int rnum = ran_int(0, available-1); (deck+rnum)->dealt = 1; copyCard(deck+rnum, cards+i); swapCards(&deck[rnum], &deck[available-1]); available--; } return true; }
struct Card playCard(struct Hand *hand, int index) { //First, bubble the requested card to the top for (int card = index; card < hand->cardc - 1; card++) { swapCards(hand->cardv, card, card + 1); } //Next, decrement the card amount by one hand->cardc--; //Finally, return the card which is now outside the hand return hand->cardv[hand->cardc]; }
void Deck::shuffleDeck() { for(int i = 0; i < 100; i++) { int a = rand() % 52; int b = rand() % 52; swapCards(a,b); } top = 0; //displayDeck(); }
//************************************ // Method: shuffle - shuffle the deck depth times // FullName: Deck::shuffle // Access: public // Returns: void // Qualifier: // Parameter: int depth - the number of times to shuffle the deck //************************************ void Deck::shuffle(int depth) { //Fisher–Yates shuffling algorithm m_timesToShuffle=depth; srand((unsigned int)time(0)); //Seed random numbers generator int j; for (int i=((int)m_dqDeck.size())-1; i>=1; i--) { j=rand()%(i+1); //Generate a number between 0 and i swapCards(m_dqDeck[i],m_dqDeck[j]); } }
void shuffleCard(pokerCard *deck, int deckSize, int shuffleSize) { // initialize 2 pointers point to the first card of the deck pokerCard *pc_l = deck; pokerCard *pc_r = deck; int i = 0; for (i=0; i<shuffleSize; i++) { // randomly offset 2 pointers on the deck and swap cards being pointed swapCards(pc_l+ran_int(0, deckSize-1), pc_r+ran_int(0, deckSize-1)); } }
void sortHand(struct Hand hand) { //Sort a hand of cards using a bubble sort int unsorted; do { unsorted = 0; for (int card = 0; card < hand.cardc - 1; card++){ if (hand.cardv[card].value > hand.cardv[card + 1].value) { swapCards(hand.cardv, card, card+1); unsorted = 1; } } } while (unsorted); }
void CardDeck::shuffle() { for (int i = 0; i<100; i++) { swapCards(rand()%52, rand()%52); } }
//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; }