-
Notifications
You must be signed in to change notification settings - Fork 0
/
iconic-memory-game.elm
266 lines (222 loc) · 6.88 KB
/
iconic-memory-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
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
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import List exposing (..)
import Char
import Process
import Random
import String
import Task
import Json.Decode as Json
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ grid : List (List Char)
, seekRow : Int
, cols : Int
, rows : Int
, showTime : Int
, delay : Int
, score : Int
, error : String
, letters : String
, showLetters : Bool
, indicateRow : Bool
}
init : (Model, Cmd Msg)
init =
let initialState =
{ grid = generateNewGrid (List.repeat 12 0) 4
, seekRow = 0
, cols = 4
, rows = 3
, showTime = 150
, delay = 100
, score = 0
, error = ""
, letters = ""
, showLetters = False
, indicateRow = False
} in
(initialState, getNewGrid initialState)
-- UPDATE
type Msg
= StartNextRound () | StartRound () | GetNewGrid (Int, List Int) | PromptForAnswer () | FlashGrid () | TickFail () |
CheckAnswer String | SpeedUp | SpeedDown | DelayUp | DelayDown | ColsUp | ColsDown | RowsUp | RowsDown |
SkipOnEnter Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
StartNextRound _ ->
startRound model
StartRound _ ->
(model, getNewGrid model)
GetNewGrid random ->
({ model | showLetters = True, seekRow = (fst random), grid = generateNewGrid (snd random) model.cols }, flashGrid model)
FlashGrid _ ->
({ model | showLetters = False }, promptForAnswer model)
PromptForAnswer _ ->
({ model | indicateRow = True }, Cmd.none)
TickFail _ ->
(model, Cmd.none)
CheckAnswer letters ->
if String.length letters == model.cols then
let
thisRow = List.head <| List.drop model.seekRow model.grid
in
case thisRow of
Nothing ->
({model | error = "Grid is missing a row!"}, Cmd.none)
Just row ->
let
numCorrect = sum <|
map2 (\a b -> if a==b then 1 else 0)
(String.toList <| String.toUpper letters)
row
rowScore = numCorrect * 100 // model.cols
model' = {model | score = model.score + rowScore, letters = "", showLetters = True}
in
(model', startNextRound model')
else
({model | letters = letters}, Cmd.none)
SpeedDown ->
({ model | showTime = Basics.max 100 (model.showTime - 25) }, Cmd.none)
SpeedUp ->
({ model | showTime = Basics.min 1000 (model.showTime + 25) }, Cmd.none)
DelayDown ->
({ model | delay = Basics.max 25 (model.delay - 25) }, Cmd.none)
DelayUp ->
({ model | delay = Basics.min 1000 (model.delay + 25) }, Cmd.none)
ColsDown ->
({ model | cols = Basics.max 3 (model.cols - 1) }, Cmd.none)
ColsUp ->
({ model | cols = Basics.min 6 (model.cols + 1) }, Cmd.none)
RowsDown ->
({ model | rows = Basics.max 3 (model.rows - 1) }, Cmd.none)
RowsUp ->
({ model | rows = Basics.min 6 (model.rows + 1) }, Cmd.none)
SkipOnEnter keycode ->
case keycode of
13 -> startRound model
_ -> (model, Cmd.none)
generateNewGrid : List Int -> Int -> List (List Char)
generateNewGrid grid cols =
collate cols <| List.map (\i -> Char.fromCode (65 + i)) grid
collate : Int -> List a -> List (List a)
collate n xs =
case (List.take n xs) of
[] -> []
x -> x :: collate n (List.drop n xs)
startRound model =
({ model | letters = "", showLetters = False, indicateRow = False }, waitToStartRound model)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- TASKS
getNewGrid : Model -> Cmd Msg
getNewGrid model =
Random.generate GetNewGrid <| Random.pair (Random.int 0 (model.rows - 1)) (Random.list (model.cols * model.rows) (Random.int 0 25))
flashGrid : Model -> Cmd Msg
flashGrid model =
Task.perform TickFail FlashGrid <| Process.sleep (toFloat model.showTime)
promptForAnswer : Model -> Cmd Msg
promptForAnswer model =
Task.perform TickFail PromptForAnswer <| Process.sleep (toFloat model.delay)
startNextRound : Model -> Cmd Msg
startNextRound model =
Task.perform TickFail StartNextRound <| Process.sleep (toFloat 1000)
waitToStartRound : Model -> Cmd Msg
waitToStartRound model =
Task.perform TickFail StartRound <| Process.sleep (toFloat 500)
-- VIEW
view : Model -> Html Msg
view model =
div
[style
[ ("width", "20%")
, ("height", "75%")
, ("text-align", "center")
, ("margin", "auto")
, ("margin-top", "10%")
]
]
[ showGrid model
, br [] []
, input [ placeholder "Letters, or enter to skip", value model.letters, onInput CheckAnswer, onKeyDown SkipOnEnter ] []
, br [] []
, text <| String.append "Score: " (toString model.score)
, br [] []
, text "Show time (ms):"
, button [ onClick SpeedDown ] [ text "-" ]
, text (toString model.showTime)
, button [ onClick SpeedUp ] [ text "+" ]
, br [] []
, text "Prompt delay (ms):"
, button [ onClick DelayDown ] [ text "-" ]
, text (toString model.delay)
, button [ onClick DelayUp ] [ text "+" ]
, br [] []
, text "Columns:"
, button [ onClick ColsDown ] [ text "-" ]
, text (toString model.cols)
, button [ onClick ColsUp ] [ text "+" ]
, br [] []
, text "Rows:"
, button [ onClick RowsDown ] [ text "-" ]
, text (toString model.rows)
, button [ onClick RowsUp ] [ text "+" ]
, br [] []
, a [href "http://github.com/DestyNova/iconic-memory-game"] [text "Source"]
]
showGrid : Model -> Html Msg
showGrid model =
let
rowStyle =
[ style
[ ("justify-content", "space-around")
, ("align-items", "stretch")
, ("display", "flex")
, ("font-size", "24pt")
]
]
tileStyle =
[ style
[ ("color", "navy")
, ("backgroundColor", "cornsilk")
, ("flex", "1 1 50%")
, ("height", "50%")
]
]
in
div [] <|
List.concatMap
(\indexedRow -> [div rowStyle <|
List.concat
[ [ getMarker (model.indicateRow && model.seekRow == fst indexedRow) ">" ]
, List.concatMap (\letter ->
[div tileStyle [text <| if model.showLetters then String.fromList [letter] else "_"]])
(snd indexedRow)
, [ getMarker (model.indicateRow && model.seekRow == fst indexedRow) "<" ]]
])
(List.indexedMap (,) model.grid)
getMarker showMarker symbol =
let
markerStyle =
[ style
[ ("color", "red")
, ("opacity", if showMarker then "1" else "0.1")
]
]
in
div markerStyle [ text symbol ]
onKeyDown : (Int -> msg) -> Attribute msg
onKeyDown tagger =
on "keydown" (Json.map tagger keyCode)