-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.elm
276 lines (232 loc) · 5.85 KB
/
Board.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
module Board exposing (..)
import Collage exposing (..)
import Element exposing (Element)
import Color exposing (Color)
import Set exposing (empty, fromList, toList, union)
import Common exposing (boardFreeSpace, fieldWidth, fieldHeight, Direction, toDimension, cellSize, shiftShip, fieldRows, fieldCols)
import Ship
type alias Model =
{ myField : Field
, otherField : Field
}
type Side
= My
| Opponent
type alias Field =
{ ships : Ships
, side : Side
, shiftAt : (Float, Float)
, occupiedArea : List (Int, Int)
, validPosition : Bool
}
type alias Ships =
List Ship.Model
initialModel : Model
initialModel =
let
shiftX field =
(fieldWidth + boardFreeSpace) / 2
shiftField field =
case field.side of
My ->
{ field | shiftAt = (-(shiftX field), 0) }
Opponent ->
{ field | shiftAt = (shiftX field, 0) }
in
{ myField = initialField My |> shiftField
, otherField = initialField Opponent |> shiftField
}
initialField : Side -> Field
initialField side =
let
model =
{ ships = []
, side = side
, shiftAt = (0, 0)
, occupiedArea = []
, validPosition = True
}
in
case side of
My ->
model
Opponent ->
model
fieldColor : Color
fieldColor = Color.lightBlue
shipLengths : List Int
shipLengths =
List.map (\n -> List.repeat n (5 - n)) [1..4]
|> List.foldr (++) []
toElement : Model -> Element
toElement model =
[ model.myField, model.otherField ]
|> List.map fieldToForm
|> collage (round (boardFreeSpace * 3 + fieldWidth * 2)) (round (boardFreeSpace * 2 + fieldHeight))
fieldToForm : Field -> Form
fieldToForm field =
let
fieldRect = rect fieldWidth fieldHeight
border = outlined (solid Color.black) fieldRect
ships =
case List.head field.ships of
Nothing ->
[]
Just currentShip ->
-- Debug.crash currentShip
let
currentShipForm =
case field.validPosition of
True ->
Ship.toForm currentShip
False ->
Ship.toInvalidForm currentShip
otherShipForms =
case List.tail field.ships of
Nothing ->
[]
Just otherShips ->
List.map Ship.toForm otherShips
in
List.reverse (currentShipForm :: otherShipForms)
in
group
[ filled fieldColor fieldRect
, occupiedForm field
, border
, group ships
]
|> move field.shiftAt
addNextShip : Model -> Model
addNextShip model =
{ model
| myField =
model.myField
|> addNextShipToField
|> updateOccupied
|> checkPosition
}
rotateCurrentShip : Model -> Model
rotateCurrentShip model =
{ model
| myField =
model.myField
|> rotateCurrentShipInAField
|> updateOccupied
|> checkPosition
}
moveCurrentShip : Model -> Direction -> Model
moveCurrentShip model direction =
{ model
| myField =
model.myField
|> moveCurrentShipInAField direction
|> updateOccupied
|> checkPosition
}
updateOccupied : Field -> Field
updateOccupied field =
{ field
| occupiedArea =
field.ships
|> occupiedArea
}
checkPosition : Field -> Field
checkPosition field =
{ field
| validPosition =
case List.head field.ships of
Nothing ->
True
Just ship ->
ship.shape
|> List.any (\point -> List.member point field.occupiedArea)
|> not
}
addNextShipToField : Field -> Field
addNextShipToField field =
let
nextShipLength =
List.drop (List.length field.ships) shipLengths
|> List.head
|> Maybe.withDefault 0
in
case nextShipLength of
0 ->
field
_ ->
{ field
| ships = (Ship.init 0 nextShipLength) :: field.ships
}
rotateCurrentShipInAField : Field -> Field
rotateCurrentShipInAField field =
{ field | ships = rotateFirstShip field.ships }
rotateFirstShip : Ships -> Ships
rotateFirstShip ships =
let
firstShip = List.head ships
in
case firstShip of
Nothing ->
ships
Just ship ->
let
rotated = Ship.rotate ship
otherShips = List.tail ships |> Maybe.withDefault []
in
rotated :: otherShips
moveCurrentShipInAField : Direction -> Field -> Field
moveCurrentShipInAField direction field =
let
firstShip = List.head field.ships
in
case firstShip of
Nothing ->
field
Just ship ->
let
moved = Ship.moveTo ship direction
otherShips = List.tail field.ships |> Maybe.withDefault []
in
{ field | ships = moved :: otherShips }
occupiedArea : Ships -> List (Int, Int)
occupiedArea ships =
let
pointToOccupied (row, col) =
let
rangeForRow row =
Set.fromList [(row, col - 1), (row, col), (row, col + 1)]
in
rangeForRow (row + 1)
|> Set.union (rangeForRow row)
|> Set.union (rangeForRow (row - 1))
occupied ship =
ship.shape
|> List.map pointToOccupied
|> List.foldl Set.union Set.empty
allOccupied ships =
ships
|> List.map occupied
|> List.foldl Set.union Set.empty
inTheField (row, col) =
row >= 0 && row < fieldRows && col >= 0 && col < fieldCols
in
List.tail ships
|> Maybe.withDefault []
|> allOccupied
|> Set.filter inTheField
|> Set.toList
occupiedForm : Field -> Form
occupiedForm field =
let
translate (row, column) =
move (toDimension column, toDimension -row) form
form =
square cellSize
|> filled (Color.rgba 50 50 50 0.5)
forms =
field.occupiedArea
|> List.map translate
in
group forms
|> move shiftShip