-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
executable file
·195 lines (170 loc) · 5.53 KB
/
Main.hs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Data.Map as DM (lookup, fromList)
import qualified Data.Set as DS (null, unions)
import Data.Maybe
import Miso
import Miso.String (ms, MisoString, append)
import qualified Miso.Svg as MSV ( g_ , height_ , rect_ , svg_ , transform_ , version_ , width_ , x_ , y_, style_)
import Piece
import Init
import Solve (stepsHylo, Progress(..))
data Rate
= Fast
| Slow
| Step
deriving (Eq)
data Action
= Time Double
| SetRate Rate
| RequestStep
deriving (Eq)
data Model = Model
{ time :: Double
, steps :: [Progress Cell]
, solutions :: [Progress Cell]
, rate :: Rate
, stepRequested :: Bool
}
-- Model comparision using derived Eq runs the risk
-- of comparing long lists of steps which causes app
-- to freeze. Model comparison is only used to
-- prevent unnecessary view refreshes so, worst case,
-- this may result in more DOM diffing.
instance Eq Model where
_ == _ = False
main :: IO ()
main = do
t <- now
let pieces =
[ ['I', 'P', 'P', 'Y', 'Y', 'Y', 'Y', 'V', 'V', 'V']
, ['I', 'P', 'P', 'X', 'Y', 'L', 'L', 'L', 'L', 'V']
, ['I', 'P', 'X', 'X', 'X', 'F', 'Z', 'Z', 'L', 'V']
, ['I', 'T', 'W', 'X', 'F', 'F', 'F', 'Z', 'U', 'U']
, ['I', 'T', 'W', 'W', 'N', 'N', 'F', 'Z', 'Z', 'U']
, ['T', 'T', 'T', 'W', 'W', 'N', 'N', 'N', 'U', 'U']
]
board = [(row, col) | row <- [0 .. 11], col <- [0 .. 4]]
zeroProgress :: Progress Cell
zeroProgress = initialProgress board pieces
allSteps = Solve.stepsHylo zeroProgress
initialModel =
Model
{ time = 0
, steps = allSteps
, solutions = []
, rate = Step
, stepRequested = False
}
startApp
App
{ model = initialModel
, initialAction = Time t
, update = updateModel
, view = viewModel
, events = defaultEvents
, mountPoint = Nothing
, subs = []
}
updateModel :: Action -> Model -> Effect Action Model
updateModel (SetRate newRate) model = Effect (model {rate = newRate}) []
updateModel RequestStep model = Effect (model {stepRequested = True}) []
updateModel (Time nTime) model@Model {..} = Effect newModel [Time <$> now]
where
delta = nTime - time
(newSteps, newSolutions, newTime) =
if ((rate == Slow) && (delta < 400.0))
|| ((rate == Step) && not stepRequested)
then (steps, solutions, time)
else (nSteps, nSolutions, nTime)
where
nSteps = tail steps
currentStep = head nSteps
-- if no cells remain uncovered, we have a solution
-- so add it to the list of solutions.
nSolutions =
if DS.null $ uncovered currentStep
then currentStep : solutions
else solutions
newModel =
model
{ time = newTime
, Main.steps = newSteps
, solutions = newSolutions
, stepRequested = False
}
viewModel :: Model -> View Action
viewModel Model {..} =
div_ []
( viewControls rate
: viewProgress workCellSize (head steps)
: fmap (viewProgress solutionCellSize) (reverse solutions))
where
workCellSize = 24
solutionCellSize = (workCellSize * 2) `div` 3
viewControls :: Rate -> View Action
viewControls rate =
div_ []
([ input_ [ type_ "radio" , name_ "updateRate" , checked_ (rate == Fast) , onClick (SetRate Fast) ] [] , text "Fast"
, input_ [ type_ "radio" , name_ "updateRate" , checked_ (rate == Slow) , onClick (SetRate Slow) ] [] , text "Slow"
, input_ [ type_ "radio" , name_ "updateRate" , checked_ (rate == Step) , onClick (SetRate Step) ] [] , text "Step"
] ++ [button_ [onClick RequestStep] [text "Step"] | rate == Step]
)
viewProgress :: Int -> Progress Cell -> View Action
viewProgress cellSize (Progress used uncovered _) =
div_
[]
[ MSV.svg_
[ MSV.version_ "1.1"
, MSV.width_ (ms (w * cellSize))
, MSV.height_ (ms (h * cellSize))
]
(concatMap (showPiece cellSize) used)
]
where
cells = DS.unions (uncovered : used)
((rMin, cMin), (rMax, cMax)) = bounds $ getLocations cells
w = 1 + cMax - cMin
h = 1 + rMax - rMin
showPiece :: Int -> Piece -> [View Action]
showPiece cellSize p =
fmap (showCell cellSize (getColor p)) (getLocations p)
showCell :: Int -> MisoString -> (Int, Int) -> View Action
showCell cellSize color (row, col) =
MSV.g_
[ MSV.transform_ $ scaleTransform `append` translateTransform
]
[ MSV.rect_
[ MSV.x_ "0.05"
, MSV.y_ "0.05"
, MSV.width_ "0.9"
, MSV.height_ "0.9"
-- Couldn't get MSV.style_ to work so using Miso.style_
, Miso.style_ $ fromList [("fill", color)]
]
[]
]
where
scale = ms cellSize
scaleTransform = "scale (" `append` scale `append` ", " `append` scale `append` ") "
translateTransform = "translate (" `append` ms col `append` ", " `append` ms row `append` ") "
getColor :: Piece -> MisoString
getColor piece =
let name = getName piece
colorMap =
fromList
[ ('F', "green")
, ('I', "blue")
, ('L', "red")
, ('N', "yellow")
, ('P', "purple")
, ('T', "brown")
, ('U', "maroon")
, ('V', "cyan")
, ('W', "pink")
, ('X', "orange")
, ('Y', "navy")
, ('Z', "lime")
]
in fromMaybe "black" (DM.lookup name colorMap)