Nice implementation but turns out i misunderstood minesweeper, changes up next
This commit is contained in:
parent
270ccdb026
commit
2f1dc1f658
|
@ -22,7 +22,10 @@ setup w = void $ do
|
|||
UI.addStyleSheet w "minesweeper.css"
|
||||
|
||||
rng <- liftIO newStdGen
|
||||
let board = createBoard 10 0.2 rng
|
||||
--let board = createBoard 10 0.2 rng
|
||||
let board = uncover (createBoard 10 0.2 rng) (4,4)
|
||||
--let board = uncoverSafeAdjacents board (4,4)
|
||||
--let board = uncoverSafe board [(3,4)]
|
||||
|
||||
getBody w #+ [
|
||||
UI.div #. "container" #+ [
|
||||
|
|
|
@ -34,13 +34,13 @@ seedList rng p (l:ls) = (newBool : seedList2 newRng p ls, newRng)
|
|||
seedList2 :: StdGen -> Float -> [Bool] -> [Bool]
|
||||
seedList2 _ _ [] = []
|
||||
seedList2 rng p (l:ls) = newBool : seedList2 newRng p ls
|
||||
where (newBool, newRng) = randomlyTrue rng p
|
||||
where (newBool, newRng) = randomlyTrue rng p
|
||||
|
||||
randomlyTrue :: StdGen -> Float -> (Bool, StdGen)
|
||||
randomlyTrue rng p = (generatedFloat <= p, newRng)
|
||||
where (generatedFloat, newRng) = randomR (0.0, 1.0) rng
|
||||
|
||||
-- Functions for determing status of a square
|
||||
-- Functions for determing statuses and info on square(s)
|
||||
-- N.B. (r,c) = (row, column)
|
||||
|
||||
hasMine :: Board -> Square -> Bool
|
||||
|
@ -48,7 +48,8 @@ hasMine b (r,c) | validSquare b (r,c) = (mines b !! r) !! c
|
|||
| otherwise = False
|
||||
|
||||
isUncovered :: Board -> Square -> Bool
|
||||
isUncovered b (r,c) = (uncovered b !! r) !! c
|
||||
isUncovered b (r,c) | validSquare b (r,c) = (uncovered b !! r) !! c
|
||||
| otherwise = False
|
||||
|
||||
isFlagged :: Board -> Square -> Bool
|
||||
isFlagged b (r,c) = (flagged b !! r) !! c
|
||||
|
@ -57,16 +58,22 @@ validSquare :: Board -> Square -> Bool
|
|||
validSquare b (r,c) = r >= 0 && c >= 0 && r < size b && c < size b
|
||||
|
||||
squareAscii :: Board -> Square -> String
|
||||
squareAscii b (r,c) | hasMine b (r,c) = "X"
|
||||
| otherwise = show $ adjacentBombs b (r,c)
|
||||
squareAscii b (r,c) | isUncovered b (r,c) && hasMine b (r,c) = "X"
|
||||
| isUncovered b (r,c) = show $ adjacentBombs b (r,c) -- remove this for production
|
||||
| adjacentToUncovered b (r,c) = show $ adjacentBombs b (r,c)
|
||||
| otherwise = ""
|
||||
|
||||
squareColour :: Board -> Square -> String
|
||||
squareColour b (r,c) | hasMine b (r,c) = "red"
|
||||
| otherwise = "green"
|
||||
squareColour b (r,c) | isUncovered b (r,c) && hasMine b (r,c) = "bomb"
|
||||
| isUncovered b (r,c) = "uncovered"
|
||||
| otherwise = "covered"
|
||||
|
||||
adjacentBombs :: Board -> Square -> Int
|
||||
adjacentBombs b (r,c) = sum $ map (boolToInt . hasMine b) $ adjacentSquares (r,c)
|
||||
|
||||
adjacentToUncovered :: Board -> Square -> Bool
|
||||
adjacentToUncovered b (r,c) = any (isUncovered b) $ adjacentSquares (r,c)
|
||||
|
||||
adjacentSquares :: Square -> [Square]
|
||||
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)]
|
||||
|
||||
|
@ -74,6 +81,27 @@ boolToInt :: Bool -> Int
|
|||
boolToInt x | x = 1
|
||||
| otherwise = 0
|
||||
|
||||
-- Functions for changing the status of square(s)
|
||||
|
||||
uncover :: Board -> Square -> Board
|
||||
uncover b (r,c) | not $ validSquare b (r,c) = b
|
||||
| isUncovered b (r,c) = b
|
||||
| otherwise = let Board s m u f = b
|
||||
(rowsA, row : rowsB) = splitAt r u
|
||||
(cellsA, _ : cellsB) = splitAt c row
|
||||
newRow = cellsA ++ True : cellsB
|
||||
newRows = rowsA ++ newRow : rowsB
|
||||
in uncoverSafeAdjacents (Board s m newRows f) (r,c)
|
||||
|
||||
uncoverSafeAdjacents :: Board -> Square -> Board
|
||||
uncoverSafeAdjacents b (r,c) | adjacentBombs b (r,c) == 0 = uncoverSafe b $ adjacentSquares (r,c)
|
||||
| otherwise = b
|
||||
|
||||
uncoverSafe :: Board -> [Square] -> Board
|
||||
uncoverSafe b [] = b
|
||||
uncoverSafe b ((r,c):xs) | adjacentBombs b (r,c) == 0 = uncoverSafe (uncover b (r,c)) xs
|
||||
| otherwise = uncoverSafe b xs
|
||||
|
||||
-- Functions for turning a board into a string for debug purposes
|
||||
|
||||
printBoard :: Board -> String
|
||||
|
|
|
@ -10,10 +10,14 @@ td {
|
|||
border: 1px solid black;
|
||||
}
|
||||
|
||||
.red {
|
||||
.bomb {
|
||||
background-color: red;
|
||||
}
|
||||
|
||||
.green {
|
||||
background-color: lime;
|
||||
.covered {
|
||||
background-color: darkgray;
|
||||
}
|
||||
|
||||
.uncovered {
|
||||
background-color: lightgray;
|
||||
}
|
Loading…
Reference in New Issue