Forest fire: Difference between revisions

Content deleted Content added
Wherrera (talk | contribs)
Hout (talk | contribs)
m →‎{{header|Haskell}}: Dropped one (now redundant) import.
Line 4,616: Line 4,616:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import System.Random (randomRIO)
<lang haskell>import Control.Monad (replicateM, unless)
import Data.List (tails, transpose)
import Data.List (tails, transpose)
import Control.Monad (liftM2, replicateM, unless)
import System.Random (randomRIO)


data Cell
data Cell
Line 4,625: Line 4,625:
| Fire
| Fire
deriving (Eq)
deriving (Eq)

instance Show Cell where
instance Show Cell where
show Empty = " "
show Empty = " "
show Tree = "T"
show Tree = "T"
show Fire = "$"
show Fire = "$"

randomCell :: IO Cell
randomCell :: IO Cell
randomCell = fmap ([Empty, Tree] !!) (randomRIO (0, 1) :: IO Int)
randomCell = fmap ([Empty, Tree] !!) (randomRIO (0, 1) :: IO Int)

randomChance :: IO Double
randomChance :: IO Double
randomChance = randomRIO (0, 1.0) :: IO Double
randomChance = randomRIO (0, 1.0) :: IO Double

rim :: a -> [[a]] -> [[a]]
rim :: a -> [[a]] -> [[a]]
rim b = fmap (fb b) . (fb =<< rb)
rim b = fmap (fb b) . (fb =<< rb)
where
where
fb = liftM2 (.) (:) (flip (++) . return)
fb = (.) <$> (:) <*> (flip (++) . return)
rb = fst . unzip . zip (repeat b) . head
rb = fst . unzip . zip (repeat b) . head

take3x3 :: [[a]] -> [[[a]]]
take3x3 :: [[a]] -> [[[a]]]
take3x3 = concatMap (transpose . fmap take3) . take3
take3x3 = concatMap (transpose . fmap take3) . take3
where
where
take3 = init . init . takeWhile (not . null) . fmap (take 3) . tails
take3 = init . init . takeWhile (not . null) . fmap (take 3) . tails

list2Mat :: Int -> [a] -> [[a]]
list2Mat :: Int -> [a] -> [[a]]
list2Mat n = takeWhile (not . null) . fmap (take n) . iterate (drop n)
list2Mat n = takeWhile (not . null) . fmap (take n) . iterate (drop n)

evolveForest :: Int -> Int -> Int -> IO ()
evolveForest :: Int -> Int -> Int -> IO ()
evolveForest m n k = do
evolveForest m n k = do
Line 4,672: Line 4,672:
nfs >>= evolve (i + 1)
nfs >>= evolve (i + 1)
evolve 1 fs
evolve 1 fs

main :: IO ()
main :: IO ()
main = evolveForest 6 50 3</lang>
main = evolveForest 6 50 3</lang>