-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hex.hs
181 lines (140 loc) · 5.94 KB
/
Hex.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
{-{ http://en2.wikipedia.org/wiki/Hex_(game) }-}
---------
-- Hex --
---------
module Hex (Hex, hex) where
import Game
import Data.Array
import Graphics.UI.WX hiding (border, point)
import Graphics.UI.WXCore hiding (point, win)
import Tools
data Hex = Hex (Array (Int, Int) (Maybe Player)) deriving (Eq, Show)
type HexMove = (Int, Int)
hex :: Hex
hex = undefined
instance Game Hex where
name _ = "hex"
standard _ = Properties { players = 2, boardsize = 11, human = [True, False] }
possible _ = PropertyRange { playersrange = [2], boardsizerange = [5 .. 15] }
new pr = let bsz = boardsize pr
in Hex $ array ((0, 0), (bsz - 1, bsz - 1)) [((x, y), Nothing) | x <- [0 .. bsz - 1], y <- [0 .. bsz - 1]]
moves pr p (Hex st) = map (move pr) (allMoves pr p st)
showmove pr p (Hex s) i = case allMoves pr p s !! i
of (x, y) -> ['a' ..] !! x : show (1 + y)
value pr _p (Hex st)
| win st (boardsize pr) 0 = [ 1, -1]
| win st (boardsize pr) 1 = [-1, 1]
| otherwise = [ 0, 0]
board p pr vart _ia move' = do
marble <- bitmapCreateLoad "images\\marble.bmp" wxBITMAP_TYPE_ANY
varg <- varCreate $ grate rectZero 0 (0, 0) sizeZero
let
onpaint :: DC () -> Rect -> IO ()
onpaint dc r = do
t <- varGet vart
let Hex st = state t
bsz = boardsize pr
b <- border dc (bsz, bsz)
let g = grate r b (2 * bsz + 1, 2 * bsz + 2) (Size 7 4)
radius = rectWidth (field g (0, 0)) `div` 3
lin' :: Rect -> Rect -> Color -> IO ()
lin' (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2) c = do
line dc (pt (x1 + w1 `div` 2) (y1 + h1)) (pt (x2 + w2 `div` 2) (y2 + h2)) [penWidth := 4, penColor := c]
lin :: (Int, Int) -> (Int, Int) -> Color -> IO ()
lin p' q = lin' (field g $ tograte bsz p') (field g $ tograte bsz q)
varSet varg g
tileBitmap dc r marble
--{ drawGrate dc g [penColor := yellow]
for 0 (bsz - 1) (\i -> do
drawTextRect dc [['A' ..] !! i] $ field g (bsz + 2 + i, 2 * bsz + 1 - i) |#| field g (bsz + 2 + i, 2 * bsz + 2 - i)
drawTextRect dc (show (i + 1)) $ field g (bsz - 2 - i, 2 * bsz + 1 - i) |#| field g (bsz - 2 - i, 2 * bsz + 2 - i)
)
lin (0, -1) (bsz - 1, -1) blue
lin (0, bsz) (bsz - 1, bsz) blue
lin (-1, 0) (-1, bsz - 1) red
lin (bsz, 0) (bsz, bsz - 1) red
for 0 (bsz - 1) (\i -> for 0 (bsz - 1) (\j ->
drawPiece dc (field g $ tograte bsz (i, j)) radius (st ! (i, j))
) )
onclick :: Point -> IO ()
onclick point = do
t <- varGet vart
g <- varGet varg
let Hex st = state t
bsz = boardsize pr
n = fromgrate bsz $ locate g point
case lookup n $ zip (allMoves pr (player t) st) [0..] of
Nothing -> return ()
Just i -> move' i
set p [ on click := onclick
, on paint := onpaint
, on resize ::= repaint
]
return ()
drawPiece :: DC () -> Rect -> Int -> Maybe Player -> IO ()
drawPiece dc (Rect x y w h) r mp = do
circle dc (pt (x + w `div` 2) (y + h)) (max 1 (r `div` 5)) [brushColor := black]
case mp of Just 0 -> circle dc (pt (x + w `div` 2) (y + h)) r [brushKind := BrushTransparent, penColor := blue, penWidth := 3]
Just 1 -> circle dc (pt (x + w `div` 2) (y + h)) r [brushKind := BrushTransparent, penColor := red , penWidth := 3]
Nothing -> return ()
_ -> error "drawPiece: Unexpected value"
tograte :: Int -> (Int, Int) -> (Int, Int)
tograte bsz (i, j) = (bsz + i - j, 2 * bsz - 1 - i - j)
fromgrate :: Int -> (Int, Int) -> (Int, Int)
fromgrate bsz (i, j) = ((bsz + i - j) `div` 2, (3 * bsz - i - j) `div` 2)
{-
i = s - 1 + x - y
j = 2s - 2 - x - y
i + j = 3s - 3 - 2y
2y = 3s - 3 - i - j
y = (...) / 2
i - j = -s + 1 + 2x
2x = s - 1 + i - j
-}
(+-) :: Num a => (a, a) -> (a, a) -> (a, a)
(a, b) +- (c, d) = (a + c, b + d)
allMoves :: Properties -> Player -> Array (Int, Int) (Maybe Player) -> [HexMove]
allMoves pr p st
| win st (boardsize pr) (1 - p) = []
| otherwise = map fst $ filter ((== Nothing) . snd) $ assocs st
move :: Properties -> HexMove -> (Player, Hex) -> (Player, Hex)
move _pr place (p, Hex s) = (1 - p, Hex $ s // [(place, Just p)])
win :: Array (Int, Int) (Maybe Player) -> Int -> Player -> Bool
win st bsz 0 = any ((== bsz - 1) . snd) $ floodfill st 0 (zip [0 .. bsz - 1] [-1, -1 ..])
win st bsz 1 = any ((== bsz - 1) . fst) $ floodfill st 1 (zip [-1, -1 ..] [0 .. bsz - 1])
win _ _ _ = error "win: Unexpected value"
floodfill :: Array (Int, Int) (Maybe Player) -> Player -> [(Int, Int)] -> [(Int, Int)]
floodfill st p togo = floodfill_ p togo []
where
floodfill_ :: Player -> [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
floodfill_ _ [] known = known
floodfill_ p' (f : togo') known = let new' = filter (not . flip elem known) $ map (f +-) steps
good = filter (\f' -> st ! f' == Just p') $ filter (inRange (bounds st)) new'
in floodfill_ p' (togo' ++ good) (known ++ good)
steps :: [(Int, Int)]
steps = [(1, 1), (1, 0), (0, -1), (-1, -1), (-1, 0), (0, 1)]
{-
jumps :: [((Int, Int), [(Int, Int)])]
jumps = [ (( 2, 1), [( 1, 0), ( 1, 1)])
, (( 1, 2), [( 0, 1), ( 1, 1)])
, ((-1, 1), [( 0, 1), (-1, 0)])
, ((-2, -1), [(-1, 0), (-1, -1)])
, ((-1, -2), [( 0, -1), (-1, -1)])
, (( 1, -1), [( 1, 0), ( 0, -1)])
]
-}
{- the hexboard internally look like this:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
j * * * i
* * * *
2 * 2
1 1
0
-}