HTML ⛶ <!-- Sliding Puzzle by 101Computing - www.101computing.net/sliding-puzzle/--> <center><div id="table" style="display: table;"> <div id="row1" style="display: table-row;"> <div id="cell11" class="tile1" onClick="clickTile(1,1);"></div> <div id="cell12" class="tile2" onClick="clickTile(1,2);"></div> <div id="cell13" class="tile3" onClick="clickTile(1,3);"></div> </div> <div id="row2" style="display: table-row;"> <div id="cell21" class="tile4" onClick="clickTile(2,1);"></div> <div id="cell22" class="tile5" onClick="clickTile(2,2);"></div> <div id="cell23" class="tile6" onClick="clickTile(2,3);"></div> </div> <div id="row3" style="display: table-row;"> <div id="cell31" class="tile7" onClick="clickTile(3,1);"></div> <div id="cell32" class="tile8" onClick="clickTile(3,2);"></div> <div id="cell33" class="tile9" onClick="clickTile(3,3);"></div> </div> </div> <button onClick="shuffle();">New Game</button> </center>
CSS 🛠⛶ BODY { background: #AAAAAA; } .tile1, .tile2, .tile3, .tile4, .tile5, .tile6, .tile7, .tile8, .tile9 { display: table-cell; width: 120px; height: 120px; border: 1px solid white; background: url("https://www.101computing.net/wp/wp-content/uploads/flower.png"); cursor: pointer; } .tile1 {background-position: left top;} .tile2 {background-position: center top;} .tile3 {background-position: right top;} .tile4 {background-position: left center;} .tile5 {background-position: center center;} .tile6 {background-position: right center;} .tile7 {background-position: left bottom;} .tile8 {background-position: center bottom;} .tile9 {background: white; cursor: default;}
JavaScript ⛶ function swapTiles(cell1,cell2) { var temp = document.getElementById(cell1).className; document.getElementById(cell1).className = document.getElementById(cell2).className; document.getElementById(cell2).className = temp; } function shuffle() { //Use nested loops to access each cell of the 3x3 grid for (var row=1;row<=3;row++) { //For each row of the 3x3 grid for (var column=1;column<=3;column++) { //For each column in this row var row2=Math.floor(Math.random()*3 + 1); //Pick a random row from 1 to 3 var column2=Math.floor(Math.random()*3 + 1); //Pick a random column from 1 to 3 swapTiles("cell"+row+column,"cell"+row2+column2); //Swap the look & feel of both cells } } } function clickTile(row,column) { var cell = document.getElementById("cell"+row+column); var tile = cell.className; if (tile!="tile9") { //Checking if white tile on the right if (column<3) { if ( document.getElementById("cell"+row+(column+1)).className=="tile9") { swapTiles("cell"+row+column,"cell"+row+(column+1)); return; } } //Checking if white tile on the left if (column>1) { if ( document.getElementById("cell"+row+(column-1)).className=="tile9") { swapTiles("cell"+row+column,"cell"+row+(column-1)); return; } } //Checking if white tile is above if (row>1) { if ( document.getElementById("cell"+(row-1)+column).className=="tile9") { swapTiles("cell"+row+column,"cell"+(row-1)+column); return; } } //Checking if white tile is below if (row<3) { if ( document.getElementById("cell"+(row+1)+column).className=="tile9") { swapTiles("cell"+row+column,"cell"+(row+1)+column); return; } } } }