-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.hs
412 lines (333 loc) · 14.8 KB
/
Board.hs
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
401
402
403
404
405
406
407
408
409
410
411
412
module Board where
import qualified Data.Map as Map
import Data.List
import Random
import Utils (slice, mapFetch)
-- ==============================
-- declarations and instancing
-- ==============================
--white connects top to bottom
--black connects left to right
data Color = Black | White deriving Eq
instance Show Color where
show Black = "B"
show White = "W"
type Coord = Int
--size is (maxrow + 1, maxcol + 1)
type Size = (Coord, Coord)
--pos is (row, col) where row|col is in [0, maxrow|maxcol -1]
type Pos = (Coord, Coord)
type GroupId = Pos
data Peg = Peg {pegPos :: Pos, pegColor :: Color} deriving (Eq)
type Pegs = [Peg]
type PegMap = Map.Map Pos Peg
data Group = Group {grpColor :: Color,
grpId :: GroupId,
grpMinCoord :: Coord,
grpMaxCoord :: Coord,
grpPegs :: Pegs
} deriving (Show, Eq)
type GroupMap = Map.Map GroupId Group
data Board = Board {bdSize :: Size,
bdPegMap:: PegMap,
bdToPlay :: Color,
bdGroupMap :: GroupMap,
bdWinner :: Maybe Color,
bdLastWhite :: Maybe Peg,
bdLastBlack :: Maybe Peg
} deriving (Eq)
-- ==============================
-- representation
-- ==============================
colorToJsonStr :: Color -> String
colorToJsonStr White = "1"
colorToJsonStr Black = "2"
pegToJsonStr :: Peg -> String
pegToJsonStr peg = "{\"player\": " ++ (colorToJsonStr (pegColor peg)) ++ ", \"x\":" ++
(show $ getCol(pegPos peg) + 1) ++ ", \"y\":" ++ (show $ getRow(pegPos peg) + 1) ++
", \"type\": 1}"
pegsToJsonStr :: Pegs -> String
pegsToJsonStr pegs = "[" ++ (intercalate "," (map pegToJsonStr pegs)) ++ "]"
showSquare :: Board -> Pos -> String
showSquare board pos = let value = Map.lookup pos (bdPegMap board)
in case value of
Nothing -> " . "
Just peg -> " " ++ show (pegColor peg) ++ " "
instance Show Peg where
show peg = "[" ++ show (pegPos peg) ++ show (pegColor peg) ++ "]"
instance Show Board where
show board =
let (sizey, sizex) = bdSize board
line = [ showSquare board (row, col) | row <- [0, 1..(sizey - 1)], col <- [0, 1..(sizex - 1)] ]
header = "Board " ++ (show $ bdToPlay board) ++ " to play:"
in unlines $ [header] ++
(map concat $ slice sizex line)
-- ++ ["Groups: " ++ show (Map.elems $ bdGroupMap board)]
-- ==============================
-- constructors and convenience functions
-- ==============================
mkBoard :: Coord -> Coord -> Board
mkBoard sizey sizex =
let size = (sizey, sizex) in
Board {
bdSize = size,
bdToPlay = White,
bdPegMap = Map.empty,
bdGroupMap = Map.empty,
bdWinner = Nothing,
bdLastWhite = Nothing,
bdLastBlack = Nothing
}
mkGroup :: Peg -> Group
mkGroup peg =
let coord = if pegColor peg == White
then getRow (pegPos peg)
else getCol (pegPos peg)
in Group{
grpColor=pegColor peg,
grpId=pegPos peg,
grpMinCoord=coord,
grpMaxCoord=coord,
grpPegs = [peg]
}
mkPeg :: Coord -> Coord -> Color -> Peg
mkPeg posy posx color = Peg{pegPos=(posy, posx), pegColor=color}
--abs + rel pos -> pos
offsetPos :: Pos -> Pos -> Pos
offsetPos (posy, posx) (offy, offx) = (posy + offy, posx + offx)
oppColor :: Color -> Color
oppColor White = Black
oppColor Black = White
getRow :: Pos -> Coord
getRow (row, col) = row
getCol :: Pos -> Coord
getCol (row, col) = col
pegOnRow :: Peg -> Coord -> Bool
pegOnRow peg row = (getRow $ pegPos peg) == row
pegOnCol :: Peg -> Coord -> Bool
pegOnCol peg col = (getCol $ pegPos peg) == col
mkPegs :: [(Coord, Coord, Color)] -> Pegs
mkPegs [] = []
mkPegs ((posy, posx, color):rest) = (mkPeg posy posx color):(mkPegs rest)
pegsByColor :: Pegs -> Color -> Pegs
pegsByColor pegs color = filter (\peg -> pegColor peg == color) pegs
whitePegs = flip pegsByColor White
blackPegs = flip pegsByColor Black
pegSameColor :: Peg -> Peg -> Bool
pegSameColor peg1 peg2 = pegColor peg1 == pegColor peg2
getBoardPegs :: Board -> Pegs
getBoardPegs board = Map.elems $ bdPegMap board
noPos :: Pos
noPos = (0, 0)
noPeg :: Peg
noPeg = mkPeg 0 0 White
getRandomPos :: (RandomGen gen) => Board -> gen -> (Pos, gen)
getRandomPos board gen =
let (row, gen1) = randomR (1, getRow (bdSize board) - 1) gen
(col, gen2) = randomR (1, getCol (bdSize board) - 1) gen1
in ((row, col), gen)
getLastByColor :: Board -> Color -> Maybe Peg
getLastByColor board White = bdLastWhite board
getLastByColor board Black = bdLastBlack board
-- ==============================
-- playable positions
-- ==============================
getCorners :: Size -> [Pos]
getCorners size =
[(0, 0),
(getRow size - 1, 0),
(getRow size - 1, getCol size - 1),
(0, getCol size - 1)]
getSpecificPos :: Size -> Color -> [Pos]
getSpecificPos size White =
[(y, x) | y <- [0, (getRow size - 1)], x <- [1.. getCol size - 2]]
getSpecificPos size Black =
[(y, x) | x <- [0, (getCol size - 1)], y <- [1.. getRow size - 2]]
getCommonPos :: Size -> [Pos]
getCommonPos size =
[(y, x) | y <- [1..(getRow size - 2)],
x <- [1..(getCol size - 2)]]
getAllEmptyPos :: Board -> [Pos]
getAllEmptyPos board =
let size = bdSize board in
--all squares
[(y, x) | y <- [0..(getRow size - 1)], x <- [0..(getCol size - 1)]]
--minus corners and full squares
\\ ((getCorners size) ++ (Map.keys $ bdPegMap board))
getEmptyPos :: Board -> Color -> [Pos]
getEmptyPos board color =
let all = (getSpecificPos (bdSize board) color) ++
(getCommonPos (bdSize board))
in (nub all \\ (Map.keys $ bdPegMap board))
-- ==============================
-- limitations
-- ==============================
isGoalEdge:: Size -> Pos -> Color -> Bool
isGoalEdge _ (0, _) White = True
isGoalEdge _ (_, 0) Black = True
isGoalEdge (sizey, _) (posy, _) White | sizey - 1 == posy = True
isGoalEdge (_, sizex) (_, posx) Black | sizex - 1 == posx = True
isGoalEdge _ _ _ = False
isEdge :: Size -> Pos -> Bool
isEdge size pos = (isGoalEdge pos size White) || (isGoalEdge pos size Black)
isOnBoard :: Board -> Peg -> Bool
isOnBoard board peg = getRow (pegPos peg) < getRow (bdSize board)
&& getRow (pegPos peg) >= 0
&& getCol (pegPos peg) < getCol (bdSize board)
&& getCol (pegPos peg) >= 0
isEmptySquare :: Board -> Pos -> Bool
isEmptySquare board pos = Map.lookup pos (bdPegMap board) == Nothing
--on the board, not on the opponents edge and on the empty square
isLegalMove :: Board -> Peg -> Bool
isLegalMove board peg = isOnBoard board peg
&& not (isGoalEdge (bdSize board) (pegPos peg) (oppColor $ pegColor peg))
&& Map.lookup (pegPos peg) (bdPegMap board) == Nothing
-- ==============================
-- bridge spoiling
-- ==============================
--transforming posPair in any position to posPair in referential position
rotateClockWise :: Pos -> Pos
rotateClockWise (row, col) = (col, -1 * row)
flipVertically :: Pos -> Pos
flipVertically (row, col) = (-1 * row, col)
--relative positions of cross pairs for bridge at referential position (2`oclock)
relSpoilPairs = [((-2, 0), (0, 1)), ((-1, -1), (0, 1)), ((-1, 0), (1, 1)), ((1, 0), (-1, 1)),
(( 1, 2), (-1, 1)), ((0, 3), (-1, 1)), ((0, 2), (-2, 1)), ((0, 1), (-2, 2))]
--for a potential bridge (pair of positions) and processing function gives positions which can spoil it
--processor handles transformations on relSpoilPairs arising from use of non-referential pair orientation
doGenSpoilPairs :: (Pos -> Pos) -> (Pos, Pos) -> [(Pos, Pos)]
doGenSpoilPairs processor (pos1@(pos1y, pos1x), pos2@(pos2y, pos2x)) =
let processed = map (\(off1, off2) -> (processor off1, processor off2)) relSpoilPairs
in map (\(off1, off2) -> (offsetPos pos1 off1, offsetPos pos1 off2)) processed
--wrapper around doGenSpoilPairs
genSpoilPairs :: (Pos, Pos) -> [(Pos, Pos)]
genSpoilPairs posPair@((pos1y, pos1x), (pos2y, pos2x))
--pos1 should be more on the left
| pos1x >= pos2x = genSpoilPairs ((pos2y, pos2x), (pos1y, pos1x))
--1 o`clock
| pos1y - pos2y == 2 = doGenSpoilPairs (flipVertically . rotateClockWise) posPair
--2 o`clock - referential position
| pos1y - pos2y == 1 = doGenSpoilPairs id posPair
--4 o`clock
| pos2y - pos1y == 1 = doGenSpoilPairs flipVertically posPair
--5 o`clock
| pos2y - pos1y == 2 = doGenSpoilPairs rotateClockWise posPair
| otherwise = error "No matching pos pair position"
--peg-level wrapper for genSpoilPairs
dropSpoilPegs :: Peg -> Pegs -> (Pos->Pos->Bool) -> Pegs
--signature: newPeg -> neighbors -> check already connected (opponents) function -> updatedNeighbors
dropSpoilPegs newPeg neighborPegs alreadyConnected =
let neighborPegsPos = map pegPos neighborPegs
newPegPos = pegPos newPeg
posPairs = zip neighborPegsPos $ replicate (length neighborPegsPos) newPegPos
--new peg + some neighbor -> bridges they would spoil if connected
spoilPairs = map genSpoilPairs posPairs
--now check which of these pairs are actually already connected
spoiled = map (any id . (map $ uncurry alreadyConnected)) spoilPairs
--keep only those neighbors which are not spoiled
in map fst $ filter (not . snd) $ zip neighborPegs spoiled
--drops neighbors new peg cannot connect to
genGoodNeighbors :: Board -> Peg -> Pegs -> Pegs
genGoodNeighbors board newPeg neighbors = dropSpoilPegs newPeg neighbors (isBridge board)
-- ==============================
-- connecting pegs and merging groups
-- ==============================
relJumps :: [Pos]
relJumps = [(1, -2), (1, 2), (2, 1), (2, -1), (-1, 2), (-1, -2), (-2, 1), (-2, -1)]
jumps :: Pos -> [Pos]
jumps (posy, posx) = map (\(rely, relx) -> (posy + rely, posx + relx)) relJumps
filterConnectedPegs :: Peg -> Pegs -> Pegs
filterConnectedPegs peg toFilter =
let connectedPos = jumps (pegPos peg)
in filter (\apeg -> pegSameColor peg apeg && elem (pegPos apeg) connectedPos) toFilter
getConnectedPegs :: Board -> Peg -> Pegs
getConnectedPegs board peg =
let connectedPos = jumps (pegPos peg)
in filter (\apeg -> pegSameColor peg apeg) $ mapFetch connectedPos (bdPegMap board)
--looks inefficient but usually is used for little number of groups
mergeGroups :: [Group] -> Group -> Group
mergeGroups [] baseGroup = baseGroup
mergeGroups (g:gs) baseGroup =
let updatedGroup = Group {grpColor = grpColor baseGroup,
grpId = grpId baseGroup,
grpMinCoord = min (grpMinCoord baseGroup) (grpMinCoord g),
grpMaxCoord = max (grpMaxCoord baseGroup) (grpMaxCoord g),
grpPegs = (grpPegs g) ++ (grpPegs baseGroup) }
in mergeGroups gs updatedGroup
--checks whether all the pegs have same group number
arePosConnected :: Board -> [Pos] -> Bool
arePosConnected board positions = let nubbed = nub $ mapFetch positions (bdGroupMap board)
in length nubbed <= 1
isBridge :: Board -> Pos -> Pos -> Bool
isBridge board pos1 pos2 =
let group1 = Map.lookup pos1 (bdGroupMap board)
group2 = Map.lookup pos2 (bdGroupMap board)
in group1 /= Nothing && group1 == group2
arePegsConnected :: Board -> Pegs -> Bool
arePegsConnected board pegs = arePosConnected board $ map pegPos pegs
-- ==============================
-- move placing and wrappers
-- ==============================
--TODO optimize
getPosForGroup :: Board -> Group -> [Pos]
getPosForGroup board group = map pegPos (grpPegs group)
-- expects legal move
placePeg :: Board -> Peg -> Board
placePeg board peg | not $ isLegalMove board peg = error "illegal move"
placePeg board peg | otherwise =
let -- pegMapcontaining new peg
newPegMap = Map.insert (pegPos peg) peg (bdPegMap board)
-- update groupMap with newgroups id
neighbors = getConnectedPegs board peg
-- consider only those we can connectTo
goodNeighbors = genGoodNeighbors board peg neighbors
-- connect all the neighboring groups
groupsToUpdate = nub $ mapFetch (map pegPos goodNeighbors) (bdGroupMap board)
-- collect all the positions that must be updated
posToUpdate = concat $ map (getPosForGroup board) groupsToUpdate
-- merge groups in groupMap
newGroup = mergeGroups groupsToUpdate $ mkGroup peg
-- find neighboring pegs
newGroupMap = foldl (\groupMap pos -> Map.insert pos newGroup groupMap)
(bdGroupMap board) $ posToUpdate ++ [pegPos peg]
-- create the board
in Board {bdSize = bdSize board,
bdPegMap = newPegMap,
bdGroupMap = newGroupMap,
bdToPlay = oppColor $ bdToPlay board,
bdWinner = getWinnerFromGroup (bdSize board) newGroup (bdWinner board),
bdLastWhite = if pegColor peg == White then Just peg else bdLastWhite board,
bdLastBlack = if pegColor peg == Black then Just peg else bdLastBlack board
}
--silently falls back to original board if move not legal
placePegFallback :: Board -> Peg -> Board
placePegFallback board peg | not $ isLegalMove board peg = board
placePegFallback board peg | otherwise = placePeg board peg
--if not legal move returns Nothing
placePegMaybe :: Board -> Peg -> Maybe Board
placePegMaybe board peg | not $ isLegalMove board peg = Nothing
placePegMaybe board peg | otherwise = Just $ placePeg board peg
--places a sequence of pegs - expects move to be legal
placePegSeq :: Board -> Pegs -> Board
placePegSeq initBoard [] = initBoard
placePegSeq initBoard seq = foldl placePeg initBoard seq
-- ==============================
-- winner recognition functionality
-- ==============================
getWinnerFromGroup :: Size -> Group -> Maybe Color -> Maybe Color
getWinnerFromGroup size group oldWinner | oldWinner /= Nothing = oldWinner
getWinnerFromGroup size group oldWinner | isWinGroup size group = Just (grpColor group)
getWinnerFromGroup size group oldWinner = Nothing
isWinGroup :: Size -> Group -> Bool
isWinGroup size group | grpColor group == White
= grpMinCoord group == 0 && grpMaxCoord group == getRow size - 1
isWinGroup size group | grpColor group == Black
= grpMinCoord group == 0 && grpMaxCoord group == getCol size - 1
getWinner :: Board -> Maybe Color
getWinner board = bdWinner board
hasWinner :: Board -> Bool
hasWinner board = getWinner board /= Nothing
isDraw :: Board -> Bool
isDraw board =
let (y, x) = bdSize board
in Map.size (bdPegMap board) == x * y - 4