0

I'm wondering how to "shuffle" my 2d array. I'm creating a memory-match flip-card game and would appreciate some assistance or a push in the right direction.

My 2d array consists of 6 "levels" of images in a 6x6 grid

What I want to do is create a function that spits out pairs of 2 random integers in the format of my 2d array.

For example, this is my array:

var imgArray = [['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png'], 
['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png'], 
['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png'], 
['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png'], 
['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png'], 
['a.png', 'b.png', 'c.png', 'd.png', 'e.png', 'f.png', 'g.png']];


function onClickCard(image)
{
    image.src=imgArray[parseInt(image.name.substring(0, 1)) - 1][parseInt(image.name.substring(1, 2)) - 1];

}

so my images are saved in the divs as; 11, 12, 13, 14, 15, and 16. this pattern continues in increments of 1 all the way to 61, 62, 63, 64, 65, and 66 since there are 6 layers of the 2d array.

So what I want to do is create a variable that randomizes the array and splices the numbers back into a new array.

I tried to do this myself and this is what I came up with:

            var ImgLocArrayRow1 = new Array(11,12,13,14,15,16);
        var ImgLocArrayRow2 = new Array(21,22,23,24,25,26);
        var ImgLocArrayRow3 = new Array(31,32,33,34,35,36);
        var ImgLocArrayRow4 = new Array(41,42,43,44,45,46);
        var ImgLocArrayRow5 = new Array(51,52,53,54,55,56);
        var ImgLocArrayRow6 = new Array(61,62,63,64,65,66);
        var ImgLocArrayGM = new Array(ImgLocArrayRow1, ImgLocArrayRow2,       ImgLocArrayRow3, ImgLocArrayRow4, ImgLocArrayRow5, ImgLocArrayRow6);
var ArrayString = join(ImgLocArrayGM);
var Randomizah = Math.floor(Math.random() * ArrayString.length - 1, 0);
var RandomizahRegulatah = imgArray.splice(Randomizah, 1)[ImgLocArrayGM];

I expect this to spit out a random number between 11 through to 66, yet I get an error stating "join is not defined".

Thank you in advance for taking the time to read/assist me with this.

user2825148
  • 21
  • 1
  • 6
  • 2
    did you mean `ImgLocArrayGM.join()`? – akonsu Oct 16 '13 at 03:36
  • If you don't need consistency between how all arrays are shuffled, read this question: http://stackoverflow.com/questions/2450954 . Also, the way your arrays look like raises a suspicion: why are they all alike? Why do you need so many of the same thing? It feels wrong. –  Oct 16 '13 at 09:03

1 Answers1

0

Why not just this:

var rnd = Math.floor(Math.random()*6+1)*10+Math.floor(Math.random()*6+1)
Flight Odyssey
  • 2,267
  • 18
  • 25
  • That works very well, however, when I attempt to put rnd into the div I get "TypeError: imgArray[(parseInt(...) -1)] is undefined". Any idea why this is? – user2825148 Oct 16 '13 at 04:30