-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.elm
196 lines (145 loc) · 4.39 KB
/
Game.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
module Main exposing (..)
import Html
import Html exposing (Html, div, text, node)
import Html.Attributes exposing (style, id, class, rel, href)
import Window
import Task
import Time
import Process
import Timer
import Board
main =
Html.program
{ init = init -----values----
, update = update
, view = view
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ board : Board.Model
, windowSize : Window.Size
, timer : Timer.Model
, difficulty : Board.DifficultyLevel
}
init : ( Model, Cmd Msg )
init =
let
difficulty =
1
size =
{ width = 800, height = 800 }
( newBoard, boardCmd ) =
Board.init difficulty
( timer, timerCmd ) =
Timer.init
model =
{ board = newBoard
, windowSize = size
, timer = timer
, difficulty = difficulty
}
cmds =
Cmd.batch
[ (Cmd.map UpdateBoard boardCmd)
, (Cmd.map UpdateTimer timerCmd)
, getWindowSize
]
in
( model, cmds )
getWindowSize : Cmd Msg
getWindowSize =
Task.perform NewWindowSize Window.size
newLevelCmd : Board.DifficultyLevel -> Cmd Msg
newLevelCmd d =
Process.sleep (1 * Time.second)
|> Task.perform (\_ -> NewLevel d)
-- Update
type Msg
= UpdateBoard Board.Msg
| UpdateTimer Timer.Msg
| NewLevel Board.DifficultyLevel
| NoOp
| NewWindowSize Window.Size
update : Msg -> Model -> ( Model, Cmd Msg )
update message ({ board, difficulty } as model) =
case message of
NewWindowSize newWindowSize ->
{ model | windowSize = newWindowSize } ! []
UpdateBoard boardMessage ->
let
( boardModel, cmd ) =
Board.update boardMessage model.board
cmd2 =
if Board.isWon boardModel.board then
newLevelCmd (difficulty + 1)
else
Cmd.none
in
{ model | board = boardModel } ! [ Cmd.map UpdateBoard cmd, cmd2 ]
UpdateTimer message ->
let
( newTimer, cmd ) =
Timer.update message model.timer
in
{ model | timer = newTimer } ! [ Cmd.map UpdateTimer cmd ]
NewLevel difficulty ->
let
( newBoard, boardCmd ) =
Board.init difficulty
( timer, timerCmd ) =
Timer.init
in
{ model | board = newBoard, difficulty = difficulty, timer = timer }
! [ Cmd.map UpdateBoard boardCmd, Cmd.map UpdateTimer timerCmd ]
NoOp ->
model ! []
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions { timer } =
let
timerSubscription =
timer
|> Timer.subscriptions
|> Sub.map UpdateTimer
in
Sub.batch [ Window.resizes NewWindowSize, timerSubscription ]
-- VIEW
view : Model -> Html Msg
view ({ board, windowSize, timer, difficulty } as model) =
let
infoDivHeight =
80
minSize =
(Basics.min windowSize.width windowSize.height) - infoDivHeight |> toFloat
lightCellSize =
minSize / 5
boardView =
board
|> Board.view lightCellSize
|> Html.map UpdateBoard
mainDivStyle =
style [ ( "width", (toString minSize) ++ "px" ) ]
infoDivStyle =
style [ ( "height", (toString infoDivHeight) ++ "px" ) ]
innerDiv content =
div [ class "infosection" ] content
timerView =
timer
|> Timer.view
|> Html.map UpdateTimer
infoDiv =
div [ id "info", infoDivStyle ]
[ innerDiv [ text ("Time: "), timerView ]
, innerDiv [ text ("Moves: " ++ (toString board.moves)) ]
, innerDiv [ text ("Difficulty level: " ++ (toString difficulty)) ]
]
in
div [ id "main", mainDivStyle ]
[ css "style.css"
, infoDiv
, div [ class "board" ] [ boardView ]
]
css : String -> Html a
css path =
node "link" [ rel "stylesheet", href path ] []