Skip to content

Commit 512cb98

Browse files
committed
Refactor Move to adhere to OCP and LSP
OCP is Open/Closed Principle LSP is Liskov substitution principle
1 parent b6075cf commit 512cb98

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

app/dot.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Colors = require('colors')
22
Entity = require('entity')
3-
Move = require('move')
3+
Move = require('move_with_squares')
44

55
class Dot extends Entity
66

app/move.coffee

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,11 @@ class Move
1212

1313
complete: =>
1414
Crafty.removeEvent this, Crafty.stage.elem, "mouseup", this.complete
15-
if this.squarePresent()
16-
this.addAllSameColoredDots()
1715
this.destroyDots()
1816
this.destroyAllLines()
1917
this.updateGameState()
2018
this.dispose()
2119

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-
3420
updateGameState: =>
3521
if @dots.length > 1
3622
dotCount = this.getUniqueDotCount()
@@ -174,3 +160,4 @@ class Move
174160
@dots.push(dot)
175161

176162
module.exports = Move
163+

app/move_with_squares.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Move = require('move')
2+
3+
class MoveWithSquares extends Move
4+
5+
complete: =>
6+
if this.squarePresent()
7+
this.addAllSameColoredDots()
8+
super()
9+
10+
squarePresent: =>
11+
groupedDots = this.getGroupedDots()
12+
for id, dots of groupedDots
13+
return true if dots.length > 1
14+
return false
15+
16+
addAllSameColoredDots: =>
17+
uniqueDots = this.getUniqueDots()
18+
@game.dots.forEach (dot) =>
19+
if dot.color == @color and !uniqueDots[dot.id]
20+
@dots.push(dot)
21+
22+
module.exports = MoveWithSquares

0 commit comments

Comments
 (0)