Shuffling a deck of cards

Discussion in 'Javascript' started by tofu12, Apr 9, 2009.

  1. tofu12

    tofu12 Guest

    Hello I am trying to shuffle a deck of 25 cards and have the user
    select five random cards after the shuffle. On choosing the cards they
    turn over displaying the card image. The cards are shuffling, but when
    the user attempts to choose five cards only three or two of the card
    images appear. Can someone help here?

    Code submitted below
    var Thumbimgs="images/thumbs/";
    var NumOfCards=25;
    var count=0;
    var CardsInSpread=5;

    SelectedCards=new Array();
    function Selected(CardPosition, CardNumber)
    {
    var CardNumber= Math.round((NumOfCards-1)*Math.random());
    if (count < CardsInSpread) {
    for (var i = 0; i <= NumOfCards; i++)
    {
    if (SelectedCards == CardNumber) {return;}
    }
    SelectedCards[count] = CardNumber;
    count=count+1;
    document.images[CardPosition+1].src = Thumbimgs + CardNumber
    + ".gif";
    }
    }
    tofu12, Apr 9, 2009
    #1
    1. Advertising

  2. On Wed, 08 Apr 2009 16:05:32 -0700, tofu12 wrote:

    > Hello I am trying to shuffle a deck of 25 cards and have the user select
    > five random cards after the shuffle. On choosing the cards they turn
    > over displaying the card image. The cards are shuffling, but when the
    > user attempts to choose five cards only three or two of the card images
    > appear. Can someone help here?
    >
    > Code submitted below


    > var Thumbimgs="images/thumbs/";
    > var NumOfCards=25;


    > var count=0;

    Should be inside your function as a local variable.

    > var CardsInSpread=5;
    >
    > SelectedCards=new Array();


    Better written as (no var?)

    SelectedCards=[];


    > function Selected(CardPosition, CardNumber)
    > {
    > var CardNumber= Math.round((NumOfCards-1)*Math.random());
    > if (count < CardsInSpread) {
    > for (var i = 0; i <= NumOfCards; i++)
    > {
    > if (SelectedCards == CardNumber) {return;}
    > }
    > SelectedCards[count] = CardNumber;


    > count=count+1;


    Better written as
    count++;


    > document.images[CardPosition+1].src = Thumbimgs +

    CardNumber
    > + ".gif";


    I'd give the images an ID, rather than relying on the images collection.
    Lets you re-arrange the page.

    > }
    > }


    Post a link.
    Jeremy J Starcher, Apr 9, 2009
    #2
    1. Advertising

  3. tofu12

    tofu12 Guest

    On Apr 8, 7:10 pm, Jeremy J Starcher <> wrote:
    > On Wed, 08 Apr 2009 16:05:32 -0700, tofu12 wrote:
    > > Hello I am trying to shuffle a deck of 25 cards and have the user select
    > > five random cards after the shuffle. On choosing the cards they turn
    > > over displaying the card image. The cards are shuffling, but when the
    > > user attempts to choose five cards only three or two of the card images
    > > appear. Can someone help here?

    >
    > > Code submitted below
    > > var Thumbimgs="images/thumbs/";
    > > var NumOfCards=25;
    > > var count=0;

    >
    > Should be inside your function as a local variable.
    >
    > > var CardsInSpread=5;

    >
    > > SelectedCards=new Array();

    >
    > Better written as  (no var?)
    >
    > SelectedCards=[];
    >
    > >  function Selected(CardPosition, CardNumber)
    > >      {
    > >        var CardNumber= Math.round((NumOfCards-1)*Math.random());
    > >          if (count < CardsInSpread) {
    > >          for (var i = 0; i <= NumOfCards; i++)
    > >              {
    > >                  if (SelectedCards == CardNumber) {return;}
    > >              }
    > >            SelectedCards[count] = CardNumber;
    > >            count=count+1;

    >
    > Better written as
    > count++;
    >
    >
    >
    > >            document.images[CardPosition+1].src = Thumbimgs +

    > CardNumber
    > > + ".gif";

    >
    > I'd give the images an ID, rather than relying on the images collection.  
    > Lets you re-arrange the page.
    >
    > >          }
    > >       }

    >
    > Post a link.


    thank you I'l give it a try
    tofu12, Apr 9, 2009
    #3
  4. In comp.lang.javascript message <ba105b8f-f344-4fb3-be5c-d690f0b7d300@l1
    9g2000vba.googlegroups.com>, Wed, 8 Apr 2009 16:05:32, tofu12
    <> posted:
    >Hello I am trying to shuffle a deck of 25 cards and have the user
    >select five random cards after the shuffle. On choosing the cards they
    >turn over displaying the card image. The cards are shuffling, but when
    >the user attempts to choose five cards only three or two of the card
    >images appear. Can someone help here?
    >
    >Code submitted below
    >var Thumbimgs="images/thumbs/";
    >var NumOfCards=25;
    >var count=0;
    >var CardsInSpread=5;
    >
    >SelectedCards=new Array();
    > function Selected(CardPosition, CardNumber)
    > {
    > var CardNumber= Math.round((NumOfCards-1)*Math.random());
    > if (count < CardsInSpread) {
    > for (var i = 0; i <= NumOfCards; i++)
    > {
    > if (SelectedCards == CardNumber) {return;}
    > }
    > SelectedCards[count] = CardNumber;
    > count=count+1;
    > document.images[CardPosition+1].src = Thumbimgs + CardNumber
    >+ ".gif";
    > }
    > }



    Firstly, your approach is at best confusing.

    Secondly, the "-1" in it seems likely to be a mistake. Read the
    newsgroup FAQ on Math.random. The Web site seems broken at the moment,
    though.

    Thirdly, you do not seem to be shuffling at all; you have no obvious
    input deck. Instead, you seem to be dealing, where (as far as the card-
    table is concerned) cards are generated (from another dimension) in
    random order.

    Fourthly, sample code should be executable as posted, without need for
    line de-wrap or writing a test harness.

    Using WayBack, comp.lang.javascript FAQ - 9.88 - 2007-10-12 <http://web.
    archive.org/web/20071030072818/http://jibbering.com/faq/index.html>
    Section 4.22 has what you need; I cannot recall whether the present FAQ
    still does; see <http://www.merlyn.demon.co.uk/js-randm.htm>.
    Alternatively, read Knuth. One wonders why Wayback seems to have no
    more recent copy of the FAQ.

    The simplest approach is probably to use code for a full Deal, and then
    just pick the number of cards that you want by setting the length of the
    returned array. But js-randm.htm should have more efficient code,
    useful if you need to handle larger numbers of cards.

    function Deal(N) { var J, K, Q = new Array(N)
    for (J=0 ; J<N ; J++)
    { K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
    return Q }

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
    Dr J R Stockton, Apr 9, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun Nair

    Cards deck problem

    Arun Nair, Oct 26, 2006, in forum: Python
    Replies:
    13
    Views:
    1,143
    Ben Finney
    Oct 28, 2006
  2. Pratik
    Replies:
    4
    Views:
    320
    Jeff Dege
    Feb 19, 2007
  3. DevNull
    Replies:
    7
    Views:
    450
    kwikius
    Feb 27, 2007
  4. Army1987

    portably shuffle a deck

    Army1987, Aug 1, 2007, in forum: C Programming
    Replies:
    30
    Views:
    1,076
    Eric Sosman
    Aug 3, 2007
  5. RVic

    Cutting a deck of cards

    RVic, May 26, 2013, in forum: Python
    Replies:
    17
    Views:
    175
    Lee Crocker
    Jun 2, 2013
Loading...

Share This Page