-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.elm
419 lines (324 loc) · 9.86 KB
/
View.elm
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
module View exposing (view)
import MaterialModel exposing (MaterialModel)
import Model exposing (Model, Ball, Board, GameState(..), PinId(..), Pin(..), Ball(..), Rack)
import Html exposing (Html, text)
import Html.App
import Html.Attributes
import MaterialMsg exposing (MaterialMsg(Mdl, U))
import Msg exposing (Msg(..))
import Material.Button as Button
import Material.Grid as Grid exposing (Device(..))
import Svg exposing (Svg, svg, rect, path, circle, Attribute, ellipse, g)
import Svg.Attributes exposing (..)
import Svg.Events exposing (onClick)
view : MaterialModel -> Html MaterialMsg
view { mdl, model } =
Html.div []
[ Button.render Mdl
[ 0 ]
mdl
[ Button.raised
, Button.ripple
, Button.onClick (U NewGame)
]
[ text "New Game" ]
, Grid.grid []
[ Grid.cell [ Grid.size All 5 ]
[ renderRack model.rack
]
, Grid.cell [ Grid.size All 6 ]
[ Html.div [ Html.Attributes.style [ ( "width", boardWidthString ++ "px" ), ( "display", "flex" ), ( "justify-content", "center" ), ( "font-size", (boardWidth / 32 |> toString) ++ "px" ) ] ]
[ model
|> gameStateToString
|> Html.text
]
, svg
[ width boardWidthString
, height boardHeightString
, viewBox ("0 0 " ++ boardWidthString ++ " " ++ boardHeightString)
]
[ Svg.defs []
[ Svg.clipPath [ id "ballClip", clipPathUnits "objectBoundingBox" ]
[ rect
[ x "0"
, y "0.125"
, width "1"
, height "0.75"
]
[]
]
]
, renderBoard model.board
]
]
]
|> Html.App.map U
]
gameStateToString : Model -> String
gameStateToString model =
let
cpuScore =
toString (Model.currentScore Red model.board)
userScore =
toString (Model.currentScore White model.board)
in
case model.gameState of
Win ->
"You won " ++ userScore ++ " to " ++ cpuScore
Loss ->
"You lost " ++ userScore ++ " to " ++ cpuScore
Tie ->
"The game ended tied at " ++ userScore ++ " to " ++ cpuScore
_ ->
"CPU: "
++ cpuScore
++ " - You : "
++ userScore
renderBoard : Board -> Svg Msg
renderBoard board =
stand
:: renderPins board
|> g []
standX =
-boardWidth / 8
standY =
boardHeight / 3
standW =
boardWidth
standH =
boardHeight
stand =
renderStand standX standY standW standH
renderPins : Board -> List (Svg Msg)
renderPins board =
List.map (renderPin board) Model.pinIdPossibilities
halfPinWidth =
12.5
halfPinWidthString =
toString halfPinWidth
pinHeight =
100
pinHeightString =
toString pinHeight
renderPin : Board -> PinId -> Svg Msg
renderPin board pinId =
let
( baseX, baseY ) =
getPinPosition pinId standX standY standW standH
topLeftCornerX =
baseX - halfPinWidth
topLeftCornerY =
-- + 3 to cover the corner of the lines
baseY - pinHeight + 3
((Pin bottom middle top) as pin) =
Model.getPin pinId board
pinExtraAttributes =
if Model.pinHasRoom pin then
[ onClick (Place pinId) ]
else
[]
in
[ rect (pinExtraAttributes ++ [ fill "#888888", x (toString topLeftCornerX), y (toString topLeftCornerY), width "25", height pinHeightString ]) []
, Svg.circle
(pinExtraAttributes
++ [ fill "#888888"
, cx <| toString (baseX)
, cy (toString topLeftCornerY)
, r halfPinWidthString
]
)
[]
, renderBall bottom baseX (baseY - pinHeight / 6)
, renderBall middle baseX (baseY - pinHeight / 2)
, renderBall top baseX (baseY - pinHeight * 5 / 6)
]
|> g []
renderStand x y w h =
Svg.path
[ fill "#111111"
, d
<| "M"
++ toString (w * 5 / 12 + x)
++ " "
++ toString (h * 5 / 48 + y)
++ (" Q "
++ toString (w * 5 / 6 + x)
++ " "
++ toString (0 + y)
++ " "
++ toString (w * 25 / 24 + x)
++ " "
++ toString (h * 5 / 48 + y)
)
++ (" T "
++ toString (w * 5 / 6 + x)
++ " "
++ toString (h * 5 / 16 + y)
)
++ (" T "
++ toString (w * 5 / 24 + x)
++ " "
++ toString (h * 5 / 16 + y)
)
++ (" T "
++ toString (w * 5 / 12 + x)
++ " "
++ toString (h * 5 / 48 + y)
)
]
[]
:: renderLines x y w h
|> g []
renderLines : Float -> Float -> Float -> Float -> List (Svg Msg)
renderLines x y w h =
let
( rightTopX, rightTopY ) =
getPinPosition Zero x y w h
( rightMiddleX, rightMiddleY ) =
getPinPosition One x y w h
( rightBottomX, rightBottomY ) =
getPinPosition Two x y w h
( leftTopX, leftTopY ) =
getPinPosition Five x y w h
( leftMiddleX, leftMiddleY ) =
getPinPosition Six x y w h
( leftBottomX, leftBottomY ) =
getPinPosition Seven x y w h
in
[ Svg.path
[ fill "transparent"
, stroke "#888888"
, strokeWidth "4"
, d
<| "M "
++ toString leftTopX
++ " "
++ toString leftTopY
++ (" L "
++ toString rightMiddleX
++ " "
++ toString rightMiddleY
)
++ (" L "
++ toString leftBottomX
++ " "
++ toString leftBottomY
)
++ "Z"
]
[]
, Svg.path
[ fill "transparent"
, stroke "#888888"
, strokeWidth "4"
, d
<| "M "
++ toString rightTopX
++ " "
++ toString rightTopY
++ (" L "
++ toString leftMiddleX
++ " "
++ toString leftMiddleY
)
++ (" L "
++ toString rightBottomX
++ " "
++ toString rightBottomY
)
++ "Z"
]
[]
]
getPinPosition : PinId -> Float -> Float -> Float -> Float -> ( Float, Float )
getPinPosition pinId x y w h =
case pinId of
Zero ->
( w * 39 / 48 + x, h * 6 / 96 + y )
One ->
( w * 22 / 24 + x, h * 13 / 96 + y )
Two ->
( w * 49 / 48 + x, h * 20 / 96 + y )
Three ->
( w * 7 / 12 + x, h / 6 + y )
Four ->
( w * 11 / 16 + x, h * 23 / 96 + y )
Five ->
( w * 6 / 24 + x, h * 19 / 96 + y )
Six ->
( w * 17 / 48 + x, h * 26 / 96 + y )
Seven ->
( w * 11 / 24 + x, h * 33 / 96 + y )
boardWidth =
720
boardWidthString =
toString boardWidth
boardHeight =
720
boardHeightString =
toString boardHeight
centerX =
boardWidth / 2
centerY =
boardHeight / 2
centerXString =
toString centerX
centerYString =
toString centerY
rackWidth =
250
rackHeight =
600
rackWidthString =
toString rackWidth
rackHeightString =
toString rackHeight
renderRack : Rack -> Svg Msg
renderRack rack =
svg
[ width rackWidthString
, height rackHeightString
, viewBox ("0 0 " ++ rackWidthString ++ " " ++ rackHeightString)
]
<| [ Svg.rect
[ x "0"
, y "0"
, width rackWidthString
, height rackHeightString
, stroke "black"
, strokeWidth "2"
, fillOpacity "0"
]
[]
]
++ renderBalls rack
renderBalls : Rack -> List (Svg Msg)
renderBalls rack =
([0..(toFloat rack.red - 1)]
|> List.map
(\index ->
renderBall Red (rackWidth / 3) (rackHeight * (12 - index - 0.5) / 12)
)
)
++ ([0..(toFloat rack.white - 1)]
|> List.map
(\index ->
renderBall White (rackWidth * 2 / 3) (rackHeight * (12 - index - 0.5) / 12)
)
)
ballRadius =
pinHeight / 5
ballRadiusString =
toString ballRadius
nullSvg =
Svg.text ""
renderBall ball x y =
case ball of
NoBall ->
nullSvg
Red ->
renderActualBall [ fill "#FF4136" ] x y
White ->
renderActualBall [ fill "#EEEEEE" ] x y
renderActualBall extraAttributes xPos yPos =
circle ([ clipPath "url(#ballClip)", cx (toString xPos), cy (toString yPos), r ballRadiusString ] ++ extraAttributes) []