Qbasicnews.com
August 09, 2020, 11:39:06 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: card shuffler  (Read 10239 times)
aetherfox
Been there, done that
*****
Posts: 1071



WWW
« Reply #15 on: May 24, 2005, 08:57:13 AM »

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,
Logged

~''i|~thrFx~|i''~-  
avinash.vora - http://www.avinashv.net
shiftLynx
Wandering Guru
***
Posts: 340



WWW
« Reply #16 on: May 24, 2005, 01:05:00 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::shuffle()
{
    std::vector<int> couples;
    int tempDeck[52];
   
    // 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 = m_availableCards;
   
    // 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<int>::iterator item = std::find(couples.begin(),
                                                    couples.end(),
                                                    deckIndex);

        if(item != couples.end())
            couples.erase(item);
       
        int temp = tempDeck;
        tempDeck = 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]
Logged

img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Pyrokid
Wandering Guru
***
Posts: 355



WWW
« Reply #17 on: May 24, 2005, 01:56:59 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
Logged

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
Forum Regular
**
Posts: 136



WWW
« Reply #18 on: November 04, 2005, 01:44:28 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 <JUST EXAMPLES OF WHERE COULD MEAN>
 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 <JUST EXAMPLES OF WHERE COULD MEAN>
 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

Logged

t is better to error on the side of caution
than the side of haste!!!
stylin
Ancient QBer
****
Posts: 445


« Reply #19 on: November 04, 2005, 04:07:12 AM »

Here's my go at it, in C++:

Code:
#include <vector>
#include <algorithm>

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.
Logged

stylin:
Moneo
Na_th_an
*****
Posts: 1971


« Reply #20 on: November 04, 2005, 11:08:40 PM »

phycowelder,

Your code has several statements that look like this:
Code:

ii = INT(RND(1) * 54 + 1)

That's not going to work. For random numbers from 1 to 54, you should have:
Code:

ii = INT(RND*(54 - 1 + 1)) + 1
which becomes:
ii = INT(RND*(54)) + 1


But don't worry, lots of people get this mixed up.
*****
Logged
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #21 on: November 05, 2005, 02:20:25 AM »

Ahh... yep. I used to do that same exact thing. Wink

One extremely minor difference though... Somehow, I picked up the habit of doing it backwards.


Code:
ii = 1 + INT(RND * 54 )


Not like it realy matters, but it is pretty funny.  :lol:
Logged
Torahteen
Ancient Guru
****
Posts: 744



« Reply #22 on: November 05, 2005, 02:22:32 AM »

Potato Patato (not sure how to do that, but you get my point)
Logged

quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!