Skip to content

Commit

Permalink
Moving blocks! Yeah!
Browse files Browse the repository at this point in the history
  • Loading branch information
jlfwong committed Oct 13, 2011
0 parents commit 4a05fa2
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
web: {
},
debug: {
},
release: {
}
};
118 changes: 118 additions & 0 deletions scripts/puzzle.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
CELL_SIZE = 100
ABOVE = 0
RIGHT = 1
LEFT = 2
BELOW = 3

directionToDelta = (direction) ->
switch direction
when ABOVE then [-1, 0]
when RIGHT then [0, 1]
when BELOW then [1, 0]
when LEFT then [0, -1]

INIT_GRID = [
[1 , 2 , 3 , 4 ]
[5 , 6 , 7 , 8 ]
[9 , 10 , 11 , 12 ]
[13 , 14 , 15 , 0 ]
]

class PuzzleCellView
constructor: (num) ->
@node = $ "<div/>",
text: num
css:
width : "#{CELL_SIZE - 2}px"
height : "#{CELL_SIZE - 2}px"
textAlign : "center"
lineHeight : "#{CELL_SIZE}px"
fontSize : "30px"
border : "1px solid black"
position : "absolute"

origRowNum = parseInt((num - 1) / 4, 10)
origColNum = parseInt((num - 1) % 4, 10)

if (origRowNum + origColNum) % 2 == 0
@node.css
backgroundColor: 'black'
color: 'white'
else
@node.css
backgroundColor: 'white'
color: 'black'

setPosition: (rowNum, colNum, duration=0, cb=$.noop) ->
@node.animate {
top : "#{rowNum * CELL_SIZE}px"
left : "#{colNum * CELL_SIZE}px"
}, duration, cb

class PuzzleGridView
constructor: ($el, grid) ->
$el.css
position : "relative"
width : "#{4 * CELL_SIZE}px"
height : "#{4 * CELL_SIZE}px"

@moveQueue = []
@cellViews = []

for rowNum of grid
rowNum = parseInt(rowNum, 10)
@cellViews[rowNum] = []
for colNum of grid[rowNum]
colNum = parseInt(colNum, 10)

num = grid[rowNum][colNum]

if num == 0
cell = null
@emptyPos = [rowNum, colNum]
if num != 0
cell = new PuzzleCellView(num)
cell.setPosition rowNum, colNum

$el.append(cell.node)

@cellViews[rowNum].push(cell)

queueMoves: (moves) ->
@moveQueue = @moveQueue.concat(moves)

runQueue: (duration) ->
if @moveQueue.length != 0
@moveFrom @moveQueue.shift(), duration, =>
@runQueue(duration)

moveFrom: (sourceDirection, duration, cb) ->
[targetRow, targetCol] = @emptyPos
[deltaRow, deltaCol] = directionToDelta sourceDirection
@emptyPos = [sourceRow, sourceCol] = [
targetRow + deltaRow,
targetCol + deltaCol
]

cellView = @cellViews[sourceRow][sourceCol]
@cellViews[targetRow][targetCol] = cellView
@cellViews[sourceRow][sourceCol] = null

cellView.setPosition targetRow, targetCol, duration, cb

class Grid
constructor: (grid, emptyPos) ->
@emptyPos = _.clone emptyPos

@grid = []
for row in grid
@grid.push _.clone(row)

class @Puzzle
constructor: ($el) ->
@grid = new Grid(INIT_GRID, [3, 3])

@gridView = new PuzzleGridView($el, INIT_GRID)

@gridView.queueMoves [ABOVE, LEFT, ABOVE, LEFT]
@gridView.runQueue(150)
137 changes: 137 additions & 0 deletions scripts/puzzle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions scripts/underscore-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions templates/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
!!! 5
html(lang="en")
head
title Sliding Puzzle
script(src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js")
script(src="scripts/underscore-min.js")
script(src="scripts/puzzle.js")
body
#puzzle
script
var puzzle = new Puzzle($('#puzzle'))

0 comments on commit 4a05fa2

Please sign in to comment.