Better implementation of adjacent bombs
This commit is contained in:
parent
cda669bcc4
commit
f828182551
|
@ -22,7 +22,7 @@ setup rng w = void $ do
|
||||||
UI.addStyleSheet w "bootstrap.min.css"
|
UI.addStyleSheet w "bootstrap.min.css"
|
||||||
UI.addStyleSheet w "minesweeper.css"
|
UI.addStyleSheet w "minesweeper.css"
|
||||||
|
|
||||||
let board = createBoard 10 0.06 rng
|
let board = createBoard 10 0.2 rng
|
||||||
|
|
||||||
getBody w #+ [
|
getBody w #+ [
|
||||||
UI.div #. "container" #+ [
|
UI.div #. "container" #+ [
|
||||||
|
|
|
@ -65,15 +65,10 @@ squareColour b (r,c) | hasMine b (r,c) = "red"
|
||||||
| otherwise = "green"
|
| otherwise = "green"
|
||||||
|
|
||||||
adjacentBombs :: Board -> Square -> Int
|
adjacentBombs :: Board -> Square -> Int
|
||||||
adjacentBombs board (row,col) = let tl = boolToInt $ hasMine board (row-1,col-1)
|
adjacentBombs b (r,c) = sum $ map (boolToInt . hasMine b) $ adjacentSquares (r,c)
|
||||||
t = boolToInt $ hasMine board (row-1,col)
|
|
||||||
tr = boolToInt $ hasMine board (row-1,col+1)
|
adjacentSquares :: Square -> [Square]
|
||||||
l = boolToInt $ hasMine board (row,col-1)
|
adjacentSquares (r,c) = [(r-1,c-1), (r-1,c), (r-1,c+1), (r,c-1), (r,c+1), (r+1,c-1), (r+1,c), (r+1,c+1)]
|
||||||
r = boolToInt $ hasMine board (row,col+1)
|
|
||||||
bl = boolToInt $ hasMine board (row+1,col-1)
|
|
||||||
b = boolToInt $ hasMine board (row+1,col)
|
|
||||||
br = boolToInt $ hasMine board (row+1,col+1)
|
|
||||||
in tl + t + tr + l + r + bl + b + br
|
|
||||||
|
|
||||||
boolToInt :: Bool -> Int
|
boolToInt :: Bool -> Int
|
||||||
boolToInt x | x = 1
|
boolToInt x | x = 1
|
||||||
|
|
Loading…
Reference in New Issue