$(document).ready(function() {
var zi = 1;
var EmptySquare = 16;
$.fn.extend({ fifteen:
function(square_size) {
var gameObjectElement = '#' + $(this).attr('id');
var sqSize = square_size + 'px';
var boardSize = (square_size * 4) + 'px';
$(gameObjectElement).html('<div id="board"></div>');
$('#board').css({ position: 'absolute', width: boardSize, height: boardSize, border: '1px solid gray'});
for(var i = 0; i < 16; i++) {
$('#board').append("<div class='" + i + "' style='left:" + ((i % 4) * square_size) + "px; top: " + Math.floor(i / 4) * square_size + "px; width: "
+ square_size + "px; height: " + square_size + "px; background-position: " + (-(i % 4) * square_size) + "px "
+ -Math.floor(i / 4) * square_size + "px; '></div>");
// Select the children of #board ID that are divs, and the 16th one, then make backgroundImage nothing, and background color of white.
// This emptys the 16th square.
$('#board').children("div:nth-child(" + EmptySquare + ")").css({backgroundImage: "", background: "#ffffff"});
// Attach a click event to each of the squares, or divs.
$('#board').children('div').click(function() {
Move(this, square_size);
});
$('button').click(function() {
for (var i = 5; i < 15; i++) {
var randomN = Math.floor(Math.random()*15) + 1;
console.log(randomN);
var imageBox = $('#board').children("div:nth-child(" + randomN + ")");
console.log(imageBox);
Move(imageBox, 175);
}
});
}
}
});
function Move(clicked_square, square_size) {
var movable = false;
// oldx is really just empty square x and empty square y
// swap the old with the new, just make it so new has x and y for top...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!