Jump to content

Javascript Memory Game


Andrew2010

Recommended Posts

Hi everybody,

 

I've added a memory game to my site. The game has 6 rows, and 5 columns (30 cards), and I was wondering if someone could fix the script so it has 5 rows and 6 colums.

 

I'm not sure which script controls it, so here's them both:

 

document.write('<div align="center"><table cellpadding="0" cellspacing="0" border="0">');for (var a = 0; a <= 5; a++) {document.write('<tr>');for (var b = 0; b <= 4; b++) {document.write('<td align="center" class="blk" id="t'+((5*a)+b)+'"></td>');}document.write('<\/tr>');}document.write('<\/table><form name="mem"><input type="button" id="cnt" value="0:00" onclick="window.start()" \/><\/form><\/div>');
                    

 

var back = 'images/back.gif';
var tile = ['images/card0.gif','images/card1.gif','images/card2.gif','images/card3.gif','images/card4.gif','images/card5.gif',
'images/card6.gif','images/card7.gif','images/card8gif','images/card9.gif','images/card10.gif','images/card11.gif',
'images/card12.gif','images/card13.gif','images/card14.gif'];

function randOrd(a, b){return (Math.round(Math.random())-0.5);} var im = []; for (var i = 0; i < 15; i++) {im[i] = new Image(); im[i].src = tile[i]; tile[i] = '<img src="'+tile[i]+'" width="60" height="100" alt="" \/>'; tile[i+15] = tile[i];} function displayBack(i) {document.getElementById('t'+i).innerHTML = '<div onclick="disp('+i+');return false;"><img src="'+back+'" width="60" height="100" alt="back" \/><\/div>';} var ch1, ch2, tmr, tno, tid, cid, cnt; window.onload=start; function start() {for (var i = 0; i <= 29 ;i++) displayBack(i);clearInterval(tid);tmr = tno = cnt = 0;tile.sort( randOrd );cntr(); tid = setInterval('cntr()', 1000);} function cntr() {var min = Math.floor(tmr/60);var sec = tmr%60;document.getElementById('cnt').value = min+':'+ (sec<10 ? '0' : '') + sec;tmr++;} function disp(sel) {if (tno>1) {clearTimeout(cid); conceal();}document.getElementById('t'+sel).innerHTML = tile[sel];if (tno==0) ch1 = sel;else {ch2 = sel;  cid = setTimeout('conceal()', 900);}tno++;} function conceal() {tno = 0; if (tile[ch1] != tile[ch2]) {displayBack(ch1);displayBack(ch2);} else cnt++; if (cnt >= 15) clearInterval(tid);}
                    

 

Thanks!!

Link to comment
Share on other sites

Hmm, I'm not going to take the time to try and interpret that code - especially since you didn't bother adding line breaks or any other formatted to help interpret it.

 

But, you can try changing

(var a = 0; a <= 5; a++)

To

(var a = 0; a < 5; a++)

 

 

And

(var b = 0; b <= 4; b++)

 

To

(var b = 0; b < 6; b++)

Link to comment
Share on other sites

I tried what you said, it adds 6 only on the first row. :o

Here's the code with linebreaks:

 

document.write('<div align="center"><table cellpadding="0" cellspacing="0" border="0">');for (var a = 0; a <= 5; a++) 
{document.write('<tr>');for (var b = 0; b <= 4; b++) {document.write('<td align="center" class="blk" id="t'+((5*a)+b)+'"></td>');}
document.write('<\/tr>');}document.write('<\/table><form name="mem"><input type="button" id="cnt" value="0:00" onclick="window.start()" \/><\/form><\/div>');
                    

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.