-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.elm
400 lines (298 loc) · 8.01 KB
/
Model.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
module Model exposing (..)
type alias Model =
{ board : Board
, selected : Maybe Piece
, rack : Rack
, gameState : GameState
, width : Float
, playerColouring : Colouring
}
defaultModel playerColouring =
{ board = initialBoard
, selected = Just (Piece playerColouring X)
, rack = initialRack
, gameState = InProgress
, width = 768
, playerColouring = playerColouring
}
type GameState
= InProgress
| Win
| Loss
type Space
= EmptySpace
| Space Piece
type Piece
= Piece Colouring Symbol
type Colouring
= Plain
| Coloured
oppositeColouring : Colouring -> Colouring
oppositeColouring c =
case c of
Plain ->
Coloured
Coloured ->
Plain
type Symbol
= X
| O
oppositeSymbol : Symbol -> Symbol
oppositeSymbol s =
case s of
X ->
O
O ->
X
type alias Rack =
{ plain : Int
, coloured : Int
}
initialRack : Rack
initialRack =
{ plain = 8
, coloured = 8
}
removeFromRack : Piece -> Rack -> Rack
removeFromRack piece rack =
case piece of
Piece Plain _ ->
{ rack | plain = max 0 (rack.plain - 1) }
Piece Coloured _ ->
{ rack | coloured = max 0 (rack.coloured - 1) }
place : Piece -> BoardId -> Board -> Board
place piece boardId board =
setSpace boardId (Space piece) board
getAvailableBoardIds : Board -> List BoardId
getAvailableBoardIds board =
boardIdPossibilities
|> List.filter (\id -> getSpace id board == EmptySpace)
getFlippapleBoardIds : Colouring -> Board -> List BoardId
getFlippapleBoardIds colouring board =
boardIdPossibilities
|> List.filter
(\id ->
case getSpace id board of
EmptySpace ->
False
Space (Piece spaceColouring _) ->
spaceColouring == colouring
)
getAvailablePieces : Colouring -> Rack -> List Piece
getAvailablePieces colouring rack =
case colouring of
Plain ->
if rack.plain > 0 then
[ Piece Plain O, Piece Plain X ]
else
[]
Coloured ->
if rack.coloured > 0 then
[ Piece Coloured O, Piece Coloured X ]
else
[]
type alias Board =
{ zeroZero : Space
, oneZero : Space
, twoZero : Space
, threeZero : Space
, zeroOne : Space
, oneOne : Space
, twoOne : Space
, threeOne : Space
, zeroTwo : Space
, oneTwo : Space
, twoTwo : Space
, threeTwo : Space
, zeroThree : Space
, oneThree : Space
, twoThree : Space
, threeThree : Space
}
initialBoard : Board
initialBoard =
{ zeroZero = EmptySpace
, oneZero = EmptySpace
, twoZero = EmptySpace
, threeZero = EmptySpace
, zeroOne = EmptySpace
, oneOne = EmptySpace
, twoOne = EmptySpace
, threeOne = EmptySpace
, zeroTwo = EmptySpace
, oneTwo = EmptySpace
, twoTwo = EmptySpace
, threeTwo = EmptySpace
, zeroThree = EmptySpace
, oneThree = EmptySpace
, twoThree = EmptySpace
, threeThree = EmptySpace
}
type BoardId
= ZeroZero
| OneZero
| TwoZero
| ThreeZero
| ZeroOne
| OneOne
| TwoOne
| ThreeOne
| ZeroTwo
| OneTwo
| TwoTwo
| ThreeTwo
| ZeroThree
| OneThree
| TwoThree
| ThreeThree
boardIdPossibilities =
[ ZeroZero
, OneZero
, TwoZero
, ThreeZero
, ZeroOne
, OneOne
, TwoOne
, ThreeOne
, ZeroTwo
, OneTwo
, TwoTwo
, ThreeTwo
, ZeroThree
, OneThree
, TwoThree
, ThreeThree
]
flipBoardPiece : BoardId -> Board -> Board
flipBoardPiece boardId board =
setSpace boardId (getSpace boardId board |> flipSpace) board
flipSpace : Space -> Space
flipSpace space =
case space of
Space (Piece colouring symbol) ->
Piece colouring (oppositeSymbol symbol) |> Space
EmptySpace ->
EmptySpace
lineExists : Colouring -> Board -> Bool
lineExists colouring board =
checkLine colouring board ZeroZero OneZero TwoZero ThreeZero
|| checkLine colouring board ZeroOne OneOne TwoOne ThreeOne
|| checkLine colouring board ZeroTwo OneTwo TwoTwo ThreeTwo
|| checkLine colouring board ZeroThree OneThree TwoThree ThreeThree
|| checkLine colouring board ZeroZero ZeroOne ZeroTwo ZeroThree
|| checkLine colouring board OneZero OneOne OneTwo OneThree
|| checkLine colouring board TwoZero TwoOne TwoTwo TwoThree
|| checkLine colouring board ThreeZero ThreeOne ThreeTwo ThreeThree
|| checkLine colouring board ZeroZero OneOne TwoTwo ThreeThree
|| checkLine colouring board ThreeZero TwoOne OneTwo ZeroThree
checkLine : Colouring -> Board -> BoardId -> BoardId -> BoardId -> BoardId -> Bool
checkLine colouring board id1 id2 id3 id4 =
case colouring of
--check for lines for the form player
Plain ->
symbolsMatch board id1 id2
&& symbolsMatch board id2 id3
&& symbolsMatch board id3 id4
--check for lines for the colour player
Coloured ->
lineColoursMatch board id1 id2
&& lineColoursMatch board id2 id3
&& lineColoursMatch board id3 id4
symbolsMatch : Board -> BoardId -> BoardId -> Bool
symbolsMatch board id1 id2 =
case ( getSpace id1 board, getSpace id2 board ) of
( Space (Piece _ s1), Space (Piece _ s2) ) ->
s1 == s2
_ ->
False
lineColoursMatch : Board -> BoardId -> BoardId -> Bool
lineColoursMatch board id1 id2 =
case ( getSpace id1 board, getSpace id2 board ) of
( Space p1, Space p2 ) ->
lineColourFromPiece p1 == lineColourFromPiece p2
_ ->
False
type LineColour
= Dark
| Light
lineColourFromPiece : Piece -> LineColour
lineColourFromPiece piece =
case piece of
Piece Plain X ->
Dark
Piece Plain O ->
Light
Piece Coloured X ->
Light
Piece Coloured O ->
Dark
getSpace : BoardId -> Board -> Space
getSpace boardId board =
case boardId of
ZeroZero ->
board.zeroZero
OneZero ->
board.oneZero
TwoZero ->
board.twoZero
ThreeZero ->
board.threeZero
ZeroOne ->
board.zeroOne
OneOne ->
board.oneOne
TwoOne ->
board.twoOne
ThreeOne ->
board.threeOne
ZeroTwo ->
board.zeroTwo
OneTwo ->
board.oneTwo
TwoTwo ->
board.twoTwo
ThreeTwo ->
board.threeTwo
ZeroThree ->
board.zeroThree
OneThree ->
board.oneThree
TwoThree ->
board.twoThree
ThreeThree ->
board.threeThree
setSpace : BoardId -> Space -> Board -> Board
setSpace boardId space board =
case boardId of
ZeroZero ->
{ board | zeroZero = space }
OneZero ->
{ board | oneZero = space }
TwoZero ->
{ board | twoZero = space }
ThreeZero ->
{ board | threeZero = space }
ZeroOne ->
{ board | zeroOne = space }
OneOne ->
{ board | oneOne = space }
TwoOne ->
{ board | twoOne = space }
ThreeOne ->
{ board | threeOne = space }
ZeroTwo ->
{ board | zeroTwo = space }
OneTwo ->
{ board | oneTwo = space }
TwoTwo ->
{ board | twoTwo = space }
ThreeTwo ->
{ board | threeTwo = space }
ZeroThree ->
{ board | zeroThree = space }
OneThree ->
{ board | oneThree = space }
TwoThree ->
{ board | twoThree = space }
ThreeThree ->
{ board | threeThree = space }