diff --git a/src/Types/GameBoard.hs b/src/Types/GameBoard.hs index 1b81e3f..fa16b45 100644 --- a/src/Types/GameBoard.hs +++ b/src/Types/GameBoard.hs @@ -9,6 +9,7 @@ module Types.GameBoard where import Data.Array import Data.Binary.Builder (append) import Data.Maybe (mapMaybe) +import Data.Tuple.Extra import Display (Display, display) import System.Random.Stateful (RandomGen) import Types.BoardPosition @@ -34,21 +35,17 @@ select :: (GameMode mode) => BoardPosition -> BoardPosition -> GameBoard mode -> select topLeft bottomRight MkGameBoard{board} = mapMaybe (board !) (range (topLeft, bottomRight)) --- TODO: rewrite this with monadic RNG -newRandomBoard :: (RandomGen g, GameMode mode) => g -> mode -> Int -> Int -> (GameBoard mode, g) -newRandomBoard rng gameMode width height = - (MkGameBoard{width, height, gameMode, board = array (low, high) cellAssocList}, nextRng) +-- TODO: rewrite this with monadic RNG? +newRandomBoard :: (RandomGen g, GameMode mode) => mode -> Int -> Int -> g -> (GameBoard mode, g) +newRandomBoard gameMode width height rng = + (MkGameBoard{width, height, gameMode, board = array ixRange cellAssocList}, nextRng) where - low = (0, 0) - high = (width - 1, height - 1) + ixRange = ((0, 0), (width - 1, height - 1)) (cellAssocList, nextRng) = foldl' - ( \(cur, rng') -> \ix -> - let (res, next) = gen gameMode rng' in ((ix, Just res) : cur, next) - ) + (\(cur, rng') -> \ix -> first ((: cur) . (ix,) . Just) (gen gameMode rng')) ([], rng) - ixs - ixs = range (low, high) + (range ixRange) instance (Display (Tile mode)) => Display (GameBoard mode) where display MkGameBoard{width, height, board} =