import System.Random type Grid = [[Char]] gridWidth :: Int gridWidth = 20 gridHeight :: Int gridHeight = 10 -- Initialize the grid with empty spaces emptyGrid :: Grid emptyGrid = replicate gridHeight (replicate gridWidth ' ') -- Define the light source position lightSource :: (Int, Int) lightSource = (5, 5) -- Add obstacles (stones) at random positions addObstacles :: Grid -> Int -> IO Grid addObstacles grid 0 = return grid addObstacles grid n = do x <- randomRIO (0, gridWidth - 1) y <- randomRIO (0, gridHeight - 1) let updatedGrid = replaceElement grid x y '#' addObstacles updatedGrid (n - 1) -- Replace an element at the specified position replaceElement :: Grid -> Int -> Int -> Char -> Grid replaceElement grid x y newElem = take y grid ++ [take x (grid !! y) ++ [newElem] ++ drop (x + 1) (grid !! y)] ++ drop (y + 1) grid -- Spread light from the source spreadLight :: Grid -> (Int, Int) -> Int -> Grid spreadLight grid _ 0 = grid spreadLight grid (x, y) intensity | x < 0 || x >= gridWidth || y < 0 || y >= gridHeight = grid | grid !! y !! x /= ' ' && grid !! y !! x /= '#' = grid | otherwise = let updatedGrid = replaceElement grid x y '*' in foldl (\g (dx, dy) -> spreadLight g (x + dx, y + dy) (intensity - 1)) updatedGrid [(1, 0), (-1, 0), (0, 1), (0, -1)] main :: IO () main = do gridWithObstacles <- addObstacles emptyGrid 30 let finalGrid = spreadLight gridWithObstacles lightSource 5 mapM_ putStrLn finalGrid