-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
267 lines (219 loc) · 7.86 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
module Main exposing (main)
import Animation as A
import Basics exposing (toFloat)
import Browser exposing (Document)
import Color
import Dict.Any as AD
import Element as El
import Element.Background as ElBg
import Element.Border as ElBorder
import Element.Font as ElFont
import Game exposing (Game, Line(..), Way(..), Win(..), moves, play, player, wins)
import GameDialog exposing (GameDialogModel)
import Html exposing (Html, button, div, text)
import Html.Attributes
import Html.Events exposing (onClick)
import List exposing (map, range, take)
import Model exposing (Model)
import Msg exposing (..)
import Platform
import Player
import Process
import Random exposing (Seed)
import Style exposing (toElmUiColor)
import Svg exposing (LineDisplayInfo, gameToSvg)
import Svg.Animation exposing (..)
import Svg.Animation.Types exposing (LineAnimations)
import Task
main : Program () Model Msg
main =
Browser.application
{ init = \_ _ _ -> ( init, Cmd.none )
, update = update
, view = view
, subscriptions = subscriptions
, onUrlRequest = \_ -> NoOp
, onUrlChange = \_ -> NoOp
}
init : Model
init =
{ game = Game.initGame { width = 3, height = 3 }
, computerIsThinking = False
, styles = Svg.Animation.initialLineAnimations
, gameDialog = GameDialog.initGameDialogModel
}
subscriptions : Model -> Sub Msg
subscriptions =
Svg.Animation.subscriptions
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
MakeMove line ->
let
updatedGame =
play line model.game
computerTurn =
playerIsComputer { model | game = updatedGame }
in
( { model
| game = updatedGame
, computerIsThinking = computerTurn
, styles = Svg.Animation.startPlayedAnimationForLine line model.styles
}
, if computerTurn then
delay 200 MakeComputerMoveInit
else
Cmd.none
)
MakeComputerMoveInit ->
( model, computerMoveMsg )
MakeComputerMove seed ->
case Player.makeGreedyMove seed model.game of
Just line ->
let
( newModel, cmd ) =
update (MakeMove line) model
in
( newModel, cmd )
Nothing ->
( { model | computerIsThinking = False }, Cmd.none )
Animate time ->
( { model
| styles = Svg.Animation.animate time model.styles
}
, Cmd.none
)
GameDialogMessage m ->
let
( subModel, subCmd ) =
GameDialog.update m model.gameDialog
modelWithUpdatedSubmodel =
{ model | gameDialog = subModel }
( finalModel, additionalCmd ) =
checkForGameStart m modelWithUpdatedSubmodel
in
( finalModel
, Cmd.batch
[ Cmd.map GameDialogMessage subCmd
, additionalCmd
]
)
checkForGameStart : GameDialog.Msg -> Model -> ( Model, Cmd Msg )
checkForGameStart msg model =
case msg of
GameDialog.Start ->
let
wh =
case model.gameDialog.sizeChoice of
GameDialog.ThreeByThree ->
3
GameDialog.FourByFour ->
4
GameDialog.FiveByFive ->
5
newModel =
{ model | game = Game.initGame { width = wh, height = wh } }
in
( newModel
, if playerIsComputer newModel then
delay 200 MakeComputerMoveInit
else
Cmd.none
)
_ ->
( model, Cmd.none )
computerMoveMsg : Cmd Msg
computerMoveMsg =
Random.generate (\v -> MakeComputerMove (Random.initialSeed v)) (Random.int Random.minInt Random.maxInt)
delay : Float -> msg -> Cmd msg
delay time msg =
Process.sleep time
|> Task.perform (\_ -> msg)
view : Model -> Document Msg
view model =
let
currentPlayerText =
if model.gameDialog.isShown then
""
else if Game.isFinished model.game then
case Game.winner model.game of
Just PlayerA ->
"Game finished, winner is A"
Just PlayerB ->
"Game finished, winner is B"
Nothing ->
"Game finished, it's a draw"
else
(case model.game.player of
PlayerA ->
"A"
_ ->
"B"
)
++ " to play"
currentScoreText =
if model.gameDialog.isShown then
""
else
(Game.score model.game PlayerA |> String.fromInt)
++ " - "
++ (Game.score model.game PlayerB |> String.fromInt)
computerThinkingText =
if model.computerIsThinking then
"Thinking ..."
else
""
movesWithStyle =
Svg.Animation.calcMovesWithStyle (moves model.game) model.styles
in
{ title = "Dots And Boxes"
, body =
[ El.layout [ El.height El.fill, El.width El.fill ]
(El.column [ El.height <| El.fill, El.width <| El.fill, El.spaceEvenly ]
[ El.el
Style.headerStyles.headerStyle
(El.row
[ El.height El.fill, El.width El.fill ]
[ El.el Style.headerStyles.titleTextStyle (El.text Style.name)
, El.el Style.headerStyles.titleTextStyle (El.newTabLink []
{ url = "https://github.com/mcapodici/elm-dotsandboxes"
, label = El.row [] [
El.image [ El.height (El.px 36), El.width (El.px 36) ] { description="github", src="octocat.png"},
El.text "Github"]
})
, if currentScoreText /= "" || currentPlayerText /= "" then El.el Style.headerStyles.seperatorStyle El.none else El.none
, showIfText (El.text >> El.el Style.headerStyles.currentPlayerTextStyle) currentScoreText
, showIfText (El.text >> El.el Style.headerStyles.currentPlayerTextStyle) currentPlayerText
]
)
, if model.gameDialog.isShown then
El.column
Style.dialogStyles.dialogContainerStyle
[ El.map GameDialogMessage <| GameDialog.view model.gameDialog ]
else
El.el
[ El.width <| El.minimum 200 (El.maximum 600 El.fill), El.centerX ]
(El.html <| gameToSvg (not <| playerIsComputer model) model.game movesWithStyle)
, El.el
[ El.height <| El.px 100, El.height El.fill, El.padding 5 ]
El.none
]
)
]
}
showIfText : (String -> El.Element Msg) -> String -> El.Element Msg
showIfText f s =
if s == "" then
El.none
else
f s
playerIsComputer : Model -> Bool
playerIsComputer model =
let
player =
model.game.player
in
(player == PlayerA && model.gameDialog.playerOneIsComputer)
|| (player == PlayerB && model.gameDialog.playerTwoIsComputer)