/    Sign up×
Community /Pin to ProfileBookmark

Javascript Shuffle Image

hi, im new to javascript and im trying to program a small card memory game. im having troubles with shuffling the cards on startup. can anyone help me do it? thanks

[code]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Untitled Document</title>

<SCRIPT LANGUAGE=”JAVASCRIPT” TYPE=”TEXT/JAVASCRIPT”>
<!–
var cards = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8); //fill the cards array with unshuffled pair numbers
var matched = new Array(16);
var clicked = 0;
var matchCount = 0;

var firstCard;
var pair;
shuffle(cards); //call the shuffle function

function shuffle(deck)
{

}

function turnOver(e)
{
if (matchCount<8 & !matched[e.name])
{

if (clicked<1)
{
firstCard=e; //grab first card object
pair=cards[firstCard.name]; //get first pair number from cards array
e.src=document.images[“load”+pair].src; //flip over first card
clicked++;
}
else
{
e.src=document.images[“load”+cards[e.name]].src; //flip over second card
if (pair!==cards[e.name])
{ //check for snap
alert(“That’s not a match, try again!”);
firstCard.src=document.images[“load0”].src; //flip first card back
e.src=document.images[“load0”].src; //flip second card back
}
else
{
//This is a match so record matched cards and increase matchCount
matched[firstCard.name]=true;
matched[e.name]=true;
matchCount++;
}
clicked=0;
}
}
}
//–>
</SCRIPT>

<style type=”text/css”>
<!–
body
{
background-color: #FFFFFF;
}
body,td,th
{
font-family: Verdana, Arial, Helvetica, sans-serif;
}
–>
</style>
</head>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinNov 09.2006 — felgal dude helped me to learn how to random sort an array, have a look...

[SIZE=1]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD><TITLE>RandomSortArray</TITLE>

<STYLE type=text/CSS>



</STYLE>

<SCRIPT type="text/javascript">

var files= new Array(1,2,3,4,5,6,7,8,9);



function startem(){



//whatYouNeedToRandomSortTheArray

function randOrd(){return (Math.round(Math.random())-0.5); }

files.sort(randOrd);

//BetweenHere

for (i=0; i<files.length; i++) {

document.getElementById('whats').innerHTML+= files[i]+' ';

}

}

</SCRIPT>

</HEAD>

<BODY onload="startem()">

<div id="whats"></div>

</BODY></HTML>

[/SIZE]
×

Success!

Help @JJ2005 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...