-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.elm
357 lines (302 loc) · 10.7 KB
/
Update.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
module Update exposing (update)
import Msg exposing (Msg(..))
import Model exposing (..)
import Material
import Extras
import Random.Pcg as Random exposing (Seed)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewGame ->
( Model.defaultState, Cmd.none )
Place floorId spaceId ->
case model.selected of
Just piece ->
let
newModel =
{ model
| board = Model.place piece floorId spaceId model.board
, rack = Model.removeFromRack piece model.rack
, selected = Nothing
}
in
if checkForAnyLines newModel.board then
( { newModel | turnState = Win }, Cmd.none )
else
( { newModel | turnState = SelectPiece }, Cmd.none )
Nothing ->
( model, Cmd.none )
Select piece ->
let
newModel =
cpuTurn { model | selected = Just piece }
in
if newModel.turnState == SelectPiece then
case cpuSelectPiece newModel of
Just cpuSelectedPiece ->
( { newModel
| turnState = PlayPiece
, selected = Just cpuSelectedPiece
}
, Cmd.none
)
Nothing ->
( { newModel | turnState = Win }, Cmd.none )
else
( newModel, Cmd.none )
Mdl msg' ->
Material.update msg' model
cpuSelectPiece : Model -> Maybe Piece
cpuSelectPiece model =
let
remainingPieces =
Model.getRemainingPieces model.rack
|> shuffle (Random.initialSeed 42)
in
Extras.find
(\piece ->
let
moves =
getMoves piece model.board
in
case Extras.find (winningMove model.board) moves of
Nothing ->
True
Just _ ->
False
)
remainingPieces
|> Extras.orElseLazy (\() -> Random.step (Random.sample remainingPieces) (Random.initialSeed 42) |> fst)
cpuTurn : Model -> Model
cpuTurn model =
case model.selected of
Just piece ->
case makeCpuMove piece model.board of
Just newBoard ->
let
newModel =
{ model
| board = newBoard
, rack = Model.removeFromRack piece model.rack
, selected = Nothing
}
in
if checkForAnyLines newModel.board then
{ newModel | turnState = Loss }
else
newModel
Nothing ->
model
Nothing ->
model
makeCpuMove piece board =
let
moves =
getMoves piece board
in
Extras.find (winningMove board) moves
|> Extras.orElseLazy (\() -> Extras.find (nonLosingMove board) moves)
|> Extras.orElseLazy (\() -> Random.step (Random.sample moves) (Random.initialSeed 42) |> fst)
|> Maybe.map (applyMove board)
applyMove : Board -> ( FloorId, SpaceId, Piece ) -> Board
applyMove board ( floorId, spaceId, piece ) =
Model.place piece floorId spaceId board
getMoves : Piece -> Board -> List ( FloorId, SpaceId, Piece )
getMoves piece board =
let
topFloor =
Model.getFloor Top board
middleFloor =
Model.getFloor Middle board
bottomFloor =
Model.getFloor Bottom board
moveList =
(List.filterMap
(\spaceId ->
case Model.getSpace spaceId topFloor of
EmptySpace ->
Just ( Top, spaceId, piece )
_ ->
Nothing
)
Model.spaceIdPossibilities
)
++ (List.filterMap
(\spaceId ->
case Model.getSpace spaceId middleFloor of
EmptySpace ->
Just ( Middle, spaceId, piece )
_ ->
Nothing
)
Model.spaceIdPossibilities
)
++ (List.filterMap
(\spaceId ->
case Model.getSpace spaceId bottomFloor of
EmptySpace ->
Just ( Bottom, spaceId, piece )
_ ->
Nothing
)
Model.spaceIdPossibilities
)
in
shuffle (Random.initialSeed 42) moveList
shuffle : Seed -> List a -> List a
shuffle seed list =
let
length =
List.length list
randomTags =
Random.step (Random.list length (Random.int 0 length)) seed
|> fst
in
List.map2 (,) randomTags list |> List.sortBy fst |> List.unzip |> snd
winningMove : Board -> ( FloorId, SpaceId, Piece ) -> Bool
winningMove board move =
applyMove board move
|> checkForAnyLines
nonLosingMove : Board -> ( FloorId, SpaceId, Piece ) -> Bool
nonLosingMove board (( floorId, spaceId, piece ) as move) =
let
potentialBoard =
applyMove board move
potentialFutureMoves =
getMoves piece potentialBoard
in
case Extras.find (winningMove potentialBoard) potentialFutureMoves of
Just _ ->
False
Nothing ->
True
checkForAnyLines : Board -> Bool
checkForAnyLines board =
checkMultiFloorLines board
|> andCheckFloor Top board
|> andCheckFloor Middle board
|> andCheckFloor Bottom board
checkMultiFloorLines : Board -> Bool
checkMultiFloorLines board =
let
topFloor =
Model.getFloor Top board
middleFloor =
Model.getFloor Middle board
bottomFloor =
Model.getFloor Bottom board
in
(--vertical lines
checkThreeByThree topFloor.zeroZero
topFloor.oneZero
topFloor.twoZero
middleFloor.zeroZero
middleFloor.oneZero
middleFloor.twoZero
bottomFloor.zeroZero
bottomFloor.oneZero
bottomFloor.twoZero
|> andCheckThreeByThree topFloor.zeroOne
topFloor.oneOne
topFloor.twoOne
middleFloor.zeroOne
middleFloor.oneOne
middleFloor.twoOne
bottomFloor.zeroOne
bottomFloor.oneOne
bottomFloor.twoOne
|> andCheckThreeByThree topFloor.zeroTwo
topFloor.oneTwo
topFloor.twoTwo
middleFloor.zeroTwo
middleFloor.oneTwo
middleFloor.twoTwo
bottomFloor.zeroTwo
bottomFloor.oneTwo
bottomFloor.twoTwo
)
|> -- diagonals
andCheckThreeByThree topFloor.zeroZero
topFloor.oneZero
topFloor.twoZero
middleFloor.zeroOne
middleFloor.oneOne
middleFloor.twoOne
bottomFloor.zeroTwo
bottomFloor.oneTwo
bottomFloor.twoTwo
|> andCheckThreeByThree topFloor.zeroZero
topFloor.zeroOne
topFloor.zeroTwo
middleFloor.oneZero
middleFloor.oneOne
middleFloor.oneTwo
bottomFloor.twoZero
bottomFloor.twoOne
bottomFloor.twoTwo
|> andCheckThreeByThree topFloor.zeroTwo
topFloor.oneTwo
topFloor.twoTwo
middleFloor.zeroOne
middleFloor.oneOne
middleFloor.twoOne
bottomFloor.zeroZero
bottomFloor.oneZero
bottomFloor.twoZero
|> andCheckThreeByThree topFloor.twoZero
topFloor.twoOne
topFloor.twoTwo
middleFloor.oneZero
middleFloor.oneOne
middleFloor.oneTwo
bottomFloor.zeroZero
bottomFloor.zeroOne
bottomFloor.zeroTwo
andCheckFloor : FloorId -> Board -> Bool -> Bool
andCheckFloor floorId board previousResult =
if previousResult then
previousResult
else
checkFloor floorId board
checkFloor : FloorId -> Board -> Bool
checkFloor floorId board =
let
floor =
Model.getFloor floorId board
in
checkThreeByThree floor.zeroZero
floor.oneZero
floor.twoZero
floor.zeroOne
floor.oneOne
floor.twoOne
floor.zeroTwo
floor.oneTwo
floor.twoTwo
checkThreeByThree space1 space2 space3 space4 space5 space6 space7 space8 space9 =
checkLine space1 space2 space3
|> andCheckLine space4 space5 space6
|> andCheckLine space7 space8 space9
|> andCheckLine space1 space4 space7
|> andCheckLine space2 space5 space8
|> andCheckLine space3 space6 space9
|> andCheckLine space1 space5 space9
|> andCheckLine space3 space5 space7
andCheckThreeByThree space1 space2 space3 space4 space5 space6 space7 space8 space9 previousResult =
if previousResult then
previousResult
else
checkThreeByThree space1 space2 space3 space4 space5 space6 space7 space8 space9
checkLine : Space -> Space -> Space -> Bool
checkLine space1 space2 space3 =
case ( space1, space2, space3 ) of
( OccupiedSpace piece1, OccupiedSpace piece2, OccupiedSpace piece3 ) ->
checkPiecesMatch piece1 piece2 piece3
_ ->
False
andCheckLine : Space -> Space -> Space -> Bool -> Bool
andCheckLine space1 space2 space3 previousResult =
if previousResult then
previousResult
else
checkLine space1 space2 space3