@@ -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