make actuator properly create html elements
[2048.git] / js / game_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function GameManager(size, actuator) {
  this.size       = size; // Grid size
  this.actuator   = actuator;
 
  this.startTiles = 2;
  this.grid       = new Grid(this.size);
 
  this.setup();
}
 
// Set up the game
GameManager.prototype.setup = function () {
  this.addStartTiles();
 
  // Update the actuator
  this.actuate();
};
 
// Set up the initial tiles to start the game with
GameManager.prototype.addStartTiles = function () {
  for (var i = 0; i < this.startTiles; i++) {
    this.addRandomTile();
  }
};
 
// Adds a tile in a random position
GameManager.prototype.addRandomTile = function () {
  var value = Math.random() < 0.9 ? 2 : 4;
  var tile = new Tile(this.grid.randomAvailableCell(), value);
  
  this.grid.insertTile(tile);
};
 
// Sends the updated grid to the actuator
GameManager.prototype.actuate = function () {
  this.actuator.actuate(this.grid);
};
 
// Move the grid in the specified direction
GameManager.prototype.move = function (direction) {
  // 0: up, 1: right, 2:down, 3: left
 
  this.actuate();
};
 
comments