From 0f9545c64ab57d87db0dc64ee473dc3c9ba352df Mon Sep 17 00:00:00 2001 From: Jack Harley Date: Thu, 28 Jan 2021 22:59:02 +0000 Subject: [PATCH] Failed attempt at IORef --- src/Main.hs | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index 692c292..c4f166e 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -2,6 +2,7 @@ module Main where import Control.Monad import Control.Concurrent (threadDelay) +import Data.IORef import System.Random @@ -22,25 +23,42 @@ setup w = void $ do UI.addStyleSheet w "minesweeper.css" rng <- liftIO newStdGen - let board = uncover (createBoard 30 0.15 rng) (4,4) + + iob <- liftIO $ newIORef (createBoard 30 0.15 rng :: Board) + b <- liftIO $ readIORef iob getBody w #+ [ UI.div #. "container" #+ [ UI.h1 #+ [string "Minesweeper"], - --UI.h4 #+ [string "By Jack Harley "], - gameGridTable board + UI.h4 #+ [string "Jack Harley jharley@tcd.ie"], + mkElement "table" #+ rows iob 0 ]] -gameGridTable :: Board -> UI Element -gameGridTable b = mkElement "table" #+ rows b +rows :: IORef Board -> Int -> [UI Element] +rows iob r = do + b <- liftIO $ readIORef iob -rows :: Board -> [UI Element] -rows b = rows' b 0 -rows' b r | r < size b = (mkElement "tr" #+ cells b r) : rows' b (r+1) - | otherwise = [] + rows' iob b r -cells :: Board -> Int -> [UI Element] -cells b r = cells' b r 0 -cells' b r c | c < size b = mkElement "td" #. (squareBgColour b (r,c) ++ " " ++ squareTextColour b (r,c)) - #+ [string $ squareAscii b (r,c)] : cells' b r (c+1) - | otherwise = [] \ No newline at end of file + where + rows' iob b r | r < size b = (mkElement "tr" #+ cells iob b r 0) : rows' iob b (r+1) + | otherwise = [] + +cells :: IORef Board -> Board -> Int -> Int -> [UI Element] +cells iob b r c = do + cells' iob b r c + + where + cells' iob b r c | c < size b = cell iob b (r,c) : cells' iob b r (c+1) + | otherwise = [] + + +cell :: IORef Board -> Board -> Square -> UI Element +cell iob b (r,c) = do + cell <- mkElement "td" #. (squareBgColour b (r,c) ++ " " ++ squareTextColour b (r,c)) + #+ [string $ squareAscii b (r,c)] + + on UI.click cell $ \_ -> do + liftIO $ modifyIORef' iob $ \oldB -> uncover oldB (r,c) + + return cell \ No newline at end of file