I'm struggling with this puzzle game. Sometimes, when I start a new game, the pieces get shuffled incorrectly. I tried adding a function to fix it, but it doesn't seem to be working. It's really frustrating and I want to simplify the code as well.
var count = 0;
var moves = 0;
var CheckArray = new Array(9);
function swapTiles(cell1, cell2) {
var elem1 = document.getElementById(cell1),
elem2 = document.getElementById(cell2);
var tempClass = elem1.className;
var tempText = elem1.textContent;
elem1.className = elem2.className;
elem1.textContent = elem2.textContent;
elem2.className = tempClass;
elem2.textContent = tempText;
}
function shuffle() {
for (var row = 1; row <= 3; row++) {
for (var column = 1; column <= 3; column++) {
var row2 = Math.floor(Math.random() * 3 + 1);
var column2 = Math.floor(Math.random() * 3 + 1);
swapTiles("cell" + row + column, "cell" + row2 + column2);
}
}
Check();
}
//More functions and code here...
body {
background: #6ca0e4c4;
}
.tile1,
.tile2,
.tile3,
.tile4,
.tile5,
.tile6,
.tile7,
.tile8,
.tile9 {
//CSS styles here...
}
// HTML code omitted for brevity.