-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhgol.hs
More file actions
68 lines (62 loc) · 2.1 KB
/
Copy pathhgol.hs
File metadata and controls
68 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Control.Concurrent (threadDelay)
import Control.Monad (replicateM)
import System.Random (mkStdGen, random)
type Grid = [[Int]]
emptyGrid :: Int -> Int -> Float -> IO Grid
emptyGrid n m p = do
let emptyCell (i, j)
| rnd < p = 1
| otherwise = 0
where
rnd = head (randoms (mkStdGen (i*m+j))) :: Float
randoms g = let (x,g') = random g in x : randoms g'
return [[emptyCell (i, j) | j <- [0..m-1]] | i <- [0..n-1]]
-- Print a grid to the console
printGrid :: Grid -> IO ()
printGrid grid = do
putStr "\ESC[2J" -- Clear the screen
putStr "\ESC[0;0H" -- Move the cursor to the top left corner
putStrLn (unlines [concat [cellToStr (grid !! i !! j) | j <- [0..m-1]] | i <- [0..n-1]])
where
n = length grid
m = length (head grid)
cellToStr 0 = " " -- Dead cell
cellToStr 1 = "O" -- Live cell
-- Compute the next generation of a grid
nextGen :: Grid -> Grid
nextGen grid = [[cellState (grid !! i !! j) (liveNeighbors (i, j)) | j <- [0..m-1]] | i <- [0..n-1]]
where
n = length grid
m = length (head grid)
liveNeighbors (i, j) = sum [grid !! i' !! j' | i' <- [i-1..i+1], j' <- [j-1..j+1], (i', j') /= (i, j), i' >= 0, i' < n, j' >= 0, j' < m]
cellState 1 n | n < 2 || n > 3 = 0 -- Live cell dies
cellState 1 _ = 1 -- Live cell survives
cellState 0 3 = 1 -- Dead cell becomes alive
cellState 0 _ = 0 -- Dead cell remains dead
-- Simulate the Game of Life
-- time t in milisseconds
simulate :: Grid -> Float -> Int -> IO ()
simulate grid p t = loop grid
where
loop g = do
printGrid g
threadDelay t
loop (nextGen g)
n = length grid
m = length (head grid)
emptyCell (i, j)
| rnd < p = 1
| otherwise = 0
where
rnd = head (randoms (mkStdGen (i*m+j))) :: Float
emptyGrid n m p = [[emptyCell (i, j) | j <- [0..m-1]] | i <- [0..n-1]]
randoms g = let (x,g') = random g in x : randoms g'
-- Entry point of the program
main :: IO ()
main = do
let x = 25
y = 49
r = 0.5
t = 1000000 -- Pause for 1 second (time t in milisseconds)
grid <- emptyGrid x y r
simulate grid r t