Skip to content

Commit e0f1a83

Browse files
committed
core logic with tests
1 parent 98fe5a2 commit e0f1a83

File tree

6 files changed

+328
-0
lines changed

6 files changed

+328
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

board.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function Board(grid) {
2+
this.grid = grid || _.map(Array(4), function() {
3+
return _.map(Array(4), function() {
4+
return 0
5+
})
6+
})
7+
8+
this.spawn = function() {
9+
10+
// get random empty grid cell
11+
var emptyCells = []
12+
for(var r=0;r<this.grid.length;r++) {
13+
for(var c=0;c<this.grid[0].length;c++) {
14+
if(!this.grid[r][c]) {
15+
emptyCells.push([r,c])
16+
}
17+
}
18+
}
19+
20+
if(emptyCells.length === 0) {
21+
return this.endGame()
22+
}
23+
24+
var target = _.sample(emptyCells)
25+
26+
// fill with either the first or second colors
27+
var color = 1 //Math.random() > .9 ? 2 : 1;
28+
this.grid[target[0]][target[1]] = color
29+
30+
// add element to DOM
31+
}
32+
33+
this.endGame = function() {
34+
// show end game screen
35+
alert('game over')
36+
}
37+
38+
this.move = function(dir) {
39+
this._move(dir)
40+
this.spawn()
41+
}
42+
43+
this._move = function(dir) {
44+
var keymap = {
45+
up: [-1, 0],
46+
down: [1, 0],
47+
left: [0, -1],
48+
right: [0, 1]
49+
}
50+
51+
var grid = this.grid
52+
var diff = keymap[dir]
53+
var rows = _.range(0, 4)
54+
var cols = _.range(0, 4)
55+
56+
if(dir === 'down') {
57+
rows.reverse()
58+
}
59+
if(dir === 'right') {
60+
cols.reverse()
61+
}
62+
63+
// Hack, is something has been combined, it has 0.1 added to it temporarily
64+
for(var r=0;r<rows.length;r++) {
65+
for(var c=0;c<cols.length;c++) {
66+
var row = rows[r]
67+
var col = cols[c]
68+
if (grid[row] && grid[row][col]) {
69+
while (grid[row + diff[0]] && (grid[row + diff[0]][col + diff[1]] === 0 ||
70+
grid[row][col] === grid[row + diff[0]][col + diff[1]])) {
71+
72+
var combine = grid[row][col]
73+
if (grid[row][col] === grid[row + diff[0]][col + diff[1]]) {
74+
combine = grid[row][col] + 1.1
75+
}
76+
77+
grid[row][col] = 0
78+
row += diff[0]
79+
col += diff[1]
80+
grid[row][col] = combine
81+
}
82+
}
83+
}
84+
}
85+
86+
this.grid = _.map(grid, function(row) {
87+
return _.map(row, Math.floor.bind(Math))
88+
})
89+
}
90+
}
91+
92+
if(typeof module !== 'undefined') {
93+
module.exports = Board
94+
}

index.html

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.

main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var $ = document.querySelectorAll.bind(document);
2+
3+
var GAME = {
4+
board: new Board()
5+
}
6+
7+
// keybindings
8+
Hammer($('.grid')[0]).on("swipeleft", function(e) {
9+
GAME.board.move('left')
10+
}).on("swiperight", function(e) {
11+
GAME.board.move('right')
12+
}).on("swipeup", function(e) {
13+
GAME.board.move('up')
14+
}).on("swipedown", function(e) {
15+
GAME.board.move('down')
16+
});
17+
18+
Mousetrap.bind(['up', 'down', 'left', 'right'], function(e) {
19+
GAME.board.move(e.keyIdentifier.toLowerCase())
20+
});
21+

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "prism",
3+
"version": "0.0.0",
4+
"directories": {
5+
"test": "test"
6+
},
7+
"scripts": {
8+
"test": "mocha"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/Zolmeister/prism.git"
13+
},
14+
"devDependencies": {
15+
"lodash": "~2.4.1"
16+
}
17+
}

test/main.js

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
var assert = require("assert")
2+
_ = require('lodash')
3+
var Board = require('./../board')
4+
5+
var board;
6+
7+
function stringify(board) {
8+
return JSON.stringify(board.grid)
9+
}
10+
11+
function flat(board) {
12+
return JSON.stringify(_.filter(_.flatten(board.grid)))
13+
}
14+
15+
function pretty(board) {
16+
console.log()
17+
for(var r=0;r<board.grid.length;r++) {
18+
console.log(JSON.stringify(board.grid[r]))
19+
}
20+
console.log()
21+
}
22+
23+
describe('Game Board', function () {
24+
describe('init()', function () {
25+
it('should create a 4x4 grid', function() {
26+
board = new Board()
27+
assert.strictEqual(board.grid.length, 4)
28+
assert.strictEqual(board.grid[0].length, 4)
29+
})
30+
})
31+
32+
describe('spawn()', function () {
33+
it('should spawn a value in a random location', function () {
34+
35+
assert.strictEqual(flat(board), '[]')
36+
board.spawn()
37+
assert.strictEqual(flat(board), '[1]')
38+
board.spawn()
39+
assert.strictEqual(flat(board), '[1,1]')
40+
41+
})
42+
})
43+
44+
45+
describe('move() - simple', function() {
46+
47+
var board;
48+
49+
beforeEach(function() {
50+
board = new Board([
51+
[0, 0, 0, 0],
52+
[0, 0, 0, 0],
53+
[0, 0, 1, 0],
54+
[0, 0, 0, 0]
55+
])
56+
})
57+
58+
it('should move right', function() {
59+
board._move('right')
60+
assert.strictEqual(stringify(board), JSON.stringify([
61+
[0, 0, 0, 0],
62+
[0, 0, 0, 0],
63+
[0, 0, 0, 1],
64+
[0, 0, 0, 0]
65+
]))
66+
})
67+
68+
it('should move left', function() {
69+
board._move('left')
70+
assert.strictEqual(stringify(board), JSON.stringify([
71+
[0, 0, 0, 0],
72+
[0, 0, 0, 0],
73+
[1, 0, 0, 0],
74+
[0, 0, 0, 0]
75+
]))
76+
})
77+
78+
it('should move up', function() {
79+
board._move('up')
80+
assert.strictEqual(stringify(board), JSON.stringify([
81+
[0, 0, 1, 0],
82+
[0, 0, 0, 0],
83+
[0, 0, 0, 0],
84+
[0, 0, 0, 0]
85+
]))
86+
})
87+
88+
it('should move down', function() {
89+
board._move('down')
90+
assert.strictEqual(stringify(board), JSON.stringify([
91+
[0, 0, 0, 0],
92+
[0, 0, 0, 0],
93+
[0, 0, 0, 0],
94+
[0, 0, 1, 0]
95+
]))
96+
})
97+
})
98+
99+
describe('move() - advanced', function() {
100+
101+
it('should apply right one', function() {
102+
var board = new Board([
103+
[0, 0, 0, 0],
104+
[0, 0, 0, 0],
105+
[0, 0, 1, 1],
106+
[0, 0, 0, 0]
107+
])
108+
109+
board._move('right')
110+
assert.strictEqual(stringify(board), JSON.stringify([
111+
[0, 0, 0, 0],
112+
[0, 0, 0, 0],
113+
[0, 0, 0, 2],
114+
[0, 0, 0, 0]
115+
]))
116+
})
117+
118+
it('should not apply right one', function() {
119+
var board = new Board([
120+
[0, 0, 0, 0],
121+
[0, 0, 0, 0],
122+
[0, 0, 1, 2],
123+
[0, 0, 0, 0]
124+
])
125+
126+
board._move('right')
127+
assert.strictEqual(stringify(board), JSON.stringify([
128+
[0, 0, 0, 0],
129+
[0, 0, 0, 0],
130+
[0, 0, 1, 2],
131+
[0, 0, 0, 0]
132+
]))
133+
})
134+
135+
it('should apply three right two', function() {
136+
var board = new Board([
137+
[0, 0, 0, 0],
138+
[0, 0, 2, 2],
139+
[0, 1, 1, 0],
140+
[0, 3, 0, 3]
141+
])
142+
143+
board._move('right')
144+
assert.strictEqual(stringify(board), JSON.stringify([
145+
[0, 0, 0, 0],
146+
[0, 0, 0, 3],
147+
[0, 0, 0, 2],
148+
[0, 0, 0, 4]
149+
]))
150+
})
151+
152+
it('should apply up two', function() {
153+
var board = new Board([
154+
[0, 1, 0, 0],
155+
[0, 2, 2, 2],
156+
[4, 2, 1, 0],
157+
[4, 3, 0, 3]
158+
])
159+
board._move('up')
160+
161+
assert.strictEqual(stringify(board), JSON.stringify([
162+
[5, 1, 2, 2],
163+
[0, 3, 1, 3],
164+
[0, 3, 0, 0],
165+
[0, 0, 0, 0]
166+
]))
167+
})
168+
169+
it('should apply down two', function() {
170+
var board = new Board([
171+
[0, 1, 0, 0],
172+
[0, 2, 2, 2],
173+
[0, 2, 1, 0],
174+
[0, 3, 0, 3]
175+
])
176+
board._move('down')
177+
178+
assert.strictEqual(stringify(board), JSON.stringify([
179+
[0, 0, 0, 0],
180+
[0, 1, 0, 0],
181+
[0, 3, 2, 2],
182+
[0, 3, 1, 3]
183+
]))
184+
})
185+
186+
})
187+
})

0 commit comments

Comments
 (0)