Guest User

Untitled

a guest
Sep 7th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2.  
  3. -- Define a type alias for the grid as a 2D list of characters
  4. type Grid = [[Char]]
  5.  
  6. -- Define a data type for simulation parameters
  7. data SimulationParameters = SimulationParameters
  8.   { simulationWidth :: Int       -- Width of the simulation area
  9.   , simulationHeight :: Int      -- Height of the simulation area
  10.   , gridWidth :: Int             -- Width of the grid
  11.   , gridHeight :: Int            -- Height of the grid
  12.   , lightIntensity :: Int        -- Intensity of the light
  13.   , numberOfObstacles :: Int     -- Number of obstacles
  14.   }
  15.  
  16. -- Initialize an empty grid filled with spaces
  17. emptyGrid :: Int -> Int -> Grid
  18. emptyGrid width height = replicate height (replicate width ' ')
  19.  
  20. -- Define the position of the light source
  21. lightSource :: (Int, Int)
  22. lightSource = (5, 5)
  23.  
  24. -- Linear Congruential Generator (LCG) parameters
  25. a :: Int
  26. a = 1664525
  27.  
  28. c :: Int
  29. c = 1013904223
  30.  
  31. m :: Int
  32. m = 2147483647
  33.  
  34. -- Seed for the LCG
  35. seed :: Int
  36. seed = 123456789
  37.  
  38. -- Custom random number generator based on LCG
  39. rand :: Int -> Int
  40. rand s = (a * s + c) `mod` m
  41.  
  42. -- Function to add obstacles (stones) at random positions
  43. addObstacles :: Grid -> Int -> Int -> Int -> Grid
  44. addObstacles grid 0 _ _ = grid
  45. addObstacles grid n width height = do
  46.   let x = rand seed `mod` width
  47.       y = rand (rand seed) `mod` height
  48.       updatedGrid = replaceElement grid x y '#'
  49.   addObstacles updatedGrid (n - 1) width height
  50.  
  51. -- Function to replace an element at a specific position in the grid
  52. replaceElement :: Grid -> Int -> Int -> Char -> Grid
  53. replaceElement grid x y newElement =
  54.   take y grid ++
  55.   [take x (grid !! y) ++ [newElement] ++ drop (x + 1) (grid !! y)] ++
  56.   drop (y + 1) grid
  57.  
  58. -- Function to spread light from the source
  59. spreadLight :: Grid -> (Int, Int) -> Int -> Grid
  60. spreadLight grid _ 0 = grid
  61. spreadLight grid (x, y) intensity
  62.   | x < 0 || x >= gridWidthParam || y < 0 || y >= gridHeightParam = grid
  63.   | grid !! y !! x /= ' ' && grid !! y !! x /= '#' = grid
  64.   | otherwise =
  65.     let updatedGrid = replaceElement grid x y '*' -- Place light at (x, y)
  66.     in foldl (\g (dx, dy) -> spreadLight g (x + dx, y + dy) (intensity - 1))
  67.              updatedGrid [(1, 0), (-1, 0), (0, 1), (0, -1)] -- Recursively spread light
  68.  
  69. -- Function to round positions of objects in the grid with real numbers
  70. roundGridPositions :: Grid -> Grid
  71. roundGridPositions grid = map (map (\c -> if c == '*' || c == '#' then '#' else ' ')) grid
  72.  
  73. -- Main function
  74. main :: IO ()
  75. main = do
  76.   let parameters = SimulationParameters
  77.         { simulationWidth = 20
  78.         , simulationHeight = 10
  79.         , gridWidth = 20
  80.         , gridHeight = 10
  81.         , lightIntensity = 5
  82.         , numberOfObstacles = 30
  83.         }
  84.       gridWidthParam = gridWidth parameters
  85.       gridHeightParam = gridHeight parameters
  86.  
  87.   let emptyGridFunction = emptyGrid gridWidthParam gridHeightParam
  88.       gridWithObstacles = addObstacles emptyGridFunction (numberOfObstacles parameters) gridWidthParam gridHeightParam
  89.       finalGrid = spreadLight gridWithObstacles lightSource (lightIntensity parameters)
  90.       roundedGrid = roundGridPositions finalGrid
  91.   mapM_ putStrLn roundedGrid
  92.  
Advertisement
Add Comment
Please, Sign In to add comment