-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakesNladders.elm
93 lines (74 loc) · 2.85 KB
/
snakesNladders.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
import Html exposing (program, div, button, text)
import Html.Events exposing (onClick)
import Random exposing (Generator, Seed)
import Dict
main =
program
{ init =
( { seed = Nothing, position = 0, desc = " .. " }
, Random.generate Update randomNumberGenerator
)
, view = view
, update = update
, subscriptions = \model -> Sub.none
}
view model =
div []
[ button
[ onClick PlayAgain ]
[ text " -- Play --" ]
, text (toString model.position)
, text (model.desc)
]
type alias Model =
{ seed : Maybe Seed
, position : Int
, desc : String
}
type Msg
= Update Seed
| PlayAgain
randomNumberGenerator : Generator Seed
randomNumberGenerator =
Random.int Random.minInt Random.maxInt
|> Random.map (Random.initialSeed)
update msg model =
case msg of
Update seed ->
-- Preserve newly initialized Seed state.
( { model | seed = Just seed }, Cmd.none )
PlayAgain ->
let
-- In case if seed was present, new model will contain the new value and a new state for the seed.
board = Dict.fromList [(99,54), (70,55), (52,42), (25,2), (95,72), (6, 26), (11, 40), (60, 41), (46, 90)]
newModel =
model.seed
|> Maybe.map (Random.step (Random.int 1 6))
|> Maybe.map
(\( number, seed ) ->
{ model | seed = Just seed, position = number + model.position, desc = model.desc ++ " Dice Roll, Number: " ++ toString number }
)
|> Maybe.withDefault model
positionAfterSnakeLadderCheck =
if newModel.position >= 100 then
100
else
case Dict.get newModel.position board of
Just value -> value
Nothing -> newModel.position
updatedDesc =
newModel.desc ++
if positionAfterSnakeLadderCheck /= newModel.position then
" Snake/Ladder Found at number: " ++ toString newModel.position ++
" Updating Position to: " ++ toString positionAfterSnakeLadderCheck
else
" Continue .. "
checkIfWon =
if positionAfterSnakeLadderCheck >= 100 then
" WON !!"
else
updatedDesc
in
( { newModel | position = positionAfterSnakeLadderCheck, desc = checkIfWon }
, Cmd.none
)