-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMathSwipeController.coffee
150 lines (126 loc) · 4.94 KB
/
MathSwipeController.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$ = require 'jquery'
AdjacentCellsCalculator = require '../services/AdjacentCellsCalculator'
BoardSolvedService = require '../services/BoardSolvedService'
ClickHandler = require '../services/ClickHandler'
DFS = require '../services/DFS'
ExpressionGenerator = require '../services/ExpressionGenerator'
HashingService = require '../services/HashingService'
HowToPlay = require '../services/HowToPlay'
InputSolver = require '../services/InputSolver'
RandomizedFitLength = require '../services/RandomizedFitLength'
ResetButton = require '../services/ResetButton'
RunningSum = require '../services/RunningSum'
ShareGameService = require '../services/ShareGameService'
SolutionService = require '../services/SolutionService'
Title = require '../services/Title'
TrackingService = require '../services/TrackingService'
Board = require '../views/Board'
Cell = require '../views/Cell'
Colors = require '../views/Colors'
GoalContainer = require '../views/GoalContainer'
GeneralTests = require '../../tests/controllers/GeneralTests'
class MathSwipeController
constructor: ->
length = 3
@gameScene = @createGameScene()
@symbols = @getSymbols()
@bindNewGameButton()
HowToPlay.createHowToPlay @isMobile
if @isMobile().any()?
TrackingService.mobileView()
Title.mobileTitle()
else
TrackingService.desktopView()
@cursorToPointer()
ShareGameService.setMessage()
@initialize()
# # Uncomment the following line to perform general tests
# GeneralTests.tests @board
initialize: ->
length = 3
solutionPlacements = []
inputLengths = []
boardValues = []
inputs = []
goals = []
decoded = HashingService.decodeMap()
[boardValues, goals, solutionPlacements] = HashingService.parse decoded
if @malformedDecode boardValues, goals, solutionPlacements
inputLengths = RandomizedFitLength.generate length ** 2
[inputs, goals] = @generateInputs inputLengths
boardValues = @generateBoard inputs, length, solutionPlacements
@goalContainer = new GoalContainer goals, Colors
@board = new Board boardValues, @gameScene, goals, @symbols,
@goalContainer, @isMobile().any()?, Cell,
Colors, ClickHandler, SolutionService,
BoardSolvedService, RunningSum
ResetButton.bindClick @board, RunningSum
RunningSum.empty()
@createNewGame() unless HashingService.reloadPageWithHash(@board,
solutionPlacements, SolutionService)
isMobile: () ->
Android: () ->
return navigator.userAgent.match(/Android/i)
BlackBerry: () ->
return navigator.userAgent.match(/BlackBerry/i)
iOS: ()->
return navigator.userAgent.match(/iPhone|iPad|iPod/i)
Opera: () ->
return navigator.userAgent.match(/Opera Mini/i)
Windows: () ->
return navigator.userAgent.match(/IEMobile/i)
any: () ->
return (@Android() || @BlackBerry() || @iOS() || @Opera() || @Windows())
# -------- Front-end -------- #
bindNewGameButton: ->
$('#new-game-button').click (e) =>
TrackingService.boardEvent 'new game'
@createNewGame()
createNewGame: ->
@gameScene.clear()
@goalContainer.clearGoals()
ResetButton.unbindClick()
HashingService.emptyHash()
@initialize()
createGameScene: ->
gameDom = document.getElementById('game')
size = Math.min(Math.max($( window ).width(), 310), 500)
scene = new Two(
fullscreen: false
autostart: true
width: size
height: size
).appendTo(gameDom);
return scene
createGoalsScene: ->
goalsDom = document.getElementById('goals')
cursorToPointer: ->
$('#game').addClass('pointer')
$('#game-button-wrapper').addClass('pointer')
getSymbols: ->
scene = new Two()
svgs = $('#assets svg')
symbols = []
for svg, index in svgs
symbols.push (scene.interpret svg)
symbols[index].visible = false
symbols
# -------- Back-end -------- #
generateBoard: (inputs, length, solutionPlacements) ->
DFS.setEquationsOnGrid length, inputs, AdjacentCellsCalculator, solutionPlacements
generateInputs: (inputLengths) ->
goals = []
inputs = []
for inputSize in inputLengths
value = -1
while value < 1 or value > 300
expression = ExpressionGenerator.generate inputSize
value = InputSolver.compute expression
goals.push (InputSolver.compute expression)
inputs.push (expression.split(''))
[inputs, goals]
malformedDecode: (boardValues, goals, solutionPlacements) ->
boardValues.length < 1 or goals.length < 1 or solutionPlacements.length < 1
randExpression: (length) ->
ExpressionGenerator.generate length
module.exports = MathSwipeController