-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate.elm
54 lines (39 loc) · 1.36 KB
/
Update.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
module Update where
import Time
import List
import Random as R
import List.Extra exposing (zip)
import Signal exposing (..)
import Model exposing (..)
interval : Signal Time.Time
interval = Time.every <| Time.second * 3
rand : Signal (List (Hole, Mole))
rand = let
iToM i =
if i == 0 then moleInAHole else Mole True
f currentTime (_, seed) = let
seed' = if currentTime == 0
then R.initialSeed currentTime
else seed
gen = R.int 0 1
(ul, s') = gen `R.generate` seed'
(um, s'') = gen `R.generate` s'
(ur, s''') = gen `R.generate` s''
(ll, s'''') = gen `R.generate` s'''
(lr, s''''') = gen `R.generate` s''''
in (zip (List.map iToM [ul, um, ur, ll, lr]) [UL, UM, UR, LL, LR], s''''')
in List.map (\(a,b) -> (b,a)) <~ (fst <~ foldp f ([], R.initialSeed 0) interval)
update : Input -> Game -> Game
update input g = let
whack : Mole -> Score
whack m = if m.wackable then 400 else 0
whacked h = List.foldl (\(hole, mole) g' ->
if h == hole
then { g' | score <- g'.score + whack mole
, moles <- (hole, moleInAHole) :: g'.moles }
else { g' | moles <- (hole, mole) :: g'.moles }
) { score = g.score, moles = [] } g.moles
in case input of
Wack h -> whacked h
Random hms -> { g | moles <- hms }
EmptyInput -> g