From 413893d7bc3b02504a46c09665494eae4db480d3 Mon Sep 17 00:00:00 2001 From: Jack Harley Date: Thu, 28 Jan 2021 14:45:23 +0000 Subject: [PATCH] working nicely now, split func into module --- Minesweeper.cabal | 1 + src/Main.hs | 50 ++------------------------------------------ src/Minesweeper.hs | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 48 deletions(-) create mode 100644 src/Minesweeper.hs diff --git a/Minesweeper.cabal b/Minesweeper.cabal index 35ea5a8..39e374e 100644 --- a/Minesweeper.cabal +++ b/Minesweeper.cabal @@ -16,6 +16,7 @@ build-type: Simple executable Minesweeper main-is: Main.hs other-modules: + Minesweeper Paths_Minesweeper hs-source-dirs: src diff --git a/src/Main.hs b/src/Main.hs index cd81e70..63f9d54 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,55 +1,9 @@ module Main where +import Minesweeper import System.Random main :: IO () main = do rng <- newStdGen - putStrLn (printBoard $ createBoard 12 0.2 rng) - -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) - --- Seeds True statuses to a 2D grid randomly with a given probability, requires a random generator -seedGrid :: StdGen -> Float -> Grid -> Grid -seedGrid _ _ [] = [] -seedGrid rng p (l:ls) = newL : seedGrid newRng p ls - where (newL, newRng) = seedList rng p l - -seedList :: StdGen -> Float -> [Bool] -> ([Bool], StdGen) -seedList rng p (l:ls) = (newBool : seedListDiscard newRng p ls, newRng) - where (newBool, newRng) = randomlyTrue rng p - -seedListDiscard :: StdGen -> Float -> [Bool] -> [Bool] -seedListDiscard _ _ [] = [] -seedListDiscard rng p (l:ls) = newBool : seedListDiscard newRng p ls - where (newBool, newRng) = randomlyTrue rng p - -randomlyTrue :: StdGen -> Float -> (Bool, StdGen) -randomlyTrue rng p = let (generatedFloat, newRng) = randomR (0.0, 1.0) rng - in (generatedFloat <= p, newRng) - -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 + putStrLn (printBoard $ createBoard 20 0.1 rng) \ No newline at end of file diff --git a/src/Minesweeper.hs b/src/Minesweeper.hs new file mode 100644 index 0000000..1c6c1ae --- /dev/null +++ b/src/Minesweeper.hs @@ -0,0 +1,52 @@ +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 +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 + 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 turning a board into a string for debug purposes +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 \ No newline at end of file