diff --git a/src/Main.hs b/src/Main.hs index c4f166e..4ef15ea 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -3,6 +3,7 @@ module Main where import Control.Monad import Control.Concurrent (threadDelay) import Data.IORef +import Data.Maybe import System.Random @@ -24,41 +25,41 @@ setup w = void $ do rng <- liftIO newStdGen - iob <- liftIO $ newIORef (createBoard 30 0.15 rng :: Board) - b <- liftIO $ readIORef iob + let b = createBoard 30 0.15 rng :: Board + iob <- liftIO $ newIORef b getBody w #+ [ - UI.div #. "container" #+ [ + UI.div #. "container" # set UI.id_ "cont" #+ [ UI.h1 #+ [string "Minesweeper"], UI.h4 #+ [string "Jack Harley jharley@tcd.ie"], - mkElement "table" #+ rows iob 0 + mkElement "table" # set UI.id_ "grid" #+ rows iob b 0 ]] -rows :: IORef Board -> Int -> [UI Element] -rows iob r = do - b <- liftIO $ readIORef iob - - rows' iob b r - where - rows' iob b r | r < size b = (mkElement "tr" #+ cells iob b r 0) : rows' iob b (r+1) - | otherwise = [] + 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 + cells iob b r c | c < size b = cell iob b (r,c) : cells iob b r (c+1) + | otherwise = [] + + cell iob b (r,c) = do + cell <- mkElement "td" #. (squareBgColour b (r,c) ++ " " ++ squareTextColour b (r,c)) + #+ [string $ squareAscii b (r,c)] - where - cells' iob b r c | c < size b = cell iob b (r,c) : cells' iob b r (c+1) - | otherwise = [] + on UI.click cell $ \_ -> do + liftIO $ modifyIORef' iob $ \oldB -> uncover oldB (r,c) + b <- liftIO $ readIORef iob + + cont <- getElementById w "cont" + let cont' = fromJust cont + + getBody w #+ [ + UI.div #. "container" # set UI.id_ "cont" # set children [] #+ [ + UI.h1 #+ [string "Minesweeper"], + UI.h4 #+ [string "Jack Harley jharley@tcd.ie"], + mkElement "table" # set UI.id_ "grid" #+ rows iob b 0 + ]] + delete cont' -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 + return cell \ No newline at end of file