-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
468 lines (367 loc) · 11 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Array
import Tuple
main =
Html.beginnerProgram
{ model = initModel
, view = view
, update = update
}
-- MODEL
type alias Model =
{ appState : AppState
, turn : Player
, board : List (List Box)
}
type AppState
= Draw
| NewGame
| Winner Player
type Player
= User
| CPU
type alias Box =
{ id : Int
, player : Maybe Player
}
initModel : Model
initModel =
Model NewGame
User
[ [ Box 1 Nothing, Box 2 Nothing, Box 3 Nothing ]
, [ Box 4 Nothing, Box 5 Nothing, Box 6 Nothing ]
, [ Box 7 Nothing, Box 8 Nothing, Box 9 Nothing ]
]
cpuFirstInitModel : Model
cpuFirstInitModel =
Model NewGame
User
[ [ Box 1 Nothing, Box 2 Nothing, Box 3 Nothing ]
, [ Box 4 Nothing, Box 5 (Just CPU), Box 6 Nothing ]
, [ Box 7 Nothing, Box 8 Nothing, Box 9 Nothing ]
]
-- UPDATE
type Msg
= Click Int
| RestartWithCpuFirst
| RestartWithUserFirst
update : Msg -> Model -> Model
update msg model =
let
newModel =
case msg of
Click id ->
if model.turn == User then
boxClicked model id
else
model
RestartWithCpuFirst ->
cpuFirstInitModel
RestartWithUserFirst ->
initModel
in
newModel |> updateAppState |> cpuTurn |> updateAppState
boxClicked : Model -> Int -> Model
boxClicked model id =
let
clickedBox =
List.head <| List.filter (\box -> box.id == id) (List.concat model.board)
gameIsNotOver =
model.appState == NewGame
in
case clickedBox of
Just box ->
if box.player == Nothing && gameIsNotOver then
{ model
| turn = rotateTurn model.turn
, board = flipBoxById model.board id model.turn
}
else
model
Nothing ->
-- need error handling here???
model
flipBoxById : List (List Box) -> Int -> Player -> List (List Box)
flipBoxById currentBoard boxId player =
let
updateBox box =
if box.id == boxId then
Box box.id (Just player)
else
box
in
currentBoard
|> List.map (List.map updateBox)
rotateTurn : Player -> Player
rotateTurn currentPlayer =
case currentPlayer of
User ->
CPU
CPU ->
User
updateAppState : Model -> Model
updateAppState model =
let
board =
model.board
gameWinChecks =
[ checkRowWin, checkColumnWin, checkDiagonalWin, checkDraw ]
gameResults =
List.map ((|>) board) gameWinChecks
newAppState =
if List.member (Just (Winner User)) gameResults then
Winner User
else if List.member (Just (Winner CPU)) gameResults then
Winner CPU
else if List.member (Just Draw) gameResults then
Draw
else
NewGame
in
{ model | appState = newAppState }
boxValue : Box -> Int
boxValue box =
case box.player of
Just User ->
1
Just CPU ->
-1
Nothing ->
0
checkDiagonalWin : List (List Box) -> Maybe AppState
checkDiagonalWin board =
let
getElementAt : ( Int, Int ) -> Maybe Box
getElementAt ( row, col ) =
board
|> List.concat
|> List.filter (\box -> box.id == row * 3 + (col + 1))
|> List.head
diagonal1 =
[ ( 0, 0 ), ( 1, 1 ), ( 2, 2 ) ]
diagonal2 =
[ ( 0, 2 ), ( 1, 1 ), ( 2, 0 ) ]
diagonal1Boxes : List (Maybe Box)
diagonal1Boxes =
diagonal1
|> List.map ((<|) getElementAt)
diagonal2Boxes : List (Maybe Box)
diagonal2Boxes =
diagonal2
|> List.map ((<|) getElementAt)
diagonalCountList : List Int
diagonalCountList =
[ diagonal1Boxes, diagonal2Boxes ]
|> List.map
(List.map (Maybe.map boxValue))
|> List.map
(List.map (Maybe.withDefault 0))
|> List.map List.sum
in
if List.member 3 diagonalCountList then
Just (Winner User)
else if List.member -3 diagonalCountList then
Just (Winner CPU)
else
Nothing
checkRowWin : List (List Box) -> Maybe AppState
checkRowWin board =
let
rowCountList : List Int
rowCountList =
board
|> List.map
(List.map boxValue)
|> List.map List.sum
in
if List.member 3 rowCountList then
Just (Winner User)
else if List.member -3 rowCountList then
Just (Winner CPU)
else
Nothing
checkColumnWin : List (List Box) -> Maybe AppState
checkColumnWin board =
let
getElementAt : List a -> Int -> Maybe a
getElementAt list idx =
List.head (List.drop idx list)
getColumn : Int -> List (Maybe Box)
getColumn colIdx =
List.map ((flip getElementAt) colIdx) board
columnCountList : List Int
columnCountList =
List.map (getColumn) [ 0, 1, 2 ]
|> List.map
(List.sum << (List.map (Maybe.map boxValue >> Maybe.withDefault 0)))
in
if List.member 3 columnCountList then
Just (Winner User)
else if List.member -3 columnCountList then
Just (Winner CPU)
else
Nothing
checkDraw : List (List Box) -> Maybe AppState
checkDraw board =
let
usedBoxCount : Int
usedBoxCount =
board
|> List.concat
|> List.filter (\box -> box.player /= Nothing)
|> List.length
in
case usedBoxCount of
9 ->
Just Draw
_ ->
Nothing
cpuTurn : Model -> Model
cpuTurn model =
if model.appState == NewGame && model.turn == CPU then
boxClicked model (minimax model)
else
model
minimax : Model -> Int
minimax model =
let
possibleMoves : List Int
possibleMoves =
model.board
|> List.concat
|> List.filter (\box -> box.player == Nothing)
|> List.map .id
getModelFromNextMove : Int -> Model
getModelFromNextMove boxId =
boxClicked model boxId |> updateAppState
resolvedMoves : List ( Int, AppState )
resolvedMoves =
possibleMoves
|> List.map (\idx -> ( idx, resolveTree (getModelFromNextMove idx) ))
winningMoves : List ( Int, AppState )
winningMoves =
resolvedMoves
|> List.filter (\( _, appState ) -> appState == Winner model.turn)
drawMoves : List ( Int, AppState )
drawMoves =
resolvedMoves
|> List.filter (\( _, appState ) -> appState == Draw)
_ =
Debug.log "resolvedMoves" resolvedMoves
in
if List.length winningMoves /= 0 then
List.head winningMoves |> Maybe.withDefault (( 1, NewGame )) |> Tuple.first
else if List.length drawMoves /= 0 then
List.head drawMoves |> Maybe.withDefault (( 1, NewGame )) |> Tuple.first
else
Debug.crash "unexpected state" -1
resolveTree : Model -> AppState
resolveTree model =
let
currentPlayer : Player
currentPlayer =
model.turn
otherPlayer : Player
otherPlayer =
if currentPlayer == User then
CPU
else
User
possibleMoves : List Int
possibleMoves =
model.board
|> List.concat
|> List.filter (\box -> box.player == Nothing)
|> List.map .id
possibleModels : List Model
possibleModels =
possibleMoves
|> List.map (boxClicked model)
|> List.map updateAppState
branchResults : List AppState
branchResults =
possibleModels
|> List.map
(\model ->
if model.appState == NewGame then
resolveTree model
else
model.appState
)
in
if List.member (Winner currentPlayer) branchResults then
Winner currentPlayer
else if List.member Draw branchResults then
Draw
else
Winner otherPlayer
-- VIEW
view : Model -> Html Msg
view model =
div [ class "wrapper" ]
[ renderHeader
, renderGameControls
, div [ class "board" ] (renderBoard model)
, gameStateDiv model
, renderFooter
]
renderHeader : Html Msg
renderHeader =
h1 [ class "header" ]
[ span [ class "header--tic-tac-toe" ] [ text "Tic Tac Toe " ]
, span [ class "header--pipe" ] [ text "|" ]
, span [ class "header--gt" ] [ text ">" ]
, span [ class "header--elm" ] [ text " Elm" ]
]
gameStateDiv : Model -> Html Msg
gameStateDiv model =
case model.appState of
Winner p ->
div [ class "result" ] [ text <| (playerToText (Just p)) ++ " wins!" ]
Draw ->
div [ class "result" ] [ text "Draw!" ]
_ ->
text ""
renderBoard : Model -> List (Html Msg)
renderBoard model =
model.board
|> List.concat
|> List.map boardBox
boardBox : Box -> Html Msg
boardBox box =
let
classname =
case box.player of
Just User ->
"box -user"
Just CPU ->
"box -cpu"
_ ->
"box"
in
div [ class classname, onClick (Click box.id) ] [ text (playerToText box.player) ]
renderGameControls : Html Msg
renderGameControls =
div [ class "controls" ]
[ button [ class "btn restart--cpu-first", onClick RestartWithCpuFirst ] [ text "Restart AI first" ]
, button [ class "btn restart--user-first", onClick RestartWithUserFirst ] [ text "Restart me first" ]
]
renderFooter : Html Msg
renderFooter =
div [ class "footer" ]
[ text "A "
, a [ class "footer--fcc", target "_blank", href "https://www.freecodecamp.com/" ] [ text "freeCodeCamp" ]
, text " project by Ilya Murashov"
]
playerToText : Maybe Player -> String
playerToText player =
case player of
Just User ->
"X"
Just CPU ->
"O"
Nothing ->
""