• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 card shuffler Z!re Posting Freak     Posts: 3,522 Threads: 189 Joined: Dec 2003 05-24-2005, 05:00 AM Actually, I wont enter this challenge.. Sorry.. I was just curious to what you meant, and ofcourse it's all theoretical.. for a player to actually get an "ordered" hand is.. well.. rare..  Pyrokid Senior Member    Posts: 319 Threads: 68 Joined: Mar 2005 05-24-2005, 06:43 AM Aight heres mine. made it in FB (although i dont think it matters if you use qb) i took a little bit of code from from Cha0s. Im having troubles to get it to move down a number of rows so that you can fit more than 4 on one page but here it is: Code:```declare function rndcard\$() np=4 'Number of players. change to your needs 'dims the player array 'the first d is the number of players 'the second d is the number of in wich he gets the card '(sorry cant explain think of any other way to explain that) dim player\$(np,52) randomize timer for p=1 to np ' does for number of players for i=1 to (52/np) ' divides the deck    1    player\$(p,i) = rndcard\$() ' gets the card       for a= 1 to np    for n = 1 to (i - 1)                          'this checks to make           if player\$ (p,i) = player\$(a,n) then goto 1'sure the card isnt    next                                          'repeated    next next next 'the rest of this prints out the player and card names c=1 oldp=0 for p=1 to np r=1 locate r,c: print "Player "; p r=r+1 for i= 1 to (52/np)    r=r+1    locate r,c: print player\$(p,i) next c=c+18 next do:loop until inkey\$=chr\$(27) function rndcard\$()       SS=int(rnd*4)+1 'this is selects the suit    card=int(rnd*13)+1 ' this selects the card number       'names the suits    select case SS    case 1       suit\$="Spades"    case 2       suit\$="Clubs"    case 3         suit\$="Diamonds"    case 4       suit\$="Hearts"    end select          'selects the card names    select case card    case 1       cname\$="Ace"    case 11       cname\$="Jack"    case 12       cname\$="Queen"    case 13       cname\$="King"    case else       cname\$=str(card)    end select       rndcard\$=cname\$ + " of " + suit\$ 'puts it all together       end function``` *EDIT whoops probably shouldve checked it. for some reason i get one or 2 repeats somone wouldnt mind pointing out the mistake i think its in my card checker * his world has been connected... Tied to the darkness. Soon to be completely eclipsed. There is so very much to learn... You understand so little. A meaningless effort. One who knows nothing can understand nothing. -Ansem Bringer of darkness and creator of the heartless neuro Member   Posts: 58 Threads: 6 Joined: Feb 2005 05-24-2005, 07:13 AM here's my take on it - it's not the fastest but it most closely simulates the act of shuffling a deck as it might be done by a professional type Code:```int card, temp, i, shuffles, cutsplit, lefthand, righthand, whichhand; for (i=0; i < 52; i++)   card[i] = i; // reset the deck for (shuffles=1; shuffles <= 7; shuffles++) // shuffle the deck 7 times {   for (i=0; i < 52; i++)     temp[i] = card[i];   cutsplit = rand() % 3 + 24; //  cut the deck, make it slightly uneven sometimes   lefthand  = 0;   righthand = cutsplit;   whichhand = 1;   for (i=0; i < 52; i++)   {     if (rand() % 3 == 0) whichhand = -whichhand; // the act of shuffling will typically alternate left to right but add a 33% randomizing factor.     if      (whichhand == 1  && lefthand  >= cutsplit) whichhand = -whichhand; // if theres no card in the designated half, switch halves     else if (whichhand == -1 && righthand >= 52)       whichhand = -whichhand;     if      (whichhand == 1)  deck[i] = temp[lefthand++ ];     else if (whichhand == -1) deck[i] = temp[righthand++];     whichhand = -whichhand;   } }``` ignatures suck Anonymous Guest   05-24-2005, 10:49 AM Quote:Actually, I wont enter this challenge.. Sorry.. I was just curious to what you meant, and ofcourse it's all theoretical.. for a player to actually get an "ordered" hand is.. well.. rare.. thats called a straight o.o Pyrokid Senior Member    Posts: 319 Threads: 68 Joined: Mar 2005 05-24-2005, 04:02 PM Haha yeah really straights arnt THAT rare his world has been connected... Tied to the darkness. Soon to be completely eclipsed. There is so very much to learn... You understand so little. A meaningless effort. One who knows nothing can understand nothing. -Ansem Bringer of darkness and creator of the heartless aetherfox Posting Freak     Posts: 780 Threads: 49 Joined: Dec 2001 05-24-2005, 04:57 PM I was playing poker in quarantine after my exam today, i got 2 straight flushes... I've seen these sorts of challenges before, and if I was to code a solution, the Input deck would be a sorted deck, and the idea is to create a randomised output deck, Â·~Â¹'Â°Â¨Â°'Â¹i|Â¡~Ã¦thÃ©rFÃ²x~Â¡|iÂ¹'Â°Â¨Â°'Â¹~Â·- avinash.vora - http://www.avinashv.net shiftLynx Senior Member    Posts: 320 Threads: 9 Joined: Dec 2004 05-24-2005, 09:05 PM Here is the full code for my idea: http://download.cdsoft.co.uk/code/carddeck.zip This is the crux of it; the shuffling method: [syntax="c++"] /** * Randomly shuffles the array of cards. */ void CardDeck: huffle() { std::vector couples; int tempDeck; // can't shuffle 0 or 1 cards. if(m_availableCards.size() < 2) return; // copy the current deck. for(int i = 0; i < m_availableCards.size(); i++) tempDeck[i] = m_availableCards[i]; // seed the PNR generator. srand(time(0)); // separate half of the deck into a different list. for(int i = 1; i < m_availableCards.size(); i += 2) couples.push_back(i); // now swap elements randomly. for(int i = 0; i < m_availableCards.size(); i += 2) { int swapIndex = rand() % couples.size(); int deckIndex = couples[swapIndex]; // find an iterator to this item and remove it. std::vector::iterator item = std::find(couples.begin(), couples.end(), deckIndex); if(item != couples.end()) couples.erase(item); int temp = tempDeck[i]; tempDeck[i] = tempDeck[deckIndex]; tempDeck[deckIndex] = temp; } // now reconstruct the deck by randomly picking one from the top // and one from the bottom. int top = 0; int bottom = m_availableCards.size() - 1; // clear the existing available cards. m_availableCards.clear(); while(top <= bottom) { if((rand() % 2) == 1) m_availableCards.push_back(tempDeck[top++]); else m_availableCards.push_back(tempDeck[bottom--]); } } [/syntax] img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img] Pyrokid Senior Member    Posts: 319 Threads: 68 Joined: Mar 2005 05-24-2005, 09:56 PM ok now i got it. i added another checker to see if there were any repeats after i dealed them that would print out were the repeated cards were so i didnt have to check it all the time Code:```declare function rndcard\$() cls np=4 'Number of players. change to your needs 'dims the player array 'the first d is the number of players 'the second d is the number of in wich he gets the card '(sorry cant explain think of any other way to explain that) dim player\$(np,52) randomize timer for p=1 to np ' does for number of players for i=1 to (52/np) ' divides the deck    1    player\$(p,i) = rndcard\$() ' gets the card         for a= 1 to np    for n = 1 to (52/np)                          'this checks to make       if a=p and n=i then n=n+1       if player\$ (p,i) = player\$(a,n) then goto 1'sure the card isnt    next                                          'repeated    next next next 'the rest of this prints out the player and card names c=1 oldp=0 for p=1 to np r=1 locate r,c: print "Player "; p r=r+1 for i= 1 to (52/np)    r=r+1    locate r,c: print player\$(p,i) next c=c+18 next 'this just checks to make sure that there are no repeats y=20 for p=1 to np    for pa=1 to np       for i=1 to 53/np          for n=1 to 53/np             if player\$(p,i)=player\$(pa,n) then                if p<>pa and n<>i then                   locate y,1: print "error @ player ";p;" card number ";i;" = player ";pa;" card number ";n;                   y=y+1                end if             end if          next       next    next next do:loop until inkey\$=chr\$(27) function rndcard\$()         SS=int(rnd*4)+1 'this is selects the suit    card=int(rnd*13)+1 ' this selects the card number         'names the suits    select case SS    case 1       suit\$="Spades"    case 2       suit\$="Clubs"    case 3           suit\$="Diamonds"    case 4       suit\$="Hearts"    end select              'selects the card names    select case card    case 1       cname\$="Ace"    case 11       cname\$="Jack"    case 12       cname\$="Queen"    case 13       cname\$="King"    case else       cname\$=str(card)    end select         rndcard\$=cname\$ + " of " + suit\$ 'puts it all together           end function``` his world has been connected... Tied to the darkness. Soon to be completely eclipsed. There is so very much to learn... You understand so little. A meaningless effort. One who knows nothing can understand nothing. -Ansem Bringer of darkness and creator of the heartless phycowelder Member   Posts: 121 Threads: 21 Joined: Oct 2005 11-04-2005, 09:44 AM This is my take on it! it makes a full unshuffled deck(including jokers) it will shuffle 2 decks separately or together! slight modification can be used for any card game! Code:```DECLARE SUB makerealdeck ()      'MAKE INITIAL DECK DECLARE SUB shuffledeck1 ()      'SHUFFLES DECK1 DECLARE SUB shuffledeck2 ()      'SHUFFLES DECK2 DECLARE SUB shufflebothdecks ()  'SHUFFLES BOTH DECKS RANDOMIZE TIMER                  'RANDOMIZE THE SHUFFLE TYPE cards                       'VARIABLE ARRAY FOR CARD INFO suit AS STRING * 1              'HEARTS,DIAMONDS,SPADES,CLUBS vn AS STRING * 1                'A,2,3,4,5,6,7,8,9,10,J,Q,K,j an AS INTEGER                   '1,2,3,4,5,6,7,8,9,10,11,12,13,14 where AS INTEGER                '0:IN DISCARD PILE  1:IN DECK 2: IN HAND 3:ON TABLE shuffle AS INTEGER              'SHUFFLE NUMBER colr AS INTEGER                 'COLOR OF SAID CARD END TYPE TYPE card2                       'VARIABLE ARRAY FOR CARD INFO suit AS STRING * 1              'HEARTS,DIAMONDS,SPADES,CLUBS vn AS STRING * 1                'A,2,3,4,5,6,7,8,9,10,J,Q,K,j an AS INTEGER                   '1,2,3,4,5,6,7,8,9,10,11,12,13,14 where AS INTEGER                '0:IN DISCARD PILE  1:IN DECK 2: IN HAND 3:ON TABLE colr AS INTEGER                 'COLOR OF SAID CARD END TYPE DIM SHARED decktool(1 TO 54)  AS cards     ' \ DIM SHARED deck2(1 TO 54)  AS card2        '  \ ARRAYS FOR THE DECK INFO DIM SHARED deck1(1 TO 54)  AS card2        '  / DIM SHARED bothdecks(1 TO 108)  AS card2   '/ SCREEN 12 makerealdeck          'SUB FOR REAL DECK UNSHUFFLED shufflebothdecks      'SUB TO SHUFFLE 2 RANDOM DECKS USING DECK1 AND DECK2 shuffledeck1          'SUB TO RANDOMLY SHUFFLE FIRST DECK shuffledeck2          'SUB TO RANDOMLY SHUFFLE SECOND DECK FOR i = 0 TO 12 l = i * 4 COLOR deck1(l + 1).colr PRINT deck1(l + 1).suit; deck1(l + 1).vn; COLOR deck1(l + 2).colr PRINT deck1(l + 2).suit; deck1(l + 2).vn; COLOR deck1(l + 3).colr PRINT deck1(l + 3).suit; deck1(l + 3).vn; COLOR deck1(l + 4).colr PRINT deck1(l + 4).suit; deck1(l + 4).vn; "    "; COLOR deck2(l + 1).colr PRINT deck2(l + 1).suit; deck2(l + 1).vn; COLOR deck2(l + 2).colr PRINT deck2(l + 2).suit; deck2(l + 2).vn; COLOR deck2(l + 3).colr PRINT deck2(l + 3).suit; deck2(l + 3).vn; COLOR deck2(l + 4).colr PRINT deck2(l + 4).suit; deck2(l + 4).vn NEXT i FOR i = 0 TO 12 l = i * 8 COLOR bothdecks(l + 1).colr PRINT bothdecks(l + 1).suit; bothdecks(l + 1).vn; COLOR bothdecks(l + 2).colr PRINT bothdecks(l + 2).suit; bothdecks(l + 2).vn; COLOR bothdecks(l + 3).colr PRINT bothdecks(l + 3).suit; bothdecks(l + 3).vn; COLOR bothdecks(l + 4).colr PRINT bothdecks(l + 4).suit; bothdecks(l + 4).vn; COLOR bothdecks(l + 5).colr PRINT bothdecks(l + 5).suit; bothdecks(l + 5).vn; COLOR bothdecks(l + 6).colr PRINT bothdecks(l + 6).suit; bothdecks(l + 6).vn; COLOR bothdecks(l + 7).colr PRINT bothdecks(l + 7).suit; bothdecks(l + 7).vn; COLOR bothdecks(l + 8).colr PRINT bothdecks(l + 8).suit; bothdecks(l + 8).vn NEXT i SUB makerealdeck ' ' 'TO MAKE THE TRUE UN SHUFFLED DECK 'i=SUIT CHR\$(NUMBER) AND MULTIPLYER FOR THE CARD MAKING 'ii= NUMBER OF CARDS PER SUIT ' ' FOR i = 3 TO 6 FOR ii = 1 TO 13   IF i = 3 OR i = 4 THEN decktool(((i - 3) * 13) + ii).colr = 4  'NUMBER FOR RED CARDS   IF i = 5 OR i = 6 THEN decktool(((i - 3) * 13) + ii).colr = 8  'NUMBER FOR BLACK CARDS   decktool(((i - 3) * 13) + ii).suit = CHR\$(i)                   'SETTING SUIT   decktool(((i - 3) * 13) + ii).vn = CHR\$(ii + 48)               'SETTING VISUAL NUMBERS    IF decktool(((i - 3) * 13) + ii).vn = CHR\$(49) THEN decktool(((i - 3) * 13) + ii).vn = "A"         'TO MAKE vn A IF 1    IF decktool(((i - 3) * 13) + ii).vn = CHR\$(58) THEN decktool(((i - 3) * 13) + ii).vn = CHR\$(48)    'TO MAKE vn 10 IF CHR\$(58) OR :    IF decktool(((i - 3) * 13) + ii).vn = CHR\$(59) THEN decktool(((i - 3) * 13) + ii).vn = "J"         'TO MAKE vn J IF 11    IF decktool(((i - 3) * 13) + ii).vn = CHR\$(60) THEN decktool(((i - 3) * 13) + ii).vn = "Q"         'TO MAKE vn Q IF 12    IF decktool(((i - 3) * 13) + ii).vn = CHR\$(61) THEN decktool(((i - 3) * 13) + ii).vn = "K"         'TO MAKE vn K IF 13   decktool(((i - 3) * 13) + ii).an = ii                          'SETTING ACTUAL NUMBERS 1 - 13   decktool(((i - 3) * 13) + ii).where = 1                        'WHERE ALWAYS EQUALS IN THE DECK AT FIRST NEXT ii NEXT i decktool(53).suit = "r" '\ decktool(53).an = 14    ' \ decktool(53).vn = "j"   '  \ decktool(53).where = 1  '   \ decktool(53).colr = 4   '    \  SETTING WHERE THE JOCKERS GO decktool(54).suit = "b" '    / decktool(54).an = 14    '   / decktool(54).vn = "j"   '  / decktool(54).where = 1  ' / decktool(54).colr = 8   '/ END SUB SUB shufflebothdecks shuffledeck1 shuffledeck2 ' ' 'SHUFFLING BOTH DECKS IS EASY EACH NUMBER 1 - 54 OF BOTH DECK1 AND DECK2 'EQUALS ITS (NUMBER*2-1) FOR THE FIRST DECK 'AND EQUALS ITS (NUMBER*2) FOR THE SECOND DECK ' ' FOR i = 1 TO 54 bothdecks(i * 2 - 1).suit = deck1(i).suit bothdecks((i * 2)).suit = deck2(i).suit bothdecks(i * 2 - 1).an = deck1(i).an bothdecks((i * 2)).an = deck2(i).an bothdecks(i * 2 - 1).vn = deck1(i).vn bothdecks((i * 2)).vn = deck2(i).vn bothdecks(i * 2 - 1).colr = deck1(i).colr bothdecks((i * 2)).colr = deck2(i).colr bothdecks(i * 2 - 1).where = 1 bothdecks((i * 2)).where = 1 NEXT i END SUB SUB shuffledeck1 FOR i = 1 TO 54            '\ decktool(i).shuffle = 0   ' >RESET SHUFFLE NUMBER NEXT i                     '/ FOR i = 1 TO 54                    '\    i=SHUFFLE NUMBER DO:                               ' \   THE SHUFFLE NUMBER ii = INT(RND(1) * 54 + 1)         '  \  WILL BE ADDED TO A IF decktool(ii).shuffle = 0 THEN  '   \ CARD THAT DOES NOT   decktool(ii).shuffle = i         '    >HAVE A SHUFFLE NUMBER   EXIT DO                          '   / IF IT HAS A SHUFFLE NUMBER END IF                            '  /  IT WILL FIND A NEW CARD LOOP                              ' /   FOR THAT SHUFFLE NUMBER NEXT i                             '/   FOR i = 1 TO 54                      '\ ii = 1                              ' \ DO:                                 '  \   IF decktool(ii).shuffle = i THEN   '   \    IT WILL NOW FIND THE FIRST    deck1(i).suit = decktool(ii).suit '    \   TO THE LAST SHUFFLE NUMBER    deck1(i).an = decktool(ii).an     '     \  MAKING EACH NUMBER FOLLOW    deck1(i).vn = decktool(ii).vn     '      \ ITS SELF IN THE NEW DECK    deck1(i).colr = decktool(ii).colr '      / DECK1 IS NOW SHUFFLED AND NOT    deck1(i).where = 1                '     /  NUMERICALY ORDERS LIKE THE    EXIT DO                           '    /   TOOL DECK IS   END IF                             '   /   ii = ii + 1                        '  / LOOP                                ' / NEXT i                               '/ END SUB SUB shuffledeck2 FOR i = 1 TO 54            '\ decktool(i).shuffle = 0   ' >RESET SHUFFLE NUMBER NEXT i                     '/ FOR i = 1 TO 54                    '\    i=SHUFFLE NUMBER DO:                               ' \   THE SHUFFLE NUMBER ii = INT(RND(1) * 54 + 1)         '  \  WILL BE ADDED TO A IF decktool(ii).shuffle = 0 THEN  '   \ CARD THAT DOES NOT   decktool(ii).shuffle = i         '    >HAVE A SHUFFLE NUMBER   EXIT DO                          '   / IF IT HAS A SHUFFLE NUMBER END IF                            '  /  IT WILL FIND A NEW CARD LOOP                              ' /   FOR THAT SHUFFLE NUMBER NEXT i                             '/ FOR i = 1 TO 54                      '\ ii = 1                              ' \ DO:                                 '  \   IF decktool(ii).shuffle = i THEN   '   \    IT WILL NOW FIND THE FIRST    deck2(i).suit = decktool(ii).suit '    \   TO THE LAST SHUFFLE NUMBER    deck2(i).an = decktool(ii).an     '     \  MAKING EACH NUMBER FOLLOW    deck2(i).vn = decktool(ii).vn     '      \ ITS SELF IN THE NEW DECK    deck2(i).colr = decktool(ii).colr '      / DECK2 IS NOW SHUFFLED AND NOT    deck2(i).where = 1                '     /  NUMERICALY ORDERS LIKE THE    EXIT DO                           '    /   TOOL DECK IS   END IF                             '   /   ii = ii + 1                        '  / LOOP                                ' / NEXT i                               '/ END SUB``` t is better to error on the side of caution than the side of haste!!!  stylin Senior Member    Posts: 324 Threads: 12 Joined: Nov 2005 11-04-2005, 12:07 PM Here's my go at it, in C++: Code:```#include #include namespace cards {    // suit and rank constants    const unsigned int spades = 0;    const unsigned int clubs = 1;    const unsigned int hearts = 2;    const unsigned int diamonds = 3;    const unsigned int total_suits = 4;    const unsigned int ace = 0;    const unsigned int two = 1;    const unsigned int three = 2;    const unsigned int four = 3;    const unsigned int five = 4;    const unsigned int six = 5;    const unsigned int seven = 6;    const unsigned int eight = 7;    const unsigned int nine = 8;    const unsigned int ten = 9;    const unsigned int jack = 10;    const unsigned int queen = 11;    const unsigned int king = 12;    const unsigned int total_ranks = 13;    // card class    struct card {    private:       unsigned int suit_;       unsigned int rank_;    public:       card( unsigned int suit, unsigned int rank ): suit_(suit), rank_(rank) {};       const unsigned int suit() const { return suit_; }       const unsigned int rank() const { return rank_; }    }; } int main() {    // create a deck    std::vector< cards::card > deck;    // fill it with cards    for( unsigned int suit = 0; suit < cards::total_suits; ++suit )       for( unsigned int rank = 0; rank < cards::total_ranks; ++rank )          deck.push_back( cards::card( suit, rank ) );    // and shuffle it    std::random_shuffle( deck.begin(), deck.end() );    return 0; };``` Note that std::random_shuffle will give uniform distribution. stylin: « Next Oldest | Next Newest »

Forum Jump:

Users browsing this thread: 1 Guest(s) 