-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
74 lines (57 loc) · 1.52 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
module Main exposing (..)
import Html.App as Html
import Common exposing (..)
import Game exposing (..)
import Board exposing (..)
import Display exposing (..)
import Ai exposing (..)
-- The first move takes a very long time to process, but it is always pos 0 or 4
cheatFirstMove : Game -> Int
cheatFirstMove game =
if validMove 0 game then
0
else
4
makeComputerMove : Game -> Game
makeComputerMove game =
let
moveLocation =
if List.isEmpty (movesFor game.board Computer) then
cheatFirstMove game
else
Ai.bestMove game.board game.turn
in
{ game | board = updateBoard moveLocation game.turn game.board }
update : Msg -> Game -> ( Game, Cmd Msg )
update action game =
case action of
NewGame ->
( newGame, Cmd.none )
NoOp ->
( game, Cmd.none )
Move id ->
if validMove id game then
( makeMove id game
|> checkForWinners
|> switchCurrentPlayer
|> makeComputerMove
|> checkForWinners
|> switchCurrentPlayer
, Cmd.none
)
else
( game, Cmd.none )
init =
( newGame
, Cmd.none
)
subscriptions : Game -> Sub Msg
subscriptions game =
Sub.none
main =
Html.program
{ init = init
, view = display
, update = update
, subscriptions = subscriptions
}