Permalink
Browse files

Add block matching algorithm.

  • Loading branch information...
1 parent 2acde69 commit 4de1bcd2323442dd5d793de15c7bc63f33b2ea39 @jimmycuadra committed Nov 18, 2012
@@ -16,6 +16,7 @@ class ph.Application
@setUpGrid(rowCount, columnCount)
@setUpSounds()
@setUpHUD()
+ @on("push", @markMatches)
$("#loading").remove()
setUpGrid: (rowCount, columnCount) ->
@@ -55,6 +56,25 @@ class ph.Application
upperNeighbors[j] = new ph.Cell(upperNeighbor: upperNeighbor)
new ph.CellRow(collection)
+ markMatches: ->
+ marked = []
+
+ for row, rowIndex in @grid.rows[0..@grid.rows.length - 3]
+ for cell, columnIndex in row.models
+ tempMarked = [cell]
+ color = cell.get("color")
+ i = 1
+ nextCell = @grid.rows[rowIndex + i].at(columnIndex)
+ while nextCell.get("color") is color
+ tempMarked.push(nextCell)
+ i++
+ nextCell = @grid.rows[rowIndex + i].at(columnIndex)
+ if tempMarked.length >= 3
+ marked.push.apply(marked, tempMarked)
+
+ _.each marked, (cell) ->
+ cell.trigger("clear")
+
_.extend(ph.Application.prototype, Backbone.Events)
$ ->
@@ -8,3 +8,8 @@ class ph.Cell extends Backbone.Model
colors = _.difference(colors, [upperNeighbor.get("color")])
index = Math.floor(Math.random() * colors.length)
@set("color", colors[index])
+
+ @on("clear", @clear)
+
+ clear: =>
+ @set("color", null)
@@ -26,3 +26,4 @@ class ph.CellRowView extends Backbone.View
nextColor = cell.get("color")
cell.set("color", newColor)
ph.app.sfx.trigger("play", "push") if ph.app.store.get("playSoundEffects")
+ ph.app.trigger("push")

0 comments on commit 4de1bcd

Please sign in to comment.