Velocity Reviews > Shuffling a deck of cards

# Shuffling a deck of cards

tofu12
Guest
Posts: n/a

 04-08-2009
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[i] == CardNumber) {return;}
}
SelectedCards[count] = CardNumber;
count=count+1;
document.images[CardPosition+1].src = Thumbimgs + CardNumber
+ ".gif";
}
}

Jeremy J Starcher
Guest
Posts: n/a

 04-09-2009
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[i] == 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.

tofu12
Guest
Posts: n/a

 04-09-2009
On Apr 8, 7:10*pm, Jeremy J Starcher <(E-Mail Removed)> 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[i] == 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

Dr J R Stockton
Guest
Posts: n/a

 04-09-2009
In comp.lang.javascript message <ba105b8f-f344-4fb3-be5c-d690f0b7d300@l1
9g2000vba.googlegroups.com>, Wed, 8 Apr 2009 16:05:32, tofu12
<(E-Mail Removed)> 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[i] == 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)

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Pratik C++ 4 02-19-2007 01:50 PM Arun Nair Python 13 10-28-2006 12:18 AM Silverstrand Front Page News 0 08-15-2006 04:57 PM bigal Hardware 20 01-04-2006 02:22 PM sanpablo DVD Video 0 05-27-2005 02:39 PM

Advertisments