-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.hs
75 lines (64 loc) · 2.11 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
module Board where
import qualified Data.Map as M
import qualified Data.List as L
data Cell = Black
| White
| Empty
deriving(Show, Eq)
data Board = Board {
posMap :: M.Map (Int, Int) Cell,
size :: Int
}
instance Show Board where
show = unlines . map concat . L.transpose . makeCols
makeBoard :: Int -> Board
makeBoard n =
let
coordMax = (n-1) * 2
grid =
M.fromList $ map (\p -> (p, Empty))
(filter
(\(x, y) -> (x >= 0 && x <= coordMax &&
y >= 0 && y <= coordMax &&
abs (x-y) < n))
[(x, y) | x <- [0..coordMax], y <- [0..coordMax]])
in Board grid n
diam :: Board -> Int
diam = (+ (-1)) . (* 2) . size
makeSepCol :: Board -> Int -> [String]
makeSepCol b i =
let numSep = if i < size b then i + size b else 3 * size b - 1 - i
pad = replicate (2*(2 * size b - 1 - numSep)) " "
leftSep = [" /", "/ ", "\\ ", " \\"]
rightSep = ["\\ ", " \\", " /", "/ "]
sep = if i < size b then leftSep else rightSep
line = " " : " " : pad ++ concat (replicate numSep sep) ++ pad
in
line
makeCellCol :: Board -> Int -> [String]
makeCellCol b i =
let targetDiff = size b - 1 - i
cells = M.filterWithKey (\pos _ -> uncurry (-) pos == targetDiff) (posMap b)
sortedCells = L.sortOn fst (M.toList cells)
pad = replicate (2 * abs targetDiff) [" "]
in
concat $ [" "] : pad ++ ["______"] : map (uncurry printCellWithPos) sortedCells ++ pad
pad :: Int -> Char -> String -> String
pad n c
| n <= 1 = id
| otherwise = pad (n - 1) c . (:) c
printCellWithPos :: (Int, Int) -> Cell -> [String]
printCellWithPos (x, y) c =
case c of
Black -> [" ", " \x1b[30m▗██▖\x1b[0m ", " \x1b[30m▝██▘\x1b[0m ", "______"]
White -> [" ", " \x1b[37;1m▗██▖\x1b[0m ", " \x1b[37;1m▝██▘\x1b[0m ", "______"]
Empty -> [" ", pad 6 ' ' $ show x, pad 6 ' ' $ show y, "______"]
makeCols :: Board -> [[String]]
makeCols b =
let m = diam b * 2
in
map
(\i -> if even i
then makeSepCol b (i `div` 2)
else makeCellCol b (i `div` 2))
[0..m]