-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.elm
217 lines (166 loc) · 4.45 KB
/
main.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
module Main exposing (main)
import Array exposing (Array)
import Browser exposing (element)
import Grid exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class, href, rel, style, type_, value)
import Html.Events exposing (onClick, onInput)
import Random exposing (Generator)
import String
type alias Board =
Grid Color
type Color
= Red
| Green
| Blue
| Purple
| Yellow
type Msg
= RandomBoard
| SetBoard Board
| Advance Color
| SetX Int
| SetY Int
type alias Model =
{ board : Board
, moveCount : Int
, newSize : Point
}
main : Program () Model Msg
main =
element { init = always init, view = view, update = update, subscriptions = always Sub.none }
init : ( Model, Cmd Msg )
init =
( newModel ( 20, 20 ) Grid.empty
, randomBoard ( 20, 20 )
)
view : Model -> Html Msg
view model =
let
colorButtons color =
button [ onClick <| Advance color ] [ text <| colorName color ]
arrMap f xs =
Array.map f xs |> Array.toList
viewRow row =
div [ class "row" ] <| arrMap viewCell row
viewCell cell =
span
[ style "background-color" (colorName cell)
, class "cell"
]
[]
( x, y ) =
model.newSize
numInput msg =
String.toInt >> Maybe.withDefault 0 >> msg |> onInput
in
main_ []
[ css "style.css"
, div [ class "buttons" ] <| List.map colorButtons colors
, div [ class "grid" ] <| arrMap viewRow model.board
, text <| "Moves: " ++ String.fromInt model.moveCount
, span [ class "new-game" ]
[ text "Rows: "
, input [ type_ "number", value <| String.fromInt x, numInput SetX ] []
, text "Cols: "
, input [ type_ "number", value <| String.fromInt y, numInput SetY ] []
, button [ onClick RandomBoard ] [ text "New Game" ]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Advance color ->
( advance color model
, Cmd.none
)
SetBoard board ->
( newModel model.newSize board
, Cmd.none
)
RandomBoard ->
( model
, randomBoard model.newSize
)
SetX x ->
let
( _, y ) =
model.newSize
in
( { model | newSize = ( x, y ) }
, Cmd.none
)
SetY y ->
let
( x, _ ) =
model.newSize
in
( { model | newSize = ( x, y ) }
, Cmd.none
)
newModel : Point -> Board -> Model
newModel newSize board =
{ board = board
, moveCount = 0
, newSize = newSize
}
advance : Color -> Model -> Model
advance color model =
let
newBoard =
makeMove color model.board
in
if model.board == newBoard then
model
else
{ model
| board = newBoard
, moveCount = model.moveCount + 1
}
makeMove : Color -> Board -> Board
makeMove color board =
let
oldColor =
Maybe.withDefault color <| get2d ( 0, 0 ) board
in
if color == oldColor then
board
else
recolor oldColor color ( 0, 0 ) board
css : String -> Html a
css path =
node "link" [ rel "stylesheet", href path ] []
recolor : Color -> Color -> Point -> Board -> Board
recolor oldColor color point board =
neighbors point
|> List.filter (\p -> get2d p board == Just oldColor)
|> List.foldl (recolor oldColor color) (set2d point color board)
sample : Array a -> Generator (Maybe a)
sample arr =
let
gen =
Random.int 0 <| Array.length arr - 1
in
Random.map (\a -> Array.get a arr) gen
colors : List Color
colors =
[ Red, Green, Blue, Purple, Yellow ]
colorName : Color -> String
colorName color =
case color of
Red ->
"Red"
Green ->
"Green"
Blue ->
"Blue"
Purple ->
"Purple"
Yellow ->
"Yellow"
randomColor : Generator Color
randomColor =
Random.map (Maybe.withDefault Red) <| sample <| Array.fromList colors
randomBoard : Point -> Cmd Msg
randomBoard ( x, y ) =
Random.generate SetBoard <| randomGrid x y randomColor