make actuator properly create html elements
make actuator properly create html elements

file:a/index.html -> file:b/index.html
--- a/index.html
+++ b/index.html
@@ -46,18 +46,7 @@
       </div>
 
       <div class="tile-container">
-        <div class="tile tile-2 tile-position-1-1">
-          2
-        </div>
-         <div class="tile tile-4 tile-position-2-3">
-            4
-          </div>
-          <div class="tile tile-512 tile-position-3-2">
-            512
-          </div>
-          <div class="tile tile-2048 tile-position-4-4">
-            2048
-          </div>
+
       </div>
     </div>
 

--- a/js/game_manager.js
+++ b/js/game_manager.js
@@ -25,7 +25,9 @@
 
 // Adds a tile in a random position
 GameManager.prototype.addRandomTile = function () {
-  var tile = new Tile(this.grid.randomAvailableCell());
+  var value = Math.random() < 0.9 ? 2 : 4;
+  var tile = new Tile(this.grid.randomAvailableCell(), value);
+  
   this.grid.insertTile(tile);
 };
 

--- a/js/html_actuator.js
+++ b/js/html_actuator.js
@@ -1,14 +1,37 @@
 function HTMLActuator() {
-
+  this.tileContainer = document.getElementsByClassName("tile-container")[0];
 }
 
 HTMLActuator.prototype.actuate = function (grid) {
-  // Temporary debug visualizer
-  grid.cells.forEach(function (row) {
-    var mapped = row.map(function (tile) {
-      return tile ? tile.value : " ";
-    }).join(" | ");
-    console.log(mapped);
+  var self = this;
+
+  this.clearContainer();
+
+  grid.cells.forEach(function (column) {
+    column.forEach(function (cell) {
+      if (cell) {
+        self.addTile(cell);
+      }
+    });
   });
 };
 
+HTMLActuator.prototype.clearContainer = function () {
+  while (this.tileContainer.firstChild) {
+    this.tileContainer.removeChild(this.tileContainer.firstChild);
+  }
+};
+
+HTMLActuator.prototype.addTile = function (tile) {
+  var element = document.createElement("div");
+
+  var x = tile.x + 1;
+  var y = tile.y + 1;
+  var position = "tile-position-" + x + "-" + y;
+
+  element.classList.add("tile", "tile-" + tile.value, position);
+  element.textContent = tile.value;
+
+  this.tileContainer.appendChild(element);
+};
+

comments