2021-01-28 14:45:23 +00:00
|
|
|
module Minesweeper where
|
|
|
|
|
|
|
|
import System.Random
|
|
|
|
|
|
|
|
type Square = (Int, Int)
|
|
|
|
type Grid = [[Bool]]
|
|
|
|
|
|
|
|
data Board = Board { size :: Int
|
|
|
|
, mines :: Grid
|
|
|
|
, uncovered :: Grid
|
|
|
|
, flagged :: Grid
|
|
|
|
} deriving (Show)
|
|
|
|
|
|
|
|
-- Creates a board given a size (width/height), mine ratio and random generator
|
|
|
|
createBoard :: Int -> Float -> StdGen -> Board
|
|
|
|
createBoard size mineRatio rng = Board size (seedGrid rng mineRatio (createGrid size)) (createGrid size) (createGrid size)
|
|
|
|
|
|
|
|
-- Creates a 2D list of booleans of given size, initialised to False
|
|
|
|
createGrid :: Int -> Grid
|
|
|
|
createGrid size = replicate size (replicate size False)
|
|
|
|
|
|
|
|
-- Functions relating to seeding a grid with mines
|
2021-01-28 19:31:39 +00:00
|
|
|
|
2021-01-28 14:45:23 +00:00
|
|
|
seedGrid :: StdGen -> Float -> Grid -> Grid
|
|
|
|
seedGrid _ _ [] = []
|
|
|
|
seedGrid rng p (l:ls) = newL : seedGrid rng2 p ls
|
|
|
|
where (rng1, rng2) = split rng
|
|
|
|
(newL, _) = seedList rng1 p l
|
|
|
|
|
|
|
|
seedList :: StdGen -> Float -> [Bool] -> ([Bool], StdGen)
|
|
|
|
seedList rng p (l:ls) = (newBool : seedList2 newRng p ls, newRng)
|
|
|
|
where (newBool, newRng) = randomlyTrue rng p
|
|
|
|
|
|
|
|
seedList2 :: StdGen -> Float -> [Bool] -> [Bool]
|
|
|
|
seedList2 _ _ [] = []
|
|
|
|
seedList2 rng p (l:ls) = newBool : seedList2 newRng p ls
|
2021-01-28 21:08:17 +00:00
|
|
|
where (newBool, newRng) = randomlyTrue rng p
|
2021-01-28 14:45:23 +00:00
|
|
|
|
|
|
|
randomlyTrue :: StdGen -> Float -> (Bool, StdGen)
|
|
|
|
randomlyTrue rng p = (generatedFloat <= p, newRng)
|
|
|
|
where (generatedFloat, newRng) = randomR (0.0, 1.0) rng
|
|
|
|
|
2021-01-28 21:08:17 +00:00
|
|
|
-- Functions for determing statuses and info on square(s)
|
2021-01-28 19:31:39 +00:00
|
|
|
-- N.B. (r,c) = (row, column)
|
|
|
|
|
|
|
|
hasMine :: Board -> Square -> Bool
|
|
|
|
hasMine b (r,c) | validSquare b (r,c) = (mines b !! r) !! c
|
|
|
|
| otherwise = False
|
|
|
|
|
|
|
|
isUncovered :: Board -> Square -> Bool
|
2021-01-28 21:08:17 +00:00
|
|
|
isUncovered b (r,c) | validSquare b (r,c) = (uncovered b !! r) !! c
|
2021-01-28 21:31:29 +00:00
|
|
|
| otherwise = True
|
2021-01-28 19:31:39 +00:00
|
|
|
|
|
|
|
isFlagged :: Board -> Square -> Bool
|
|
|
|
isFlagged b (r,c) = (flagged b !! r) !! c
|
|
|
|
|
|
|
|
validSquare :: Board -> Square -> Bool
|
|
|
|
validSquare b (r,c) = r >= 0 && c >= 0 && r < size b && c < size b
|
|
|
|
|
2021-01-28 21:31:29 +00:00
|
|
|
onEdge :: Board -> Square -> Bool
|
|
|
|
onEdge b (r,c) = r == 0 || c == 0 || r+1 == size b || c+1 == size b
|
|
|
|
|
2021-01-28 19:31:39 +00:00
|
|
|
squareAscii :: Board -> Square -> String
|
2021-01-28 21:08:17 +00:00
|
|
|
squareAscii b (r,c) | isUncovered b (r,c) && hasMine b (r,c) = "X"
|
2021-01-28 21:31:29 +00:00
|
|
|
| isUncovered b (r,c) && adjacentToCovered b (r,c) = show $ adjacentBombs b (r,c)
|
2021-01-28 21:08:17 +00:00
|
|
|
| otherwise = ""
|
2021-01-28 19:31:39 +00:00
|
|
|
|
|
|
|
squareColour :: Board -> Square -> String
|
2021-01-28 21:08:17 +00:00
|
|
|
squareColour b (r,c) | isUncovered b (r,c) && hasMine b (r,c) = "bomb"
|
|
|
|
| isUncovered b (r,c) = "uncovered"
|
|
|
|
| otherwise = "covered"
|
2021-01-28 19:31:39 +00:00
|
|
|
|
2021-01-28 21:31:29 +00:00
|
|
|
squareTextColour :: Board -> Square -> String
|
|
|
|
squareTextColour b (r,c) | isUncovered b (r,c) && adjacentToCovered b (r,c) =
|
|
|
|
case adjacentBombs b (r,c) of
|
|
|
|
1 -> "text-blue"
|
|
|
|
2 -> "text-green"
|
|
|
|
3 -> "text-red"
|
|
|
|
4 -> "text-purple"
|
|
|
|
5 -> "text-maroon"
|
|
|
|
6 -> "text-turquoise"
|
|
|
|
7 -> "text-black"
|
|
|
|
8 -> "text-gray"
|
|
|
|
| otherwise = ""
|
|
|
|
|
2021-01-28 19:31:39 +00:00
|
|
|
adjacentBombs :: Board -> Square -> Int
|
2021-01-28 19:46:46 +00:00
|
|
|
adjacentBombs b (r,c) = sum $ map (boolToInt . hasMine b) $ adjacentSquares (r,c)
|
|
|
|
|
2021-01-28 21:31:29 +00:00
|
|
|
adjacentToCovered :: Board -> Square -> Bool
|
|
|
|
adjacentToCovered b (r,c) = not $ all (isUncovered b) $ adjacentSquares (r,c)
|
2021-01-28 21:08:17 +00:00
|
|
|
|
2021-01-28 19:46:46 +00:00
|
|
|
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)]
|
2021-01-28 19:31:39 +00:00
|
|
|
|
|
|
|
boolToInt :: Bool -> Int
|
|
|
|
boolToInt x | x = 1
|
|
|
|
| otherwise = 0
|
|
|
|
|
2021-01-28 21:08:17 +00:00
|
|
|
-- 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
|
2021-01-28 21:31:29 +00:00
|
|
|
in uncoverAdjacentsIfSafe (Board s m newRows f) (r,c)
|
2021-01-28 21:08:17 +00:00
|
|
|
|
2021-01-28 21:31:29 +00:00
|
|
|
uncoverAdjacentsIfSafe :: Board -> Square -> Board
|
|
|
|
uncoverAdjacentsIfSafe b (r,c) | adjacentBombs b (r,c) == 0 = uncoverAll b $ adjacentSquares (r,c)
|
|
|
|
| otherwise = b
|
|
|
|
|
|
|
|
uncoverAll :: Board -> [Square] -> Board
|
|
|
|
uncoverAll b [] = b
|
|
|
|
uncoverAll b ((r,c):xs) = uncoverAll newB xs where newB = uncover b (r,c)
|
2021-01-28 21:08:17 +00:00
|
|
|
|
2021-01-28 14:45:23 +00:00
|
|
|
-- Functions for turning a board into a string for debug purposes
|
2021-01-28 19:31:39 +00:00
|
|
|
|
2021-01-28 14:45:23 +00:00
|
|
|
printBoard :: Board -> String
|
|
|
|
printBoard b = printBoardGrid (mines b)
|
|
|
|
|
|
|
|
printBoardGrid :: Grid -> String
|
|
|
|
printBoardGrid [] = ""
|
|
|
|
printBoardGrid (l:ls) = printBoardLine l ++ "\n" ++ printBoardGrid ls
|
|
|
|
|
|
|
|
printBoardLine :: [Bool] -> String
|
|
|
|
printBoardLine [] = ""
|
|
|
|
printBoardLine (x:xs) = (if x then " x" else " .") ++ printBoardLine xs
|