I do not use JavaScript often, so I will be thankful if you could improve my code. It is my implementation of the game 2048.
Here is a part of the Angular controller:
$scope.grid = new Grid(4);
$scope.key_pressed = function(e) {
if (e.keyCode == 37) {
$scope.grid.moveLeft();
} else if (e.keyCode == 38) {
$scope.grid.moveUp();
} else if (e.keyCode == 39) {
$scope.grid.moveRight();
} else if (e.keyCode == 40) {
$scope.grid.moveDown();
};
$scope.grid.generateRandomCell();
};
And here is an implementation:
function Grid(size) {
this.size = size;
this.algo = new Algo();
this.cells = this.empty();
this.generateRandomCell();
this.generateRandomCell();
}
Grid.prototype.empty = function() {
var result = new Array(this.size);
for (var i = 0; i < this.size; i++)
{
result[i] = new Array(this.size);
for (var j = 0; j < this.size; j++)
result[i][j] = 0;
}
return result;
}
Grid.prototype.get = function(x, y) {
return this.cells[x-1][y-1];
}
Grid.prototype.set = function(x, y, value) {
this.cells[x-1][y-1] = value;
}
Grid.prototype.get_row = function(n) {
var result = new Array(this.size);
for (var i = 0; i < this.size; i++) {
result[i] = this.cells[n-1][i];
}
return result;
}
Grid.prototype.set_row = function(n, row) {
for (var i = 0; i < row.length; i++) {
this.cells[n-1][i] = row[i];
}
}
Grid.prototype.get_column = function(n) {
var result = new Array(this.size);
for (var i = 0; i < this.size; i++) {
result[i] = this.cells[i][n-1];
}
return result;
}
Grid.prototype.set_column = function(n, column) {
for (var i = 0; i < column.length; i++) {
this.cells[i][n-1] = column[i];
}
}
Grid.prototype.moveUp = function() {
for (var i = 1; i <= 4; i++)
{
var column = this.get_column(i)
var result = this.algo.collapseAndReduce(column);
this.set_column(i, result);
}
}
Grid.prototype.moveDown = function() {
for (var i = 1; i <= 4; i++)
{
var column = this.get_column(i)
var result = this.algo.collapseAndReduce(column.reverse());
this.set_column(i, result.reverse());
}
}
Grid.prototype.moveLeft = function() {
for (var i = 1; i <= 4; i++)
{
var row = this.get_row(i)
var result = this.algo.collapseAndReduce(row);
this.set_row(i, result);
}
}
Grid.prototype.moveRight = function() {
for (var i = 1; i <= 4; i++)
{
var row = this.get_row(i)
var result = this.algo.collapseAndReduce(row.reverse());
this.set_row(i, result.reverse());
}
}
Grid.prototype.generateRandomCell = function() {
var x = Math.floor(Math.random() * 4) + 1;
var y = Math.floor(Math.random() * 4) + 1;
console.info(x, y)
if(this.get(x, y) == 0)
{
this.set(x, y, 2);
console.info('hoora');
return;
}
else
{
console.info('woo');
return this.generateRandomCell();
}
}
function Algo() { }
Algo.prototype.canBeCollapsed = function(line) {
var zero = false;
for (var i = 0; i < line.length; i++)
{
if ((zero) && (line[i] != 0))
return true;
if(line[i] == 0)
zero = true;
}
return false;
}
Algo.prototype.collapse = function(line) {
while(this.canBeCollapsed(line))
{
for (var i = 0; i < line.length - 1; i++)
if(line[i] == 0)
{
line[i] = line[i+1];
line[i+1] = 0;
}
}
return line;
}
Algo.prototype.reduce = function(line) {
for (var i = 0; i < line.length - 1; i++)
if(line[i] == line[i+1])
{
line[i] += line[i+1];
line[i+1] = 0;
return line;
}
return line;
}
Algo.prototype.collapseAndReduce = function(line) {
var m = this.collapse(line);
var k = this.reduce(m);
var m = this.collapse(k);
return m;
}