Skip to content

Commit

Permalink
Created main function to read world/points from stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtchepak committed Dec 5, 2011
1 parent 05e4a17 commit 7e8231a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions blinker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.x.
.x.
.x.
22 changes: 19 additions & 3 deletions life.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ neighbours (Point x y) = [(Point (x-1) (y-1)), (Point x (y-1)), (Point (x+1) (y-
(Point (x-1) y), (Point (x+1) y),
(Point (x-1) (y+1)), (Point x (y+1)), (Point (x+1) (y+1))]

-- Main game entry
-- The game

life :: Int -> [Point] -> IO()
life 0 _ = return ()
Expand All @@ -44,6 +44,22 @@ life gens world = do
life (gens-1) (tick world)
where dimensions = world_dimensions world

-- Run game from stdin

main = interact (show_gens 10 . parse_points (Point 0 0))

parse_points :: Point -> [Char] -> [Point]
parse_points _ [] = []
parse_points (Point x y) (c:cs)
| c == '.' = parse_points (Point (x+1) y) cs
| c == '\n' = parse_points (Point 0 (y+1)) cs
| otherwise = (Point x y) : parse_points (Point (x+1) y) cs

show_gens :: Int -> [Point] -> [Char]
show_gens 0 _ = []
show_gens gen world = show_world world dimensions ++ "\n" ++ show_gens (gen-1) (tick world)
where dimensions = world_dimensions world

-- Display

show_world :: [Point] -> (Point, Point) -> [Char]
Expand Down Expand Up @@ -86,8 +102,8 @@ assert_equal x y
| otherwise = putStrLn ("Expected " ++ show x ++ ", got " ++ show y)


main :: IO ()
main = do
test :: IO ()
test = do
assert_equal True (is_neighbour (Point 1 1) (Point 1 2))
assert_equal False (is_neighbour (Point 1 1) (Point 1 1))
assert_equal False (is_neighbour (Point 321 1) (Point 1 1))
Expand Down

0 comments on commit 7e8231a

Please sign in to comment.