Permalink
Browse files

Allow making of squares which remove all dots of the same color

  • Loading branch information...
1 parent bc498fd commit b6075cf87475d425334b7e08ce8b0722e0591882 @marcinbunsch committed Oct 16, 2014
Showing with 52 additions and 5 deletions.
  1. +7 −0 app/game.coffee
  2. +45 −5 app/move.coffee
View
@@ -27,6 +27,7 @@ class Game
@level = options.level || new Level()
@score = 0
@moves = @level.moves
+ @dots = []
dispose: =>
@level = null
@@ -87,6 +88,12 @@ class Game
column: column
})
dot.draw()
+ @dots.push dot
+
+ removeDot: (dot) =>
+ index = @dots.indexOf(dot)
+ @dots.splice(index, 1)
+ dot.destroy()
module.exports = Game
View
@@ -12,14 +12,29 @@ class Move
complete: =>
Crafty.removeEvent this, Crafty.stage.elem, "mouseup", this.complete
+ if this.squarePresent()
+ this.addAllSameColoredDots()
this.destroyDots()
this.destroyAllLines()
this.updateGameState()
this.dispose()
+ squarePresent: =>
+ groupedDots = this.getGroupedDots()
+ for id, dots of groupedDots
+ return true if dots.length > 1
+ return false
+
+ addAllSameColoredDots: =>
+ uniqueDots = this.getUniqueDots()
+ @game.dots.forEach (dot) =>
+ if dot.color == @color and !uniqueDots[dot.id]
+ @dots.push(dot)
+
updateGameState: =>
if @dots.length > 1
- @game.addPoints(@dots.length * 10)
+ dotCount = this.getUniqueDotCount()
+ @game.addPoints(dotCount * 10)
@game.subtractMove()
dispose: =>
@@ -30,22 +45,47 @@ class Move
destroyDots: =>
if @dots.length > 1
+ replacements = []
uniqueDots = this.getUniqueDots()
- iterator = 0
+ iterator = {}
for id, dot of uniqueDots
- iterator += 1
- dot.destroy()
- this.addReplacementDot(dot.column, iterator)
+ column = dot.column
+ iterator[column] ||= 0
+ iterator[column] += 1
+ this.removeDot(dot)
+ replacements.push({
+ column: column,
+ iterator: iterator[column]
+ })
+ replacements.forEach (data) =>
+ this.addReplacementDot(data.column, data.iterator)
addReplacementDot: (column, index) =>
@game.addDotAt(-index, column)
+ removeDot: (dot) =>
+ @game.removeDot(dot)
+
getUniqueDots: =>
uniqueDots = {}
for index, dot of @dots
uniqueDots[dot.id] = dot
uniqueDots
+ getGroupedDots: =>
+ uniqueDots = {}
+ for index, dot of @dots
+ uniqueDots[dot.id] ||= []
+ uniqueDots[dot.id].push(dot)
+ uniqueDots
+
+ getUniqueDotCount: =>
+ uniqueDots = this.getUniqueDots()
+ count = 0
+ for index, dot of uniqueDots
+ count++
+ count
+
destroyAllLines: =>
for line in @lines
line.destroy()

0 comments on commit b6075cf

Please sign in to comment.