Skip to content

Commit b6075cf

Browse files
committed
Allow making of squares which remove all dots of the same color
1 parent bc498fd commit b6075cf

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

app/game.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Game
2727
@level = options.level || new Level()
2828
@score = 0
2929
@moves = @level.moves
30+
@dots = []
3031

3132
dispose: =>
3233
@level = null
@@ -87,6 +88,12 @@ class Game
8788
column: column
8889
})
8990
dot.draw()
91+
@dots.push dot
92+
93+
removeDot: (dot) =>
94+
index = @dots.indexOf(dot)
95+
@dots.splice(index, 1)
96+
dot.destroy()
9097

9198
module.exports = Game
9299

app/move.coffee

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,29 @@ class Move
1212

1313
complete: =>
1414
Crafty.removeEvent this, Crafty.stage.elem, "mouseup", this.complete
15+
if this.squarePresent()
16+
this.addAllSameColoredDots()
1517
this.destroyDots()
1618
this.destroyAllLines()
1719
this.updateGameState()
1820
this.dispose()
1921

22+
squarePresent: =>
23+
groupedDots = this.getGroupedDots()
24+
for id, dots of groupedDots
25+
return true if dots.length > 1
26+
return false
27+
28+
addAllSameColoredDots: =>
29+
uniqueDots = this.getUniqueDots()
30+
@game.dots.forEach (dot) =>
31+
if dot.color == @color and !uniqueDots[dot.id]
32+
@dots.push(dot)
33+
2034
updateGameState: =>
2135
if @dots.length > 1
22-
@game.addPoints(@dots.length * 10)
36+
dotCount = this.getUniqueDotCount()
37+
@game.addPoints(dotCount * 10)
2338
@game.subtractMove()
2439

2540
dispose: =>
@@ -30,22 +45,47 @@ class Move
3045

3146
destroyDots: =>
3247
if @dots.length > 1
48+
replacements = []
3349
uniqueDots = this.getUniqueDots()
34-
iterator = 0
50+
iterator = {}
3551
for id, dot of uniqueDots
36-
iterator += 1
37-
dot.destroy()
38-
this.addReplacementDot(dot.column, iterator)
52+
column = dot.column
53+
iterator[column] ||= 0
54+
iterator[column] += 1
55+
this.removeDot(dot)
56+
replacements.push({
57+
column: column,
58+
iterator: iterator[column]
59+
})
60+
replacements.forEach (data) =>
61+
this.addReplacementDot(data.column, data.iterator)
3962

4063
addReplacementDot: (column, index) =>
4164
@game.addDotAt(-index, column)
4265

66+
removeDot: (dot) =>
67+
@game.removeDot(dot)
68+
4369
getUniqueDots: =>
4470
uniqueDots = {}
4571
for index, dot of @dots
4672
uniqueDots[dot.id] = dot
4773
uniqueDots
4874

75+
getGroupedDots: =>
76+
uniqueDots = {}
77+
for index, dot of @dots
78+
uniqueDots[dot.id] ||= []
79+
uniqueDots[dot.id].push(dot)
80+
uniqueDots
81+
82+
getUniqueDotCount: =>
83+
uniqueDots = this.getUniqueDots()
84+
count = 0
85+
for index, dot of uniqueDots
86+
count++
87+
count
88+
4989
destroyAllLines: =>
5090
for line in @lines
5191
line.destroy()

0 commit comments

Comments
 (0)