Shuffling a deck of cards

T

tofu12

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";
}
}
 
J

Jeremy J Starcher

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

tofu12

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
 
D

Dr J R Stockton

In comp.lang.javascript message <ba105b8f-f344-4fb3-be5c-d690f0b7d300@l1
9g2000vba.googlegroups.com>, Wed, 8 Apr 2009 16:05:32, tofu12
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 }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top