-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOthello.elm
266 lines (201 loc) · 6.08 KB
/
Othello.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
module Othello
exposing
( Board(..)
, Cell(..)
, Stone(..)
, Coord
, Move
, Moves
, NextMoves(..)
, startBoard
, initialValidMoves
, otherColor
, cellAt
, countStones
, applyMove
, moveAtCoord
, moveCoord
, movePlayer
, emptyMoves
, toMoveList
, validNextMoves
)
import Array exposing (Array)
import Array as Array
import Dict exposing (Dict)
import Dict as Dict
type Board
= Board (Array (Array Cell))
startBoard : Board
startBoard =
[ List.repeat 8 Empty
, List.repeat 8 Empty
, List.repeat 8 Empty
, [ Empty, Empty, Empty, Occupied Black, Occupied White, Empty, Empty, Empty ]
, [ Empty, Empty, Empty, Occupied White, Occupied Black, Empty, Empty, Empty ]
, List.repeat 8 Empty
, List.repeat 8 Empty
, List.repeat 8 Empty
]
|> List.map Array.fromList
|> Array.fromList
|> Board
type Cell
= Empty
| Occupied Stone
type Stone
= Black
| White
otherColor : Stone -> Stone
otherColor stone =
case stone of
Black ->
White
White ->
Black
type NextMoves
= NoValidMoves
| ValidMoves Stone Moves
type Moves
= Moves (Dict Coord Move)
type Move
= Move
{ atCoord : Coord
, changedCells : List Coord
, player : Stone
}
type alias Coord =
( Int, Int )
initialValidMoves : Moves
initialValidMoves =
validMoves startBoard White
cellAt : Board -> Coord -> Maybe Cell
cellAt (Board board) ( row, col ) =
Array.get row board
`Maybe.andThen` (Array.get col)
countStones : Board -> Stone -> Int
countStones (Board board) player =
Array.toList board
|> List.map (Array.filter (\cell -> cell == Occupied player) >> Array.length)
|> List.sum
emptyMoves : Moves
emptyMoves =
Moves (Dict.empty)
isEmptyMoves : Moves -> Bool
isEmptyMoves (Moves dict) =
Dict.isEmpty dict
applyMove : Move -> Board -> Board
applyMove (Move { player, changedCells }) board =
setStones board player changedCells
toMoveList : Moves -> List Move
toMoveList (Moves dict) =
Dict.values dict
moveAtCoord : Moves -> Coord -> Maybe Move
moveAtCoord (Moves dict) coord =
Dict.get coord dict
moveCoord : Move -> Coord
moveCoord (Move move) =
move.atCoord
movePlayer : Move -> Stone
movePlayer (Move move) =
move.player
validNextMoves : Board -> Stone -> NextMoves
validNextMoves board player =
let
currentMoves =
validMoves board player
in
if not (isEmptyMoves currentMoves) then
ValidMoves player currentMoves
else
let
otherPlayer =
otherColor player
otherMoves =
validMoves board otherPlayer
in
if not (isEmptyMoves otherMoves) then
ValidMoves otherPlayer otherMoves
else
NoValidMoves
validMoves : Board -> Stone -> Moves
validMoves board stone =
List.concatMap (\r -> List.map (\c -> ( r, c )) [0..7]) [0..7]
|> List.filterMap
(\coord ->
let
changed =
affectedCellsByMoveAt board stone coord
in
if List.isEmpty changed then
Nothing
else
Just
( coord
, Move
{ atCoord = coord
, changedCells = changed
, player = stone
}
)
)
|> Dict.fromList
|> Moves
affectedCellsByMoveAt : Board -> Stone -> Coord -> List Coord
affectedCellsByMoveAt board player coord =
case
List.concatMap
(affectedCellsByMoveAtInDirection board player coord)
[ ( -1, -1 ), ( -1, 0 ), ( -1, 1 ), ( 0, 1 ), ( 1, 1 ), ( 1, 0 ), ( 1, -1 ), ( 0, -1 ) ]
of
[] ->
[]
cells ->
coord :: cells
affectedCellsByMoveAtInDirection : Board -> Stone -> Coord -> ( Int, Int ) -> List Coord
affectedCellsByMoveAtInDirection board player coord dir =
if not (isEmptyCell board coord) then
[]
else
terminatedWith board player dir (move dir coord) []
terminatedWith : Board -> Stone -> ( Int, Int ) -> Coord -> List Coord -> List Coord
terminatedWith board stone dir coord captured =
case getCellOccupation board coord of
Just stone' ->
if stone' == stone then
captured
else
terminatedWith board stone dir (move dir coord) (coord :: captured)
Nothing ->
[]
setStones : Board -> Stone -> List Coord -> Board
setStones board stone =
List.foldl (setStone stone) board
setStone : Stone -> Coord -> Board -> Board
setStone stone ( row, col ) (Board board) =
let
updateRow oldRow =
Array.set col (Occupied stone) oldRow
in
case Array.get row board of
Just oldRow ->
Board (Array.set row (updateRow oldRow) board)
Nothing ->
(Board board)
isEmptyCell : Board -> Coord -> Bool
isEmptyCell board coord =
cellAt board coord == Just Empty
getCellOccupation : Board -> Coord -> Maybe Stone
getCellOccupation board coord =
cellAt board coord
`Maybe.andThen`
(\cell ->
case cell of
Empty ->
Nothing
Occupied stone ->
Just stone
)
move : ( Int, Int ) -> Coord -> Coord
move ( dx, dy ) ( row, col ) =
( row + dx, col + dy )