Skip to content

Commit

Permalink
Allow making of squares which remove all dots of the same color
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbunsch committed Oct 16, 2014
1 parent bc498fd commit b6075cf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/game.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Game
@level = options.level || new Level()
@score = 0
@moves = @level.moves
@dots = []

dispose: =>
@level = null
Expand Down Expand Up @@ -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

50 changes: 45 additions & 5 deletions app/move.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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: =>
Expand All @@ -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()
Expand Down

0 comments on commit b6075cf

Please sign in to comment.