Skip to content

Commit 69c32c4

Browse files
committed
Adding parser and gospers glider gun to be parsed by default
1 parent 9174118 commit 69c32c4

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

gospersGliderGun.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
............................................
2+
............................ ...............
3+
.......................... . ...............
4+
................ ...... ............ ....
5+
............... ... .... ............ ....
6+
.... ........ ..... ... ..................
7+
.... ........ ... . .... . ...............
8+
.............. ..... ....... ...............
9+
............... ... ........................
10+
................ ..........................
11+
............................................

src/Life.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Life
77
, newWorldWithCells
88
, size
99
, evolve
10-
, setCellAt
10+
, updateCells
1111
, cellAt
1212
, cells
1313
, neighboringAddresses
@@ -27,7 +27,7 @@ instance Show World where
2727
show world = "World " ++ (show $ size world)
2828

2929
newWorld :: Address -> World
30-
newWorld worldSize = newWorldWithCells worldSize (cycle [Dead, Alive])
30+
newWorld worldSize = newWorldWithCells worldSize (cycle [Dead])
3131

3232
newWorldWithCells :: Address -> [Cell] -> World
3333
newWorldWithCells (width, height) cells = World $ listArray ((0,0), (width-1, height-1)) cells
@@ -40,8 +40,8 @@ evolve :: World -> World
4040
evolve world@(World ary) = World $ array (bounds ary) (map newCellAt $ indices ary)
4141
where newCellAt ix = (ix, fate (cellAt world ix) (map (cellAt world) (neighboringAddresses world ix)))
4242

43-
setCellAt :: World -> Address -> Cell -> World
44-
setCellAt w _ _ = w
43+
updateCells:: World -> [(Address, Cell)] -> World
44+
updateCells (World ary) updates = World $ (ary // updates)
4545

4646
cellAt :: World -> Address -> Cell
4747
cellAt (World ary) ix = ary ! ix

src/Pattern.hs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Pattern
2+
( Pattern
3+
, loadPattern
4+
, drawPatternAt
5+
)
6+
where
7+
8+
import Data.List
9+
import Life
10+
11+
data Pattern = Pattern ![(Address, Cell)] deriving (Show)
12+
13+
loadPattern :: FilePath -> IO Pattern
14+
loadPattern filePath = do
15+
fileContents <- readFile filePath
16+
let parseChar '.' = Dead
17+
parseChar ' ' = Alive
18+
mapCell y x cell = (x+1, ((x,y), parseChar cell))
19+
mapRow y row = (y+1, snd $ mapAccumL (mapCell y) 0 row)
20+
patternCells = snd $ mapAccumL mapRow 0 (lines fileContents)
21+
return $ Pattern (concat patternCells)
22+
23+
drawPatternAt :: Address -> Pattern -> World -> World
24+
drawPatternAt (xOff, yOff) (Pattern updates) world = updateCells world (map offset updates)
25+
where offset ((x, y), cell) = ((x + xOff, y + yOff), cell)
26+

src/WebApp.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ import qualified Text.Blaze.Html5 as H
1919

2020
import Life
2121
import Timeline
22+
import Pattern
2223

2324
main :: IO ()
2425
main = do
25-
timeline <- newTimeline (30, 30)
26+
timeline <- newTimeline (50, 30)
27+
pattern <- loadPattern "gospersGliderGun.txt"
28+
interfere (drawPatternAt (0, 0) pattern) timeline
2629
quickServer $
2730
ifTop (writeBS "hello world") <|>
2831
route [ ("echo/:echoparam", echoHandler)

0 commit comments

Comments
 (0)