简体   繁体   中英

JavaScript slider puzzle

Ultimately I'm making a slider puzzle with a table, but at the moment I am generating a new puzzle and shuffling the tiles, but it appears that something in my code is making this work incorrectly. (not all the table cells are populated, etc.) Any idea what could be causing this?

In the program I am writing, I am using body onload to build the puzzle but for some reason that won't work in the Fiddle:

</head>
<body onload="newPuzzle(_r, _c)">
</body>

Fiddle Example

Try changing

//declare and populate array
 var _array = new Array();

 for (var i = 0; i <= r*c; i++)
 {
   _array[i] = i;
 }

to

//declare and populate array
 var _array = new Array();

 for (var i = 1; i < r*c; i++)
 {
   _array[i] = i;
 }
   _array[0] = "";

AND changing this:

var rand = Math.floor(Math.random() * _array.length) + i;

to this:

var rand = Math.floor(Math.random() * _array.length);

Just a note, as you're shuffling with a new array, there is not point in the first configuration (0 to 8), that way you can remove two FOR s and just set a fixed value when generating it (before shuffle). Like so:

gridTable[0].rows[i].cells[j].innerHTML = "0";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM