1
1
$ = require ' jquery'
2
- SolutionService = require ' ./SolutionService'
3
2
4
3
class ShareGameService
5
4
6
- @ reloadPageWithHash: (board , solutionPlacements ) ->
7
- unless @ checkSolutionPlacements board, solutionPlacements
5
+ @ reloadPageWithHash: (board , solutionPlacements , SolutionService ) ->
6
+ unless @ checkSolutionPlacements board, solutionPlacements, SolutionService
8
7
window .location .hash = ' '
9
- console .log window .location .hash
10
8
return false
11
9
hash = @ encode board .initialValues , board .goals , solutionPlacements
12
10
window .location .hash = hash
13
11
14
- @ encode: (boardValues , goals , slnPlacements ) ->
12
+ @ encode: (boardValues , goals , solutionPlacements ) ->
15
13
boardValues = (JSON .stringify boardValues).replace (/ (\[ | \] | "| ,| {| })* / g , ' ' )
16
14
17
15
length = Math .sqrt boardValues .length
18
- for placement in [0 ... slnPlacements .length ]
19
- for coord in [0 ... slnPlacements[placement].length ]
20
- slnPlacements[placement][coord] = slnPlacements[placement][coord][0 ] * length + slnPlacements[placement][coord][1 ]
16
+ for list in [0 ... solutionPlacements .length ]
17
+ for pos in [0 ... solutionPlacements[list].length ]
18
+ solutionPlacements[list][pos] = solutionPlacements[list][pos][0 ] * length +
19
+ solutionPlacements[list][pos][1 ]
21
20
22
- btoa (JSON .stringify {b : boardValues, g : goals, p : slnPlacements })
21
+ btoa (JSON .stringify {b : boardValues, g : goals, p : solutionPlacements })
23
22
24
- @ decode: (boardValues , goals , slnPlacements ) ->
23
+ @ decode: (boardValues , goals , solutionPlacements ) ->
25
24
try
26
25
decoded = atob window .location .hash .substr (1 , window .location .hash .length )
27
26
decoded = JSON .parse decoded
28
27
catch e
29
- return false
30
- return false unless decoded? and @ isValidDecode decoded
31
- return false unless decoded .b ? and decoded .g ? and decoded .p ?
32
- length = Math .sqrt decoded .b .length
33
- index = 0
34
-
35
- for i in [0 ... length]
36
- row = []
37
- for j in [0 ... length]
38
- row .push decoded .b [index++ ]
39
- boardValues .push row
40
-
41
- for goal in decoded .g
42
- goals .push goal
43
-
44
- for placement in [0 ... decoded .p .length ]
45
- expression = []
46
- for coord in [0 ... decoded .p [placement].length ]
47
- expression .push [(Math .floor decoded .p [placement][coord] / length), (decoded .p [placement][coord] % length)]
48
- slnPlacements .push expression
28
+ decoded = null
29
+ return false unless decoded? and decoded .b ? and decoded .g ? and
30
+ decoded .p ? and @ isValidDecode decoded
49
31
32
+ length = Math .sqrt decoded .b .length
33
+ @ decodeBoardValues decoded .b , boardValues, length
34
+ @ decodeGoals decoded .g , goals
35
+ @ decodeSolutionPlacements decoded .p , solutionPlacements, length
50
36
true
51
37
52
38
@ isValidDecode: (decoded ) ->
@@ -58,28 +44,53 @@ class ShareGameService
58
44
return false if alphabet .indexOf (char) is - 1
59
45
true
60
46
61
- @ checkSolutionPlacements: (board , solutionPlacements ) ->
62
- @tempBoard = {}
63
- @tempBoard .boardValues = []
64
- for row, i in board .initialValues
65
- @tempBoard .boardValues .push []
66
- for col in row
67
- @tempBoard .boardValues [i].push col
47
+ @ decodeBoardValues: (copy , boardValues , length ) ->
48
+ index = 0
49
+ for i in [0 ... length]
50
+ row = []
51
+ for j in [0 ... length]
52
+ row .push copy[index++ ]
53
+ boardValues .push row
54
+
55
+ @ decodeGoals: (copy , goals ) ->
56
+ goals .push goal for goal in copy
57
+
58
+ @ decodeSolutionPlacements: (copy , solutionPlacements , length ) ->
59
+ for list in [0 ... copy .length ]
60
+ expression = []
61
+ for coord in [0 ... copy[list].length ]
62
+ expression .push [(Math .floor copy[list][coord] / length),
63
+ (copy[list][coord] % length)]
64
+ solutionPlacements .push expression
65
+
66
+ @ checkSolutionPlacements: (board , solutionPlacements , SolutionService ) ->
67
+ @ initializeTempBoard board
68
68
@solutionService = new SolutionService @tempBoard , board .goals
69
+
69
70
for expression in solutionPlacements
70
71
clickedCells = []
71
72
for index in [0 ... expression .length ]
72
73
cell = expression[index]
73
74
clickedCells .push {row : cell[0 ], col : cell[1 ]}
74
75
@solutionService .initialize clickedCells
76
+
75
77
for cell in clickedCells
76
78
@tempBoard .boardValues [cell .row ][cell .col ] = ' '
77
79
@ pushDownTempBoard ()
78
80
79
81
unless @solutionService .isSolution ()
80
82
return false
83
+
81
84
true
82
85
86
+ @ initializeTempBoard: (board ) ->
87
+ @tempBoard = {}
88
+ @tempBoard .boardValues = []
89
+ for row, i in board .initialValues
90
+ @tempBoard .boardValues .push []
91
+ for col in row
92
+ @tempBoard .boardValues [i].push col
93
+
83
94
@ pushDownTempBoard: ->
84
95
for row in [@tempBoard .boardValues .length - 1 .. 1 ]
85
96
for col in [@tempBoard .boardValues .length - 1 .. 0 ]
0 commit comments