Skip to content

Commit

Permalink
basic grid implementation with initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielecirulli committed Mar 8, 2014
1 parent 0970a72 commit cf31e14
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"esnext": true,
"indent": 2,
"maxlen": 80,
"freeze": true,
"camelcase": true,
"unused": true,
"eqnull": true,
"proto": true,
"supernew": true,
"noyield": true,
"evil": true,
"node": true,
"boss": true,
"expr": true,
"loopfunc": true,
"white": true,
"maxdepth": 4
}
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<title>2048</title>

<link href="style/main.css" rel="stylesheet" type="text/css">

<script src="js/html_actuator.js"></script>
<script src="js/tile.js"></script>
<script src="js/game_manager.js"></script>
<script src="js/application.js"></script>
</head>
<body>
<div class="container">
Expand Down
4 changes: 4 additions & 0 deletions js/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
document.addEventListener("DOMContentLoaded", function () {
var actuator = new HTMLActuator;
var manager = new GameManager(4, actuator);
});
83 changes: 83 additions & 0 deletions js/game_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function GameManager(size, actuator) {
this.size = size; // Grid size
this.actuator = actuator;

this.startTiles = 2;
this.grid = [];

this.setup();
}

// Set up the game
GameManager.prototype.setup = function () {
this.buildGrid();
this.addStartTiles();

// Update the actuator
this.update();
};

// Build a grid of the specified size
GameManager.prototype.buildGrid = function () {
for (var y = 0; y < this.size; y++) {
this.grid[y] = [];
for (var x = 0; x < this.size; x++) {
this.grid[y].push(null);
}
}
};

// Set up the initial tiles to start the game with
GameManager.prototype.addStartTiles = function () {
for (var i = 0; i < this.startTiles; i++) {
this.addTile();
}
};

// Adds a tile in a random position
GameManager.prototype.addTile = function () {
this.insertTile(new Tile(this.randomCell()));
};

// Find the first available random position
GameManager.prototype.randomCell = function () {
// TODO: build a map of available positions and choose from it
var self = this;

var position;

function randomPosition() {
return Math.floor(Math.random() * self.size);
}

do {
position = {
x: randomPosition(),
y: randomPosition()
};
} while (this.cellOccupied(position));

return position;
};

// Check if the specified cell is taken
GameManager.prototype.cellOccupied = function (cell) {
return !!this.grid[cell.x][cell.y];
};

// Insert a tile at the specified position
GameManager.prototype.insertTile = function (tile) {
this.grid[tile.x][tile.y] = tile;
};

// Sends the updated grid to the actuator
GameManager.prototype.update = function () {
this.actuator.update(this.grid);
};

// Move the grid in the specified direction
GameManager.prototype.move = function (direction) {
// 0: up, 1: right, 2:down, 3: left

this.update();
};
13 changes: 13 additions & 0 deletions js/html_actuator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function HTMLActuator() {

}

HTMLActuator.prototype.update = function (grid) {
// Temporary debug visualizer
grid.forEach(function (row) {
var mapped = row.map(function (tile) {
return tile ? tile.value : " ";
}).join(" | ");
console.log(mapped);
});
};
7 changes: 7 additions & 0 deletions js/tile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Tile(position, value) {
this.x = position.x;
this.y = position.y;
this.value = value || 2;

this.previousPosition = null;
}

0 comments on commit cf31e14

Please sign in to comment.