title: Tiling and Packing a board using Hexominoes with javascript
body: <p>I'm tiling and packing 15 x 15 board with hexomino figures. To fill a 15 x 15 rectangle with a hexomino figure, I have to use all of the 35 hexominoes, one duplicate figure, and a 3x3 square.
  So, I created 3x3 square and made one duplicate figure randomly. 
  When I tested on a small board like 9 X 15, I can quickly find the answer as you can see.</p>
  
  <p>Every hexomino has its own key 0 to 36 and 'mark' variable to check if it's used.</p>
  
  <p><a href="https://i.stack.imgur.com/Oorca.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oorca.png" alt="enter image description here"></a></p>
  
  <p>However, the problem is that it takes too long to find the 15 x 15 solution. To test this, I slept with the script turned on, but I couldn't get the result.
  I double-checked all the hexomino coordinates, so I think this algorithm is just not good.</p>
  
  <p>Therefore, I used Python with another method, and I could find the answer 
  <a href="https://i.stack.imgur.com/Ctkt2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ctkt2.png" alt="enter image description here"></a></p>
  
  <p>This is my js code having a problem.</p>
  
  <pre><code>  colors = [
      "#EEAAAA", "#DDBB99", "#CCCC88", "#BBDD99", "#AAEEAA", "#99DDBB", "#B22222",
      "#88CCCC", "#99BBDD", "#AAAAEE", "#BB99DD", "#CC88CC", "#DD99BB", "#663399",
      "#C0C0C0" ,"#4682B4", "#D2B48C", "#2E8B57", "#FF0000", "#B0E0E6", "#DB7093",
      "#FFDAB9",   "green", "#EEE8AA",    "gray", "#191970", "#00FA9A", "#48D1CC",
      "#C71585", "#800000", "#FFA07A",  "orange",  "yellow", "#2F4F4F", "#FF8C00",
      "#556B2F", "white"
  
    ]; // Colors to distinguish hexomino
  
  function setup() { 
    createCanvas(2500, 2500);
  
    puzzle = new Puzzle();
    puzzle.run(9,15);
  
  } 
  
  function draw() { 
    paint(puzzle);
  }
  
  function Square(x, y) {
    this.x = x;
    this.y = y;
    this.move = function(row, column) {
      return new Square(this.x + column, this.y + row);
    };
    this.check = function(width, height) {
      if (this.x &lt; 0 || this.y &lt; 0) {
        return false;
      } else {
        if (this.x &gt;= width || this.y &gt;= height) {
          return false;
        } else {
          return true;
        }
      }
    };
  }
  
  function Hexomino(key, squares) {
    this.key = key;
    this.squares = squares.map(function(square) {
      return new Square(square[0], square[1]);
    });
    this.move = function(row, column) {
      var hexo = new Hexomino(this.key, []);
      hexo.squares = this.squares.map(function(square) {
        return square.move(row, column);
      });
      return hexo;
    };
  }
  
  //Hexominoes Coordinates
  function Hexominos() {
    this.shapes = [
      [ // #0 = 1 figure , 9 cells
        new Hexomino( 0, [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]),
      ],
  
      [ // #1 = 2
        new Hexomino( 1, [[0,0], [0, 1], [0, 2], [0, 3], [0,4], [0,5]]),
        new Hexomino( 1, [[0,0], [1, 0], [2, 0], [3, 0], [4,0], [5,0]]),
      ],
  
      [ // #2 = 8
        new Hexomino( 2, [[0,1], [1,1], [2,1], [3, 1], [4,1], [4,0]]),
        new Hexomino( 2, [[0,0], [1,0], [1,1], [1, 2], [1,3], [1,4]]),
        new Hexomino( 2, [[0,1], [0,0], [1,0], [2, 0], [3,0], [4,0]]),
        new Hexomino( 2, [[0,0], [0,1], [0,2], [0, 3], [0,4], [1,4]]),
  
        new Hexomino( 2, [[0,0], [0,1], [1,1], [2,1], [3,1], [4,1]]),
        new Hexomino( 2, [[0,4], [1,4], [1,3], [1,2], [1,1], [1,0]]),
        new Hexomino( 2, [[0,0], [1,0], [2,0], [3,0], [4,0], [4,1]]),
        new Hexomino( 2, [[0,0], [0,1], [0,2], [0,3], [0,4], [1,0]]),
      ],
  
      [ // #3 = 8
        new Hexomino( 3, [[0,1], [1,1], [2,1], [3,1],[3,0],[4,1]]),
        new Hexomino( 3, [[0,0], [0,1], [0,2], [0,3],[1,3],[0,4]]),
        new Hexomino( 3, [[0,1], [1,1], [1,0], [1,2],[1,3],[1,4]]),
        new Hexomino( 3, [[0,0], [1,0], [1,1], [2,0],[3,0],[4,0]]),
  
        new Hexomino( 3, [[0,1], [1,1], [1,0], [2,1],[3,1],[4,1]]),
        new Hexomino( 3, [[0,3], [1,3], [1,4], [1,2],[1,1],[1,0]]),
        new Hexomino( 3, [[0,0], [1,0], [2,0], [3,0],[3,1],[4,0]]),
        new Hexomino( 3, [[0,0], [0,1], [1,1], [0,2],[0,3],[0,4]]),
  
      ],
  
      [ // #4 = 4
        new Hexomino( 4, [[0,1], [1, 1], [2, 1], [2,0], [3,1], [4,1]]),
        new Hexomino( 4, [[0,0], [1, 0], [2, 0], [2,1], [3,0], [4,0]]),
        new Hexomino( 4, [[0,2], [1, 2], [1, 3], [1,4], [1,1], [1,0]]),
        new Hexomino( 4, [[0,0], [0, 1], [0, 2], [1,2], [0,3], [0,4]]),
      ],
  
      [ // #5 = 8
        new Hexomino( 5, [[0,1], [1,1],[2,1],[3,1],[3,0],[4,0]]),
        new Hexomino( 5, [[0,1], [1,1],[1,0],[2,0],[3,0],[4,0]]),
        new Hexomino( 5, [[0,0], [0,1],[0,2],[0,3],[1,3],[1,4]]),
        new Hexomino( 5, [[0,0], [0,1],[1,1],[1,2],[1,3],[1,4]]),
  
        new Hexomino( 5, [[0,0], [1,0],[1,1],[2,1],[3,1],[4,1]]),
        new Hexomino( 5, [[0,0], [1,0],[2,0],[3,0],[3,1],[4,1]]),
        new Hexomino( 5, [[0,4], [0,3],[1,3],[1,2],[1,1],[1,0]]),
        new Hexomino( 5, [[0,4], [0,3],[0,2],[0,1],[1,1],[1,0]]),
      ],
  
      [ // #6 = 8
        new Hexomino( 6, [[0,1],[1,1],[2,1],[3,1],[2,0],[3,0]]),
        new Hexomino( 6, [[0,0],[0,1],[1,0],[1,1],[1,2],[1,3]]),
        new Hexomino( 6, [[0,0],[0,1],[1,0],[1,1],[2,0],[3,0]]),
        new Hexomino( 6, [[0,0],[0,1],[0,2],[0,3],[1,2],[1,3]]),
  
        new Hexomino( 6, [[0,0],[0,1],[1,0],[1,1],[2,1],[3,1]]),
        new Hexomino( 6, [[0,2],[0,3],[1,3],[1,2],[1,1],[1,0]]),
        new Hexomino( 6, [[0,0],[1,0],[2,0],[3,0],[2,1],[3,1]]),
        new Hexomino( 6, [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1]]),
      ],
  
      [ // #7 = 8
        new Hexomino( 7, [[0,1],[1,1],[1,0],[2,1],[3,1],[3,0]]),
        new Hexomino( 7, [[0,0],[0,2],[1,0],[1,1],[1,2],[1,3]]),
        new Hexomino( 7, [[0,0],[0,1],[1,0],[2,0],[2,1],[3,0]]),
        new Hexomino( 7, [[0,0],[0,1],[0,2],[0,3],[1,1],[1,3]]),
  
        new Hexomino( 7, [[0,0],[2,0],[0,1],[1,1],[2,1],[3,1]]),
        new Hexomino( 7, [[0,1],[0,3],[1,0],[1,1],[1,2],[1,3]]),
        new Hexomino( 7, [[0,0],[1,0],[2,0],[3,0],[1,1],[3,1]]),
        new Hexomino( 7, [[0,0],[0,1],[0,2],[0,3],[1,0],[1,2]]),
      ],
      // ----------------------------------------------------------------------------------------
      [ // #8 = 4
        new Hexomino( 8, [[0,0],[3,0],[0,1],[1,1],[2,1],[3,1]]),
        new Hexomino( 8, [[0,0],[0,3],[1,0],[1,1],[1,2],[1,3]]),
        new Hexomino( 8, [[0,0],[0,1],[1,0],[2,0],[3,0],[3,1]]),
        new Hexomino( 8, [[0,0],[0,1],[0,2],[0,3],[1,0],[1,3]]),
      ],
  
      [ // #9 = 4
        new Hexomino( 9, [[1,0],[2,0],[0,1],[1,1],[2,1],[3,1]]),
        new Hexomino( 9, [[0,1],[0,2],[1,0],[1,1],[1,2],[1,3]]),
        new Hexomino( 9, [[0,0],[1,0],[2,0],[3,0],[1,1],[2,1]]),
        new Hexomino( 9, [[0,0],[0,1],[0,2],[0,3],[1,1],[1,2]]),
      ],
  
      [ // #10 = 8
        new Hexomino( 10, [[0,2],[1,2],[2,2],[3,2],[3,1],[3,0]]),
        new Hexomino( 10, [[0,0],[1,0],[2,0],[2,1],[2,2],[2,3]]),
        new Hexomino( 10, [[0,0],[0,1],[0,2],[1,0],[2,0],[3,0]]),
        new Hexomino( 10, [[0,0],[0,1],[0,2],[0,3],[1,3],[2,3]]),
  
        new Hexomino( 10, [[0,0],[0,1],[0,2],[1,2],[2,2],[3,2]]),  
        new Hexomino( 10, [[0,3],[1,3],[2,3],[2,2],[2,1],[2,0]]),
        new Hexomino( 10, [[0,0],[1,0],[2,0],[3,0],[3,1],[3,2]]),
        new Hexomino( 10, [[0,0],[0,1],[0,2],[0,3],[1,0],[2,0]]),
      ],
  
      [ // #11 = 8
        new Hexomino( 11, [[0,2],[1,2],[2,2],[3,2],[2,1],[2,0]]),
        new Hexomino( 11, [[0,1],[1,1],[2,0],[2,1],[2,2],[2,3]]),
        new Hexomino( 11, [[0,0],[1,0],[1,1],[1,2],[2,0],[3,0]]),
        new Hexomino( 11, [[0,0],[0,1],[0,2],[0,3],[1,2],[2,2]]),
  
        new Hexomino( 11, [[0,2],[1,2],[1,1],[1,0],[2,2],[3,2]]),
        new Hexomino( 11, [[0,2],[1,2],[2,0],[2,1],[2,2],[2,3]]),
        new Hexomino( 11, [[0,0],[1,0],[2,0],[3,0],[2,1],[2,2]]),
        new Hexomino( 11, [[0,0],[0,1],[0,2],[0,3],[1,1],[2,1]]),
      ],
  
      [ // #12 = 4
        new Hexomino( 12, [[0,1],[1,1],[2,1],[3,1],[3,2],[3,0]]),
        new Hexomino( 12, [[0,0],[1,0],[1,1],[1,2],[1,3],[2,0]]),
        new Hexomino( 12, [[0,0],[0,1],[0,2],[1,1],[2,1],[3,1]]),
        new Hexomino( 12, [[1,0],[1,1],[1,2],[1,3],[0,3],[2,3]]),
      ],
  
      [ // #13 = 8
        new Hexomino( 13, [[0,1],[1,1],[2,1],[2,2],[3,1],[3,0]]),
        new Hexomino( 13, [[0,0],[1,0],[1,1],[1,2],[1,3],[2,1]]),
        new Hexomino( 13, [[0,1],[0,2],[1,1],[1,0],[2,1],[3,1]]),
        new Hexomino( 13, [[1,0],[1,1],[1,2],[0,2],[1,3],[2,3]]),
  
        new Hexomino( 13, [[0,0],[0,1],[1,1],[1,2],[2,1],[3,1]]),
        new Hexomino( 13, [[0,3],[1,3],[1,2],[2,2],[1,1],[1,0]]),
        new Hexomino( 13, [[0,1],[1,1],[2,1],[2,0],[3,1],[3,2]]),
        new Hexomino( 13, [[0,1],[1,1],[1,0],[2,0],[1,2],[1,3]]),
      ],
  
      [ // #14 = 8
        new Hexomino( 14, [[0,1],[1,1],[1,2],[2,1],[3,1],[3,0]]),
        new Hexomino( 14, [[0,0],[1,0],[1,1],[1,2],[2,2],[1,3]]),
        new Hexomino( 14, [[0,1],[0,2],[1,1],[2,1],[2,0],[3,1]]),
        new Hexomino( 14, [[0,1],[1,0],[1,1],[1,2],[1,3],[2,3]]),
  
        new Hexomino( 14, [[0,0],[0,1],[1,1],[2,1],[3,1],[2,2]]),
        new Hexomino( 14, [[0,3],[1,3],[1,2],[1,1],[2,1],[1,0]]),
        new Hexomino( 14, [[0,1],[1,0],[1,1],[2,1],[3,1],[3,2]]),
        new Hexomino( 14, [[0,2],[1,2],[1,3],[1,1],[1,0],[2,0]]),
      ],
  
      // --------------------------------------------------------------------------------------------
      [ // #15 = 4
        new Hexomino( 15, [[0,1],[0,2],[1,1],[2,1],[3,1],[3,0]]),
        new Hexomino( 15, [[0,0],[1,0],[1,1],[1,2],[1,3],[2,3]]),
  
        new Hexomino( 15, [[0,0],[0,1],[1,1],[2,1],[3,1],[3,2]]),
        new Hexomino( 15, [[0,3],[1,3],[1,2],[1,1],[1,0],[2,0]]),
      ],
  
      [ // #16 = 4
        new Hexomino( 16, [[0,1],[1,1],[1,2],[2,1],[2,0],[3,1]]),
        new Hexomino( 16, [[0,1],[1,1],[1,0],[1,2],[1,3],[2,2]]),
  
        new Hexomino( 16, [[0,1],[1,1],[1,0],[2,1],[2,2],[3,1]]),
        new Hexomino( 16, [[0,2],[1,2],[1,3],[1,1],[2,1],[1,0]]),
      ],
  
      [ // #17 = 4
        new Hexomino( 17, [[0,1],[1,1],[2,1],[2,0],[2,2],[3,1]]),
        new Hexomino( 17, [[0,1],[1,1],[1,0],[1,2],[1,3],[2,1]]),
        new Hexomino( 17, [[0,1],[1,1],[1,0],[1,3],[2,1],[3,1]]),
        new Hexomino( 17, [[0,2],[1,0],[1,1],[1,2],[2,2],[1,3]]),
      ],
  
      [ // #18 = 8
        new Hexomino( 18, [[0,2],[1,2],[2,2],[2,1],[2,0],[3,1]]),
        new Hexomino( 18, [[0,1],[1,1],[1,0],[2,1],[2,2],[2,3]]),
        new Hexomino( 18, [[0,1],[1,1],[1,2],[1,0],[2,0],[3,0]]),
        new Hexomino( 18, [[0,0],[0,1],[0,2],[1,2],[1,3],[2,2]]),
  
        new Hexomino( 18, [[0,1],[1,0],[1,1],[1,2],[2,2],[3,2]]),
        new Hexomino( 18, [[0,2],[1,2],[1,3],[2,2],[2,1],[2,0]]),
        new Hexomino( 18, [[0,0],[1,0],[2,0],[2,1],[3,1],[2,2]]),
        new Hexomino( 18, [[1,0],[0,1],[0,2],[0,3],[1,1],[2,1]]),
      ],
  
      [ // #19 = 8
        new Hexomino( 19, [[0,0],[2,0],[3,0],[0,1],[1,1],[2,1]]),
        new Hexomino( 19, [[0,0],[0,1],[1,1],[1,2],[1,3],[0,3]]),
        new Hexomino( 19, [[0,1],[1,1],[1,0],[2,0],[3,0],[3,1]]),
        new Hexomino( 19, [[0,0],[1,0],[0,1],[0,2],[1,2],[1,3]]),
  
        new Hexomino (19, [[0,0],[1,0],[1,1],[2,1],[3,1],[3,0]]),
        new Hexomino (19, [[0,0],[1,0],[1,1],[1,2],[0,2],[0,3]]),
        new Hexomino (19, [[0,0],[0,1],[1,0],[2,0],[2,1],[3,1]]),
        new Hexomino (19, [[0,1],[0,2],[0,3],[1,3],[1,1],[1,0]]),
      ],
  
      [ // #20 = 4
        new Hexomino( 20, [[0,1],[1,1],[2,1],[2,0],[3,0],[4,0]]),
        new Hexomino( 20, [[0,0],[0,1],[0,2],[1,2],[1,3],[1,4]]),
  
        new Hexomino( 20, [[0,0],[1,0],[2,0],[2,1],[3,1],[4,1]]),
        new Hexomino( 20, [[1,0],[1,1],[1,2],[0,2],[0,3],[0,4]]),
      ],
  
      [ // #21 = 4 figures, set to 12 cells (duplicate)
        // new Hexomino( 21, [[0,1],[1,1],[2,1],[1,0],[2,0],[3,0]]),
        // new Hexomino( 21, [[0,0],[0,1],[0,2],[1,1],[1,2],[1,3]]),
  
        // new Hexomino( 21, [[0,0],[1,0],[2,0],[1,1],[2,1],[3,1]]),
        // new Hexomino( 21, [[0,1],[0,2],[0,3],[1,0],[1,1],[1,2]]),
  
        new Hexomino( 21, [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0]]),
        new Hexomino( 21, [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1]]),
  
        new Hexomino( 21, [[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1]]),
        new Hexomino( 21, [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,5],[1,4],[1,3],[1,2],[1,1],[1,0]]),
      ],
  
      // --------------------------------------------------------------------------------------------------
      [ // #22 = 2
        new Hexomino( 22, [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1]]),
        new Hexomino( 22, [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]),
      ],
  
      [ // #23 = 8
        new Hexomino( 23, [[0,1],[1,1],[2,1],[2,2],[2,0],[3,0]]),
        new Hexomino( 23, [[0,0],[0,1],[1,1],[2,1],[1,2],[1,3]]),
        new Hexomino( 23, [[0,2],[1,2],[1,1],[1,0],[2,1],[3,1]]),
        new Hexomino( 23, [[0,2],[1,0],[1,1],[1,2],[2,2],[2,3]]),
  
        new Hexomino( 23, [[0,0],[1,0],[1,1],[1,2],[2,1],[3,1]]),
        new Hexomino( 23, [[0,2],[0,3],[1,2],[2,2],[1,1],[1,0]]),
        new Hexomino( 23, [[0,1],[1,1],[2,1],[2,0],[2,2],[3,2]]),
        new Hexomino( 23, [[0,1],[1,1],[2,1],[2,0],[1,2],[1,3]]),
      ],
  
      [ // #24 = 8
        new Hexomino( 24, [[0,1],[1,1],[2,1],[2,2],[1,0],[2,0]]),
        new Hexomino( 24, [[0,0],[0,1],[1,0],[2,0],[1,1],[1,2]]),
        new Hexomino( 24, [[0,0],[0,1],[0,2],[1,2],[1,1],[2,1]]),
        new Hexomino( 24, [[0,2],[1,0],[1,1],[1,2],[2,1],[2,2]]),
  
        new Hexomino( 24, [[0,0],[1,0],[0,1],[1,1],[2,1],[0,2]]),
        new Hexomino( 24, [[0,1],[0,2],[1,2],[1,1],[1,0],[2,2]]),
        new Hexomino( 24, [[0,1],[1,1],[1,2],[2,2],[2,1],[2,0]]),
        new Hexomino( 24, [[0,0],[1,0],[2,0],[1,1],[1,2],[2,1]]),
      ],
  
      [ // #25 = 8
        new Hexomino( 25, [[0,1],[1,1],[1,2],[2,1],[2,0],[3,0]]),
        new Hexomino( 25, [[0,0],[0,1],[1,1],[1,2],[2,2],[1,3]]),
        new Hexomino( 25, [[0,2],[1,2],[1,1],[2,1],[2,0],[3,1]]),
        new Hexomino( 25, [[0,1],[1,0],[1,1],[1,2],[2,2],[2,3]]),
  
        new Hexomino( 25, [[0,0],[1,0],[1,1],[2,1],[3,1],[2,2]]),
        new Hexomino( 25, [[0,3],[0,2],[1,2],[1,1],[2,1],[1,0]]),
        new Hexomino( 25, [[0,1],[1,1],[1,0],[2,1],[2,2],[3,2]]),
        new Hexomino( 25, [[0,2],[1,2],[1,3],[1,1],[2,1],[2,0]]),
      ],
  
      [ // #26 = 8
        new Hexomino( 26, [[0,2],[1,2],[2,2],[2,1],[2,0],[3,0]]),
        new Hexomino( 26, [[0,0],[0,1],[1,1],[2,1],[2,2],[2,3]]),
        new Hexomino( 26, [[0,2],[1,2],[1,1],[1,0],[2,0],[3,0]]),
        new Hexomino( 26, [[0,0],[0,1],[0,2],[1,2],[2,2],[2,3]]),
  
        new Hexomino( 26, [[0,0],[1,0],[1,1],[1,2],[2,2],[3,2]]),
        new Hexomino( 26, [[0,3],[0,2],[1,2],[2,2],[2,1],[2,0]]),
        new Hexomino( 26, [[0,0],[1,0],[2,0],[2,1],[2,2],[3,2]]),
        new Hexomino( 26, [[0,3],[0,2],[0,1],[1,1],[2,1],[2,0]]),
      ],
  
      [ // #27 = 8
        new Hexomino( 27, [[0,2],[1,2],[2,2],[2,1],[3,1],[3,0]]),
        new Hexomino( 27, [[0,0],[1,0],[1,1],[2,1],[2,2],[2,3]]),
        new Hexomino( 27, [[0,2],[0,1],[1,1],[1,0],[2,0],[3,0]]),
        new Hexomino( 27, [[0,0],[0,1],[0,2],[1,2],[1,3],[2,3]]),
  
        new Hexomino( 27, [[0,0],[0,1],[1,1],[1,2],[2,2],[3,2]]),
        new Hexomino( 27, [[0,3],[1,3],[1,2],[2,2],[2,1],[2,0]]),
        new Hexomino( 27, [[0,0],[1,0],[2,0],[2,1],[3,1],[3,2]]),
        new Hexomino( 27, [[0,3],[0,2],[0,1],[1,1],[1,0],[2,0]]),
      ],
  
      [ // #28 = 8
        new Hexomino( 28, [[0,2],[1,2],[2,2],[2,1],[2,0],[1,0]]),
        new Hexomino( 28, [[0,1],[0,0],[1,0],[2,0],[2,1],[2,2]]),
        new Hexomino( 28, [[0,0],[0,1],[0,2],[1,2],[1,0],[2,0]]),
        new Hexomino( 28, [[0,0],[0,1],[0,2],[1,2],[2,2],[2,1]]),
  
        new Hexomino( 28, [[0,0],[0,1],[0,2],[1,2],[2,2],[1,0]]),
        new Hexomino( 28, [[0,1],[0,2],[1,2],[2,2],[2,1],[2,0]]),
        new Hexomino( 28, [[0,0],[1,0],[2,0],[2,1],[2,2],[1,2]]),
        new Hexomino( 28, [[0,2],[0,1],[0,0],[1,0],[2,0],[2,1]]),
      ],
  
      [ // #29 = 8
        new Hexomino( 29, [[0,2],[1,2],[2,2],[1,1],[1,0],[2,0]]),
        new Hexomino( 29, [[0,0],[0,1],[1,1],[2,1],[2,2],[2,0]]),
        new Hexomino( 29, [[0,0],[1,0],[2,0],[1,1],[1,2],[0,2]]),
        new Hexomino( 29, [[0,0],[0,1],[0,2],[1,1],[2,1],[2,2]]),
  
        new Hexomino( 29, [[0,0],[1,0],[1,1],[1,2],[0,2],[2,2]]),
        new Hexomino( 29, [[0,2],[0,1],[1,1],[2,1],[2,2],[2,0]]),
        new Hexomino( 29, [[0,0],[1,0],[2,0],[1,1],[1,2],[2,2]]),
        new Hexomino( 29, [[0,0],[0,1],[0,2],[1,1],[2,1],[2,0]]),
      ],
  
      [ // #30 = 4
        new Hexomino( 30, [[0,1],[1,1],[1,0],[2,0],[1,2],[2,2]]),
        new Hexomino( 30, [[0,0],[0,1],[1,1],[1,2],[2,1],[2,0]]),
        new Hexomino( 30, [[0,0],[1,0],[1,1],[2,1],[1,2],[0,2]]),
        new Hexomino( 30, [[0,2],[0,1],[1,1],[1,0],[2,1],[2,2]]),
      ],
  
      [ // #31 = 8
        new Hexomino( 31, [[0,2],[1,2],[1,1],[2,1],[3,1],[3,0]]),
        new Hexomino( 31, [[0,0],[1,0],[1,1],[1,2],[2,2],[2,3]]),
        new Hexomino( 31, [[0,2],[0,1],[1,1],[2,1],[2,0],[3,0]]),
        new Hexomino( 31, [[0,0],[0,1],[1,1],[1,2],[1,3],[2,3]]),
  
        new Hexomino( 31, [[0,0],[0,1],[1,1],[2,1],[2,2],[3,2]]),
        new Hexomino( 31, [[0,3],[1,3],[1,2],[1,1],[2,1],[2,0]]),
        new Hexomino( 31, [[0,0],[1,0],[1,1],[2,1],[3,1],[3,2]]),
        new Hexomino( 31, [[0,3],[0,2],[1,2],[1,1],[1,0],[2,0]]),
  
      ],
  
      [ // #32 = 4
        new Hexomino( 32, [[0,0],[0,1],[1,1],[0,2],[1,2],[2,2]]),
        new Hexomino( 32, [[0,2],[1,2],[2,2],[1,1],[2,1],[2,0]]),
        new Hexomino( 32, [[0,0],[1,0],[2,0],[0,1],[1,1],[0,2]]),
        new Hexomino( 32, [[0,0],[1,0],[2,0],[1,1],[2,1],[2,2]]),
      ],
  
      [ // #33 = 4
        new Hexomino( 33, [[0,2],[0,1],[1,2],[1,1],[1,0],[2,1]]),
        new Hexomino( 33, [[0,1],[1,1],[1,2],[1,0],[2,1],[2,2]]),
        new Hexomino( 33, [[0,1],[1,1],[2,1],[1,0],[2,0],[1,2]]),
        new Hexomino( 33, [[0,0],[0,1],[1,0],[1,1],[2,1],[1,2]]),
      ],
  
      [ // #34 = 8
        new Hexomino( 34, [[0,1],[0,2],[1,2],[1,1],[1,0],[2,0]]),
        new Hexomino( 34, [[0,0],[0,1],[1,1],[2,1],[1,2],[2,2]]),
        new Hexomino( 34, [[0,2],[1,2],[1,1],[1,0],[2,1],[2,0]]),
        new Hexomino( 34, [[0,0],[0,1],[1,0],[1,1],[2,1],[2,2]]),
  
        new Hexomino( 34, [[0,0],[1,0],[1,1],[1,2],[2,1],[2,2]]),
        new Hexomino( 34, [[0,2],[0,1],[1,1],[1,0],[2,0],[2,1]]),
        new Hexomino( 34, [[0,0],[0,1],[1,0],[1,1],[1,2],[2,2]]),
        new Hexomino( 34, [[0,2],[0,1],[1,1],[1,2],[2,1],[2,0]]),
      ],
  
      [ // #35 = 4
        new Hexomino( 35, [[0,2],[1,2],[1,1],[2,1],[2,0],[3,0]]),
        new Hexomino( 35, [[0,0],[0,1],[1,1],[1,2],[2,2],[2,3]]),
  
        new Hexomino( 35, [[0,0],[1,0],[1,1],[2,1],[2,2],[3,2]]),
        new Hexomino( 35, [[0,3],[0,2],[1,2],[1,1],[2,1],[2,0]]),
      ]
      // [ // #36 = any dulicate cell
      //   new Hexomino( 36, [[0,1],[1,1],[2,1],[1,0],[2,0],[3,0]]),
      //   new Hexomino( 36, [[0,0],[0,1],[0,2],[1,1],[1,2],[1,3]]),
  
      //   new Hexomino( 36, [[0,0],[1,0],[2,0],[1,1],[2,1],[3,1]]),
      //   new Hexomino( 36, [[0,1],[0,2],[0,3],[1,0],[1,1],[1,2]])
      // ],
    ];
    this.rectangle = [];
    this.enumerator = function(row, column, width, height) {
      return this.shapes.map(function(a) {
        return a.map(function(b) {
          return b.move(row, column);
        }).filter(function(c) {
          return c.squares.every(function(d) {
            return d.check(width, height);
          });
        });
      });
    };
    this.initialize = function(width, height) {
      var row, column;
      for (row = 0; row &lt; height; row++) {
        this.rectangle[row] = [];
        for (column = 0; column &lt; width; column++) {
          this.rectangle[row][column] = 
            this.enumerator(row, column, width, height);
        }
      }
    };
  }
  
  function State(rectangle, mask, count) {
    this.rectangle = rectangle;
    this.mask = mask;
    this.count = count;
    this.shapes = [];
    this.index = -1;
    this.select = function() {
      var row, column;
      for (row = 0; row &lt; this.rectangle.length; row++) {
        for (column = 0; column &lt; this.rectangle[row].length; column++) {
          if (this.rectangle[row][column] === false) {
            return new Square(column, row);
          }
        }
      }
      return null;
    };
    this.add = function(rectangle) {
      var square, shapes;
      square = this.select();
      if (square != null) {
        shapes = rectangle[square.y][square.x];
        this.mask.forEach(function(value, index) {
          if (value === false) {
            shapes[index].forEach(function(shape) {
              this.shapes.push(shape);
            }, this);
          }
        }, this);
      }
    };
    this.next = function() {
      this.index = this.index + 1;
      return this.index &lt; this.shapes.length;
    };
    this.fit = function() {
      var shape;
      shape = this.shapes[this.index];
      return shape.squares.every(function(square) {
        return this.rectangle[square.y][square.x] === false;
      }, this);
    };
    this.copy = function() {
      var rectangle, shape, mask, count;
      rectangle = this.rectangle.map(function(row) {
        return row.slice();
      });
      shape = this.shapes[this.index];
      shape.squares.forEach(function(square) {
        rectangle[square.y][square.x] = true;
      });
      mask = this.mask.slice();
      mask[shape.key] = true;
  
  // Until Count equals to 225 (15 x 15 = 225 cells)
      if(shape.key == 0){
        count = this.count + 9;
      }
      else if (shape.key == 21){
        count = this.count + 12;
      }
      else{
        count = this.count + 6;
      }
  
      return new State(rectangle, mask, count);
    };
  }
  
  function Puzzle() {
    this.stack = [];
    this.first = function(width, height) {
      var rectangle, row, column, mask;
      rectangle = [];
      for (row = 0; row &lt; height; row++) {
        rectangle[row] = [];
        for (column = 0; column &lt; width; column++) {
          rectangle[row][column] = false;
        }
      }
      mask = [
        false, false, false, false, false, false, false, false,
        false, false, false, false, false, false, false,
        false, false, false, false, false, false, false,
        false, false, false, false, false, false, false,
        false, false, false, false, false, false, false
      ];
      return new State(rectangle, mask, 0);
    };
    this.run = function(width, height) {
      var hexo, state, count;
      hexo = new Hexominos();
      hexo.initialize(width, height);
      state = this.first(width, height);
      state.add(hexo.rectangle);
      this.stack.push(state);
      count = width * height;
      while (this.stack.length &gt; 0) {
        state = this.stack.pop();
        if (state.count == count) {
          break;
        }
        while (state.next()) {
          if (state.fit()) {
            this.stack.push(state);
            state = state.copy();
            state.add(hexo.rectangle);
            this.stack.push(state);
            break;
          }
        }
      }
    };
  }
  
  
  function paint(puzzle) {
  
  
    puzzle.stack.forEach(async function(state) {
      var shape;
      shape = state.shapes[state.index];
      fill(colors[shape.key])      
      shape.squares.forEach(async function(square) {
        rect(square.x * 25, square.y * 25, 25, 25);
      });
    });
  }
  </code></pre>
  
  <p>My question is how I can improve this code's speed and modify it to get a solution. Any helps would be appreciated!</p>