提交 b7476f1f 编写于 作者: G Gabriele Cirulli

make actuator properly create html elements

上级 dca5207f
......@@ -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>
......
......@@ -25,7 +25,9 @@ GameManager.prototype.addStartTiles = function () {
// 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);
};
......
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);
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册