-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
293 lines (271 loc) · 7.61 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
module Battleship (main) where
-- Core
import String
import Keyboard
import Time
-- Evan
import Html
import Html.Attributes
import Html.Events
import StartApp
import Effects
-- 3rd Party
-- Battleship
import Player
import Ship
import Grid
import AI
---- MAIN ----
main = .html <|
StartApp.start
{ init = (defaultModel, Effects.none)
, view = view
, update = (\ a m -> (update a m, Effects.none))
, inputs =
[ Signal.map toggleOrientation <| Keyboard.isDown 68 {- D -}
, Signal.map updateSeed <| Time.every 3000
]
}
---- INPUT PROCESSORS ----
updateSeed : Float -> Action
updateSeed fl =
UpdateSeed <| floor fl
toggleOrientation : Bool -> Action
toggleOrientation bool =
if bool then
SetupOrientationToggle
else
NoOp
---- MODEL ----
defaultModel : Model
defaultModel =
{ state = Setup
, selectedShipId = Just 0 -- Potentially not reliable if we want to change the underlying model, a better way would be to explicitly get the first ID
, hoverPos = Nothing
, player = Player.defaultPlayer
, computer = Player.defaultComputer
, seed = 0
}
-- Model
type alias Model =
{ state : State
, selectedShipId : Maybe Int
, hoverPos : Maybe (Int, Int)
, player : Player.Player
, computer : Player.Player
, seed : Int
}
type Winner = Player1 | Player2
-- State
type State
= Setup
| Play
| GameOver Winner
(:=) = (,)
---- VIEW ----
-- Global wrapper
wrapper : List Html.Html -> Html.Html
wrapper htmlList =
Html.main'
[ Html.Attributes.style
[ "display" := "flex"
, "flex-direction" := "row"
, "align-items" := "center"
, "margin" := "50px 0px"
, "justify-content" := "center"
]
] htmlList
view : Signal.Address Action -> Model -> Html.Html
view address model =
let
spacer = Html.div
[Html.Attributes.style ["height" := "40px"]] []
in
case model.state of
Setup ->
wrapper <| setupView address model model.selectedShipId
Play ->
wrapper <| playView address model
GameOver winner ->
wrapper <| gameOverView winner
setupView : Signal.Address Action -> Model -> Maybe Int -> List Html.Html
setupView address model selectedShipId =
let
hoverClick = Just
{ hover = Signal.forwardTo address SetupShowShip
, click = Signal.forwardTo address SetupAddShip
}
shipSelector = Html.div
[ Html.Attributes.style
[ "display" := "flex"
, "overflow" := "hidden"
, "border-radius" := "10px"
]
] <| List.map (shipFieldView address selectedShipId) (Player.getShips model.player)
hint = Html.div
[ Html.Attributes.style ["margin" := "20px 0px"] ]
[ Html.text "Press \"D\" to change ship's orientation" ]
shipSetup = Html.div
[ Html.Attributes.style
[ "text-align" := "center" ]
]
[ hint, shipSelector ]
in
[ shipSetup
, Player.previewShip hoverClick model.hoverPos selectedShipId model.player
]
playView : Signal.Address Action -> Model -> List Html.Html
playView address model =
let
aimShoot =
Just
{ hover = Signal.forwardTo address PlayAim
, click = Signal.forwardTo address PlayShoot
}
in
[ Player.viewPrimaryGrid Nothing model.player
, Player.viewTrackingGrid aimShoot model.player
]
gameOverView : Winner -> List Html.Html
gameOverView winner =
let
gameOverMessage =
case winner of
Player1 -> "YOU WIN!!!"
Player2 -> "YOU ARE A LOSER!!!"
in
[ Html.text gameOverMessage ]
shipFieldView : Signal.Address Action -> Maybe Int -> Ship.Ship -> Html.Html
shipFieldView address selectedShipId ship =
let
isSelected =
case selectedShipId of
Just id -> id == ship.id
Nothing -> False
in
shipListView address ship isSelected
shipListView : Signal.Address Action -> Ship.Ship -> Bool -> Html.Html
shipListView address ship isSelected =
let
selectedStyle =
if isSelected then
[ "background-color" := "gray" ]
else
[]
direction =
case ship.orientation of
Ship.Horizontal -> "row"
Ship.Vertical -> "column"
hiddenStyle =
if ship.added then
["cursor" := "auto"]
else []
box = Html.div
[ Html.Attributes.style <|
[ "width" := "20px"
, "height" := "20px"
, "border" := "1px solid gray"
, "border-radius" := "3px"
, "margin" := "1px"
, "vertical-align" := "middle"
] ++ selectedStyle
] []
clickEvent =
Html.Events.onMouseDown address <|
if not isSelected then
SetupSelectShip (Just ship.id)
else
SetupOrientationToggle
in
Html.div
([ Html.Attributes.style <|
[ "display" := "flex"
, "flex-direction" := direction
, "align-items" := "center"
, "justify-content" := "center"
, "width" := "150px"
, "height" := "150px"
, "background-color" := "lightgray"
, "margin" := "0px 1px"
, "cursor" := "pointer"
] ++ hiddenStyle
] ++ if ship.added then [] else [clickEvent])
<| if ship.added then [] else List.repeat ship.length box
---- UPDATE ----
type Action
= SetupRandomOpponent Int
| SetupOrientationToggle
| SetupSelectShip (Maybe Int)
| SetupShowShip (Maybe (Int, Int))
| SetupAddShip (Int, Int)
| PlayAim (Maybe (Int, Int))
| PlayShoot (Int, Int)
| UpdateSeed Int
| NoOp
update : Action -> Model -> Model
update action model =
case action of
SetupRandomOpponent seed ->
-- TODO For some reason freezing the time signal never updates the
-- computer player, but if not frozen this function has to run each time
-- the signal updates, which is inefficient
if Player.allShipsAdded model.computer then
model
else
{ model | computer <- Player.random seed }
SetupOrientationToggle ->
case model.selectedShipId of
Just shipId ->
{ model | player <- Player.updateShip shipId Ship.toggleOrientation model.player }
Nothing ->
model
SetupSelectShip shipId ->
{ model | selectedShipId <- shipId }
SetupShowShip maybePos ->
{ model | hoverPos <- maybePos }
SetupAddShip pos ->
case model.selectedShipId of
Just shipId ->
let
newPlayer =
model.player
|> Player.updateShip shipId (\ship -> Ship.setLocation pos ship)
|> Player.addShip shipId
nextShipId = Player.nextNotAddedShipId newPlayer
in
{ model |
player <- newPlayer,
computer <- Player.random model.seed,
selectedShipId <- nextShipId,
-- If nextShipId is `Nothing`, It's time to `Play`
state <- if nextShipId == Nothing then Play else model.state
}
Nothing ->
model
PlayAim position ->
model
PlayShoot pos ->
let
(player, computer) = Player.shoot pos model.player model.computer
(newComputer, newPlayer) = AI.randomShot model.seed computer player
in
if | Player.allShipsSunk computer ->
{ model |
player <- player,
computer <- computer,
state <- GameOver Player1
}
| Player.allShipsSunk newPlayer ->
{ model |
player <- newPlayer,
computer <- newComputer,
state <- GameOver Player2
}
| otherwise ->
{ model |
player <- newPlayer,
computer <- newComputer
}
UpdateSeed seed ->
{ model | seed <- seed }
NoOp -> model