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
https://forums.phpfreaks.com/topic/218763-javascript-memory-game/
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++)

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>');
                    

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.