-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.hs
124 lines (108 loc) · 3.27 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
module Board where
import Control.Monad.Random
( Rand
, RandomGen
, getRandomR
, getStdGen
, runRand
, split
)
import Control.Monad.State
import Data.Map (Map, (!), elems, fromList, insert, toList)
import Msg
import Pos
w :: Int
w = 40
h :: Int
h = 30
data Cell = Cell
{ mined :: Bool
, exposed :: Bool
, flagged :: Bool
, mineCount :: Int
} deriving (Show, Eq)
type Board = Map Pos Cell
mkCell :: RandomGen g => Rand g Cell
mkCell = do
t <- getRandomR (0.0 :: Float, 1.0)
return $ Cell (t < 0.201) False False 0
initBoard :: RandomGen g => [Pos] -> Rand g Board
initBoard positions = do
cells <- sequence $ take (length positions) (repeat mkCell)
return $ fromList (zip positions cells)
mkBoard :: RandomGen g => Rand g Board
mkBoard = do
let positions = [(x, y) | x <- [0 .. w - 1], y <- [0 .. h - 1]]
initBoard positions
adjacents :: Pos -> [Pos]
adjacents (x, y) =
[ (xx, yy)
| xx <- [x - 1 .. x + 1]
, yy <- [y - 1 .. y + 1]
, (xx, yy) /= (x, y)
, xx >= 0
, yy >= 0
, xx < w
, yy < h
]
exposeMines :: State Board [(Pos, Maybe Cell)]
exposeMines = do
board <- get
let toExpose =
filter (\(pos, cell) -> (not . exposed) cell && mined cell) $
toList board
modifications =
fmap (\(p, c) -> (p, Just $ c {exposed = True})) toExpose
put $ foldl (\b (p, Just c) -> insert p c b) board modifications
return modifications
exposeSelection :: Pos -> Cell -> Int -> State Board [(Pos, Maybe Cell)]
exposeSelection pos cell count = do
board <- get
let cell = board ! pos
toExpose =
if flagged cell
then []
else [(pos, cell)]
modifications =
fmap
(\(p, c) -> (p, Just $ c {exposed = True, mineCount = count}))
toExpose
put $ foldl (\b (p, Just c) -> insert p c b) board modifications
return modifications
exposeCells :: Pos -> State Board [(Pos, Maybe Cell)]
exposeCells pos = do
board <- get
let cell@(Cell m e f mc) = board ! pos
indices = adjacents pos
count = length $ filter mined $ fmap (board !) indices
checkList =
if m || e || f || count /= 0
then []
else indices
exposedSelection <- exposeSelection pos cell count
exposedNeighbors <- mapM exposeCells checkList
exposedMines <-
if m
then exposeMines
else return []
return $ exposedSelection ++ concat exposedNeighbors ++ exposedMines
flagCell :: Pos -> State Board [(Pos, Maybe Cell)]
flagCell pos = do
board <- get
let cell = board ! pos
modifications =
if exposed cell
then [] -- can't flag a cell that's already exposed.
else [(pos, Just $ cell {flagged = not $ flagged cell})]
put $ foldl (\b (p, Just c) -> insert p c b) board modifications
return modifications
gameOver :: Board -> Bool
gameOver = any (\cell -> exposed cell && mined cell)
updateBoard :: Msg -> State Board [(Pos, Maybe Cell)]
updateBoard msg = do
board <- get
if gameOver board
then return []
else case msg of
LeftPick pos -> exposeCells pos
RightPick pos -> flagCell pos