Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
core logic with tests
  • Loading branch information
Zolmeister committed Mar 18, 2014
1 parent 98fe5a2 commit e0f1a83
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
94 changes: 94 additions & 0 deletions board.js
@@ -0,0 +1,94 @@
function Board(grid) {
this.grid = grid || _.map(Array(4), function() {
return _.map(Array(4), function() {
return 0
})
})

this.spawn = function() {

// get random empty grid cell
var emptyCells = []
for(var r=0;r<this.grid.length;r++) {
for(var c=0;c<this.grid[0].length;c++) {
if(!this.grid[r][c]) {
emptyCells.push([r,c])
}
}
}

if(emptyCells.length === 0) {
return this.endGame()
}

var target = _.sample(emptyCells)

// fill with either the first or second colors
var color = 1 //Math.random() > .9 ? 2 : 1;
this.grid[target[0]][target[1]] = color

// add element to DOM
}

this.endGame = function() {
// show end game screen
alert('game over')
}

this.move = function(dir) {
this._move(dir)
this.spawn()
}

this._move = function(dir) {
var keymap = {
up: [-1, 0],
down: [1, 0],
left: [0, -1],
right: [0, 1]
}

var grid = this.grid
var diff = keymap[dir]
var rows = _.range(0, 4)
var cols = _.range(0, 4)

if(dir === 'down') {
rows.reverse()
}
if(dir === 'right') {
cols.reverse()
}

// Hack, is something has been combined, it has 0.1 added to it temporarily
for(var r=0;r<rows.length;r++) {
for(var c=0;c<cols.length;c++) {
var row = rows[r]
var col = cols[c]
if (grid[row] && grid[row][col]) {
while (grid[row + diff[0]] && (grid[row + diff[0]][col + diff[1]] === 0 ||
grid[row][col] === grid[row + diff[0]][col + diff[1]])) {

var combine = grid[row][col]
if (grid[row][col] === grid[row + diff[0]][col + diff[1]]) {
combine = grid[row][col] + 1.1
}

grid[row][col] = 0
row += diff[0]
col += diff[1]
grid[row][col] = combine
}
}
}
}

this.grid = _.map(grid, function(row) {
return _.map(row, Math.floor.bind(Math))
})
}
}

if(typeof module !== 'undefined') {
module.exports = Board
}
8 changes: 8 additions & 0 deletions index.html

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions main.js
@@ -0,0 +1,21 @@
var $ = document.querySelectorAll.bind(document);

var GAME = {
board: new Board()
}

// keybindings
Hammer($('.grid')[0]).on("swipeleft", function(e) {
GAME.board.move('left')
}).on("swiperight", function(e) {
GAME.board.move('right')
}).on("swipeup", function(e) {
GAME.board.move('up')
}).on("swipedown", function(e) {
GAME.board.move('down')
});

Mousetrap.bind(['up', 'down', 'left', 'right'], function(e) {
GAME.board.move(e.keyIdentifier.toLowerCase())
});

17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{
"name": "prism",
"version": "0.0.0",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/Zolmeister/prism.git"
},
"devDependencies": {
"lodash": "~2.4.1"
}
}
187 changes: 187 additions & 0 deletions test/main.js
@@ -0,0 +1,187 @@
var assert = require("assert")
_ = require('lodash')
var Board = require('./../board')

var board;

function stringify(board) {
return JSON.stringify(board.grid)
}

function flat(board) {
return JSON.stringify(_.filter(_.flatten(board.grid)))
}

function pretty(board) {
console.log()
for(var r=0;r<board.grid.length;r++) {
console.log(JSON.stringify(board.grid[r]))
}
console.log()
}

describe('Game Board', function () {
describe('init()', function () {
it('should create a 4x4 grid', function() {
board = new Board()
assert.strictEqual(board.grid.length, 4)
assert.strictEqual(board.grid[0].length, 4)
})
})

describe('spawn()', function () {
it('should spawn a value in a random location', function () {

assert.strictEqual(flat(board), '[]')
board.spawn()
assert.strictEqual(flat(board), '[1]')
board.spawn()
assert.strictEqual(flat(board), '[1,1]')

})
})


describe('move() - simple', function() {

var board;

beforeEach(function() {
board = new Board([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]
])
})

it('should move right', function() {
board._move('right')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
]))
})

it('should move left', function() {
board._move('left')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0]
]))
})

it('should move up', function() {
board._move('up')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]))
})

it('should move down', function() {
board._move('down')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]
]))
})
})

describe('move() - advanced', function() {

it('should apply right one', function() {
var board = new Board([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 0]
])

board._move('right')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 2],
[0, 0, 0, 0]
]))
})

it('should not apply right one', function() {
var board = new Board([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 2],
[0, 0, 0, 0]
])

board._move('right')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 2],
[0, 0, 0, 0]
]))
})

it('should apply three right two', function() {
var board = new Board([
[0, 0, 0, 0],
[0, 0, 2, 2],
[0, 1, 1, 0],
[0, 3, 0, 3]
])

board._move('right')
assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 0, 0, 3],
[0, 0, 0, 2],
[0, 0, 0, 4]
]))
})

it('should apply up two', function() {
var board = new Board([
[0, 1, 0, 0],
[0, 2, 2, 2],
[4, 2, 1, 0],
[4, 3, 0, 3]
])
board._move('up')

assert.strictEqual(stringify(board), JSON.stringify([
[5, 1, 2, 2],
[0, 3, 1, 3],
[0, 3, 0, 0],
[0, 0, 0, 0]
]))
})

it('should apply down two', function() {
var board = new Board([
[0, 1, 0, 0],
[0, 2, 2, 2],
[0, 2, 1, 0],
[0, 3, 0, 3]
])
board._move('down')

assert.strictEqual(stringify(board), JSON.stringify([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 3, 2, 2],
[0, 3, 1, 3]
]))
})

})
})

0 comments on commit e0f1a83

Please sign in to comment.