diff --git a/sketch.js b/sketch.js index c3a01ac..077be81 100644 --- a/sketch.js +++ b/sketch.js @@ -16,24 +16,28 @@ function setup() { createCanvas(800, 800); // Loaded and created the tiles - tiles[0] = new Tile(tileImages[0], ["AAA", "AAA", "AAA", "AAA"]); - tiles[1] = new Tile(tileImages[1], ["BBB", "BBB", "BBB", "BBB"]); - tiles[2] = new Tile(tileImages[2], ["BBB", "BCB", "BBB", "BBB"]); - tiles[3] = new Tile(tileImages[3], ["BBB", "BDB", "BBB", "BDB"]); - tiles[4] = new Tile(tileImages[4], ["ABB", "BCB", "BBA", "AAA"]); - tiles[5] = new Tile(tileImages[5], ["ABB", "BBB", "BBB", "BBA"]); - tiles[6] = new Tile(tileImages[6], ["BBB", "BCB", "BBB", "BCB"]); - tiles[7] = new Tile(tileImages[7], ["BDB", "BCB", "BDB", "BCB"]); - tiles[8] = new Tile(tileImages[8], ["BDB", "BBB", "BCB", "BBB"]); - tiles[9] = new Tile(tileImages[9], ["BCB", "BCB", "BBB", "BCB"]); - tiles[10] = new Tile(tileImages[10], ["BCB", "BCB", "BCB", "BCB"]); - tiles[11] = new Tile(tileImages[11], ["BCB", "BCB", "BBB", "BBB"]); - tiles[12] = new Tile(tileImages[12], ["BBB", "BCB", "BBB", "BCB"]); - - for (let i = 2; i < 14; i++) { + const initialTiles = [ + new Tile(tileImages[0], ["AAA", "AAA", "AAA", "AAA"]), + new Tile(tileImages[1], ["BBB", "BBB", "BBB", "BBB"]), + new Tile(tileImages[2], ["BBB", "BCB", "BBB", "BBB"]), + new Tile(tileImages[3], ["BBB", "BDB", "BBB", "BDB"]), + new Tile(tileImages[4], ["ABB", "BCB", "BBA", "AAA"]), + new Tile(tileImages[5], ["ABB", "BBB", "BBB", "BBA"]), + new Tile(tileImages[6], ["BBB", "BCB", "BBB", "BCB"]), + new Tile(tileImages[7], ["BDB", "BCB", "BDB", "BCB"]), + new Tile(tileImages[8], ["BDB", "BBB", "BCB", "BBB"]), + new Tile(tileImages[9], ["BCB", "BCB", "BBB", "BCB"]), + new Tile(tileImages[10], ["BCB", "BCB", "BCB", "BCB"]), + new Tile(tileImages[11], ["BCB", "BCB", "BBB", "BBB"]), + new Tile(tileImages[12], ["BBB", "BCB", "BBB", "BCB"]), + ]; + + for (const tile of initialTiles) { + const rotations = [tile]; for (let j = 1; j < 4; j++) { - tiles.push(tiles[i].rotate(j)); + rotations.push(tile.rotate(j)); } + tiles.push(...Tile.removeDuplicates(rotations)); } // tiles[2] = tiles[1].rotate(1); diff --git a/tile.js b/tile.js index a451fda..e3acf3c 100644 --- a/tile.js +++ b/tile.js @@ -56,4 +56,13 @@ class Tile { } return new Tile(newImg, newEdges); } + + static removeDuplicates(arrayOfTiles) { + const uniqueTilesMap = {}; + for (const tile of arrayOfTiles) { + const key = tile.edges.join(","); // ex: "ABB,BCB,BBA,AAA" + uniqueTilesMap[key] = tile; + } + return Object.values(uniqueTilesMap); + } }