puzzle1

Benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

Number.prototype.times = function(fn) {
      for (var r = [], i = 0; i < this; i++)
        r.push(fn(i));
      return r;
    };
    var makeCounter = function() {
      var privateCounter = 0;
    
      function changeBy(val) {
        return privateCounter += val;
      }
      return {
        increment: function() {
          return privateCounter += 1;
          //changeBy(1);
        },
        value: function() {
          return privateCounter;
        },
        resetear: function() {
          return privateCounter = 0;
        }
      };
    };
    var Counter1 = makeCounter(),
      Counter2 = makeCounter(),
      Counter3 = makeCounter(),
      Counter4 = makeCounter();
    
    function Puzzle() {
      this.makeJqueryTable = function() {
        var numRows = parseInt($('#rows').val(), 10),
          numCols = parseInt($('#cols').val(), 10),
          numaleat = aleatorios();
    
        var row = function(r) {
          return $("<tr/>").attr({
            "id": "fila" + r
          }).append(numCols.times(function(c) {
            return $("<td/>").attr({
              "id": [r, c].join(""),
              "name": Counter1.increment(),
              "class": "clase" + numaleat[Counter2.increment() - 1]
            });
          }));
        };
    
        Counter1.resetear();
        Counter2.resetear();
    
        $("#tablajuego").append($("<table/>")
          .append(numRows.times(row))
          .attr({
            "border": 1,
            "id": Counter3.increment()
          }));
    
        return $("<table/>")
          .append(numRows.times(row))
          .attr("border", 1);
      };
    } // Fin de function Puzzle()
    
    function aleatorios() {
      var nums = [],
        numsLen = 9,
        maxNum = 9,
        num;
      while (nums.length < numsLen) {
        num = Math.round(Math.random() * maxNum);
        num === 0 ? num++ : num;
        if (nums.indexOf(num) === -1) {
          nums.push(num);
        }
      }
      return nums;
    }
    
    function Point(x, y) {
      this.x = x;
      this.y = y;
    }
    
    $("#gpuzzle").on("click", function() {
      var conjuntoPuzzles = {};
      conjuntoPuzzles.generar = function() {
        this["puzzle" + Counter4.increment()] = new Puzzle();
        this["puzzle" + Counter4.value()].makeJqueryTable();
      };
      conjuntoPuzzles.generar();
    });
    
    $("#tablajuego").on("click", "td", function(e) {
      var idtablactual = e.target.parentNode.parentNode.parentNode.id,
        idactual = e.target.id,
        idtabla = $("#" + idtablactual).attr("id"),
        idblanco = $("#" + idtabla).find(".clase9").attr("id"),
        blancof = parseInt(idblanco.split("")[0], 10),
        blancoc = parseInt(idblanco.split("")[1], 10);
    
      var direcciones = {
        norte: new Point(blancof - 1, blancoc),
        sur: new Point(blancof + 1, blancoc),
        este: new Point(blancof, blancoc + 1),
        oeste: new Point(blancof, blancoc - 1)
      };
    
      if ((idactual === direcciones.norte.x.toString() + direcciones.norte.y.toString()) || (idactual === direcciones.este.x.toString() + direcciones.este.y.toString()) || (idactual === direcciones.sur.x.toString() + direcciones.sur.y.toString()) || (idactual === direcciones.oeste.x.toString() + direcciones.oeste.y.toString())) {
        //alert("Movimiento OK");
        intercambiarFondo($("#" + idtablactual), e);
        return true;
      } else {
        return false;
        //alert("Donde vas calamar !!!");
      }
    });
    
    function intercambiarFondo(tablaact, ev) {
      tablaact.find(".clase9").attr("class", ev.target.className);
      ev.target.className = "clase9";
    
      findejuego(tablaact);
    }
    
    function findejuego(tact) {
      var tedes = tact.find("td");
      var tedeslongitud = tedes.length,
        tedesA = [];
    
      for (var i = 0; i < tedeslongitud; i++) {
        tedesA[i] = tedes[i];
      }
    
      var final = tedesA.every(function(item) {
        return parseInt(item.getAttribute("name")) === parseInt(item.className.substr(5, 1));
    
      });
    
      if (final === true) {
        alert("Enhorabuena, has terminado el puzzle !!!");
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
asdxcwe
Number.prototype.times = function(fn) {
  for (var r = [], i = 0; i < this; i++)
    r.push(fn(i));
  return r;
};
var makeCounter = function() {
  var privateCounter = 0;

  function changeBy(val) {
    return privateCounter += val;
  }
  return {
    increment: function() {
      return privateCounter += 1;
      //changeBy(1);
    },
    value: function() {
      return privateCounter;
    },
    resetear: function() {
      return privateCounter = 0;
    }
  };
};
var Counter1 = makeCounter(),
  Counter2 = makeCounter(),
  Counter3 = makeCounter(),
  Counter4 = makeCounter();

function Puzzle() {
  this.makeJqueryTable = function() {
    var numRows = parseInt($('#rows').val(), 10),
      numCols = parseInt($('#cols').val(), 10),
      numaleat = aleatorios();

    var row = function(r) {
      return $("<tr/>").attr({
        "id": "fila" + r
      }).append(numCols.times(function(c) {
        return $("<td/>").attr({
          "id": [r, c].join(""),
          "name": Counter1.increment(),
          "class": "clase" + numaleat[Counter2.increment() - 1]
        });
      }));
    };

    Counter1.resetear();
    Counter2.resetear();

    $("#tablajuego").append($("<table/>")
      .append(numRows.times(row))
      .attr({
        "border": 1,
        "id": Counter3.increment()
      }));

    return $("<table/>")
      .append(numRows.times(row))
      .attr("border", 1);
  };
} // Fin de function Puzzle()

function aleatorios() {
  var nums = [],
    numsLen = 9,
    maxNum = 9,
    num;
  while (nums.length < numsLen) {
    num = Math.round(Math.random() * maxNum);
    num === 0 ? num++ : num;
    if (nums.indexOf(num) === -1) {
      nums.push(num);
    }
  }
  return nums;
}

function Point(x, y) {
  this.x = x;
  this.y = y;
}

$("#gpuzzle").on("click", function() {
  var conjuntoPuzzles = {};
  conjuntoPuzzles.generar = function() {
    this["puzzle" + Counter4.increment()] = new Puzzle();
    this["puzzle" + Counter4.value()].makeJqueryTable();
  };
  conjuntoPuzzles.generar();
});

$("#tablajuego").on("click", "td", function(e) {
  var idtablactual = e.target.parentNode.parentNode.parentNode.id,
    idactual = e.target.id,
    idtabla = $("#" + idtablactual).attr("id"),
    idblanco = $("#" + idtabla).find(".clase9").attr("id"),
    blancof = parseInt(idblanco.split("")[0], 10),
    blancoc = parseInt(idblanco.split("")[1], 10);

  var direcciones = {
    norte: new Point(blancof - 1, blancoc),
    sur: new Point(blancof + 1, blancoc),
    este: new Point(blancof, blancoc + 1),
    oeste: new Point(blancof, blancoc - 1)
  };

  if ((idactual === direcciones.norte.x.toString() + direcciones.norte.y.toString()) || (idactual === direcciones.este.x.toString() + direcciones.este.y.toString()) || (idactual === direcciones.sur.x.toString() + direcciones.sur.y.toString()) || (idactual === direcciones.oeste.x.toString() + direcciones.oeste.y.toString())) {
    //alert("Movimiento OK");
    intercambiarFondo($("#" + idtablactual), e);
    return true;
  } else {
    return false;
    //alert("Donde vas calamar !!!");
  }
});

function intercambiarFondo(tablaact, ev) {
  tablaact.find(".clase9").attr("class", ev.target.className);
  ev.target.className = "clase9";

  findejuego(tablaact);
}

function findejuego(tact) {
  var tedes = tact.find("td");
  var tedeslongitud = tedes.length,
    tedesA = [];

  for (var i = 0; i < tedeslongitud; i++) {
    tedesA[i] = tedes[i];
  }

  var final = tedesA.every(function(item) {
    return parseInt(item.getAttribute("name")) === parseInt(item.className.substr(5, 1));

  });

  if (final === true) {
    alert("Enhorabuena, has terminado el puzzle !!!");
  }
}
ready
jkiuoim,
Number.prototype.times = function(fn) {
  for (var r = [], i = 0; i < this; i++)
    r.push(fn(i));
  return r;
};
var makeCounter = function() {
  var privateCounter = 0;

  function changeBy(val) {
    return privateCounter += val;
  }
  return {
    increment: function() {
      return privateCounter += 1;
      //changeBy(1);
    },
    value: function() {
      return privateCounter;
    },
    resetear: function() {
      return privateCounter = 0;
    }
  };
};
var Counter1 = makeCounter(),
  Counter2 = makeCounter(),
  Counter3 = makeCounter(),
  Counter4 = makeCounter();

function Puzzle() {
  this.makeJqueryTable = function() {
    var numRows = parseInt($('#rows').val(), 10),
      numCols = parseInt($('#cols').val(), 10),
      numaleat = aleatorios();

    var row = function(r) {
      return $("<tr/>").attr({
        "id": "fila" + r
      }).append(numCols.times(function(c) {
        return $("<td/>").attr({
          "id": [r, c].join(""),
          "name": Counter1.increment(),
          "class": "clase" + numaleat[Counter2.increment() - 1]
        });
      }));
    };

    Counter1.resetear();
    Counter2.resetear();

    $("#tablajuego").append($("<table/>")
      .append(numRows.times(row))
      .attr({
        "border": 1,
        "id": Counter3.increment()
      }));

    return $("<table/>")
      .append(numRows.times(row))
      .attr("border", 1);
  };
} // Fin de function Puzzle()

function aleatorios() {
  var nums = [],
    numsLen = 9,
    maxNum = 9,
    num;
  while (nums.length < numsLen) {
    num = Math.round(Math.random() * maxNum);
    num === 0 ? num++ : num;
    if (nums.indexOf(num) === -1) {
      nums.push(num);
    }
  }
  return nums;
}

function Point(x, y) {
  this.x = x;
  this.y = y;
}

$("#gpuzzle").on("click", function() {
  var conjuntoPuzzles = {};
  conjuntoPuzzles.generar = function() {
    this["puzzle" + Counter4.increment()] = new Puzzle();
    this["puzzle" + Counter4.value()].makeJqueryTable();
  };
  conjuntoPuzzles.generar();
});

$("#tablajuego").on("click", "td", function(e) {
  var idtablactual = e.target.parentNode.parentNode.parentNode.id,
    idactual = e.target.id,
    idtabla = $("#" + idtablactual).attr("id"),
    idblanco = $("#" + idtabla).find(".clase9").attr("id"),
    blancof = parseInt(idblanco.split("")[0], 10),
    blancoc = parseInt(idblanco.split("")[1], 10);

  var direcciones = {
    norte: new Point(blancof - 1, blancoc),
    sur: new Point(blancof + 1, blancoc),
    este: new Point(blancof, blancoc + 1),
    oeste: new Point(blancof, blancoc - 1)
  };

  if ((idactual === direcciones.norte.x.toString() + direcciones.norte.y.toString()) || (idactual === direcciones.este.x.toString() + direcciones.este.y.toString()) || (idactual === direcciones.sur.x.toString() + direcciones.sur.y.toString()) || (idactual === direcciones.oeste.x.toString() + direcciones.oeste.y.toString())) {
    //alert("Movimiento OK");
    intercambiarFondo($("#" + idtablactual), e);
    return true;
  } else {
    return false;
    //alert("Donde vas calamar !!!");
  }
});

function intercambiarFondo(tablaact, ev) {
  tablaact.find(".clase9").attr("class", ev.target.className);
  ev.target.className = "clase9";

  findejuego(tablaact);
}

function findejuego(tact) {
  var tedes = tact.find("td");
  var tedeslongitud = tedes.length,
    tedesA = [];

  for (var i = 0; i < tedeslongitud; i++) {
    tedesA[i] = tedes[i];
  }

  var final = tedesA.every(function(item) {
    return parseInt(item.getAttribute("name")) === parseInt(item.className.substr(5, 1));

  });

  if (final === true) {
    alert("Enhorabuena, has terminado el puzzle !!!");
  }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.