this.move = function(a) { if (a == this.empty) return; var ay = Math.floor(a / dim); var ax = a % dim; var by = Math.floor(this.empty / dim); var bx = this.empty % dim; if ((ax == bx && Math.abs(ay - by) == 1) || (ay == by && Math.abs(ax - bx) == 1)) { this.swap(a, this.empty); } }