Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Random
- -- Define a type alias for the grid as a 2D list of characters
- type Grid = [[Char]]
- -- Define a data type for simulation parameters
- data SimulationParameters = SimulationParameters
- { simulationWidth :: Int -- Width of the simulation area
- , simulationHeight :: Int -- Height of the simulation area
- , gridWidth :: Int -- Width of the grid
- , gridHeight :: Int -- Height of the grid
- , lightIntensity :: Int -- Intensity of the light
- , numberOfObstacles :: Int -- Number of obstacles
- }
- -- Initialize an empty grid filled with spaces
- emptyGrid :: Int -> Int -> Grid
- emptyGrid width height = replicate height (replicate width ' ')
- -- Define the position of the light source
- lightSource :: (Int, Int)
- lightSource = (5, 5)
- -- Linear Congruential Generator (LCG) parameters
- a :: Int
- a = 1664525
- c :: Int
- c = 1013904223
- m :: Int
- m = 2147483647
- -- Seed for the LCG
- seed :: Int
- seed = 123456789
- -- Custom random number generator based on LCG
- rand :: Int -> Int
- rand s = (a * s + c) `mod` m
- -- Function to add obstacles (stones) at random positions
- addObstacles :: Grid -> Int -> Int -> Int -> Grid
- addObstacles grid 0 _ _ = grid
- addObstacles grid n width height = do
- let x = rand seed `mod` width
- y = rand (rand seed) `mod` height
- updatedGrid = replaceElement grid x y '#'
- addObstacles updatedGrid (n - 1) width height
- -- Function to replace an element at a specific position in the grid
- replaceElement :: Grid -> Int -> Int -> Char -> Grid
- replaceElement grid x y newElement =
- take y grid ++
- [take x (grid !! y) ++ [newElement] ++ drop (x + 1) (grid !! y)] ++
- drop (y + 1) grid
- -- Function to spread light from the source
- spreadLight :: Grid -> (Int, Int) -> Int -> Grid
- spreadLight grid _ 0 = grid
- spreadLight grid (x, y) intensity
- | x < 0 || x >= gridWidthParam || y < 0 || y >= gridHeightParam = grid
- | grid !! y !! x /= ' ' && grid !! y !! x /= '#' = grid
- | otherwise =
- let updatedGrid = replaceElement grid x y '*' -- Place light at (x, y)
- in foldl (\g (dx, dy) -> spreadLight g (x + dx, y + dy) (intensity - 1))
- updatedGrid [(1, 0), (-1, 0), (0, 1), (0, -1)] -- Recursively spread light
- -- Function to round positions of objects in the grid with real numbers
- roundGridPositions :: Grid -> Grid
- roundGridPositions grid = map (map (\c -> if c == '*' || c == '#' then '#' else ' ')) grid
- -- Main function
- main :: IO ()
- main = do
- let parameters = SimulationParameters
- { simulationWidth = 20
- , simulationHeight = 10
- , gridWidth = 20
- , gridHeight = 10
- , lightIntensity = 5
- , numberOfObstacles = 30
- }
- gridWidthParam = gridWidth parameters
- gridHeightParam = gridHeight parameters
- let emptyGridFunction = emptyGrid gridWidthParam gridHeightParam
- gridWithObstacles = addObstacles emptyGridFunction (numberOfObstacles parameters) gridWidthParam gridHeightParam
- finalGrid = spreadLight gridWithObstacles lightSource (lightIntensity parameters)
- roundedGrid = roundGridPositions finalGrid
- mapM_ putStrLn roundedGrid
Advertisement
Add Comment
Please, Sign In to add comment