Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
gui returns simulation state
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-jana committed Sep 27, 2018
1 parent 9a269ae commit 48f1508
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Brain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import GUI

-- | simulate Brians Brain
main :: IO ()
main = randomSpace (50, 50) [Ready, Firing, Refractory] >>= runCellularAutomata2D brainRule
main = randomSpace (50, 50) [Ready, Firing, Refractory] >>= runCellularAutomata2D brainRule >> return ()

-- | the state of a neuron
data Neuron = Ready | Firing | Refractory deriving (Show, Eq, Bounded, Enum)
Expand Down
2 changes: 1 addition & 1 deletion Cave.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CellularAutomata2D
import GUI

main :: IO ()
main = randomSpace (100, 100) [True, False] >>= runCellularAutomata2D caveRule
main = randomSpace (100, 100) [True, False] >>= runCellularAutomata2D caveRule >> return ()

caveRule :: Rule Bool
caveRule = makeTotalMoorRule [6..8] [3..8]
Expand Down
2 changes: 1 addition & 1 deletion Forest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data Wood = Tree | Empty | Fire deriving (Show, Eq, Bounded, Enum)

-- | run the simulation
main :: IO ()
main = randomSpace (150, 150) [Empty, Tree] >>= runCellularAutomata2D forestRule
main = randomSpace (150, 150) [Empty, Tree] >>= runCellularAutomata2D forestRule >> return ()

-- | Probability of a fire in a tree if one the neighbor tree is burning
newFireProb :: Float
Expand Down
8 changes: 4 additions & 4 deletions GUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ targetScreenSize = 500
-- used to updated the space.
-- The user can press space to start and stop the simulation of the automata.
-- He can also edit the space by clicking into a cell witch goes to the next state.
runCellularAutomata2D :: Cell a => Rule a -> Torus a -> IO ()
runCellularAutomata2D :: Cell a => Rule a -> Torus a -> IO (Torus a)
runCellularAutomata2D rule space = do
-- compute our window dimensions
let (spaceHeight, spaceWidth) = getSpaceSize space
Expand Down Expand Up @@ -109,13 +109,13 @@ data SimulationState a = SimulationState
}

-- | the game loop
loop :: Cell a => SimulationState a -> IO ()
loop :: Cell a => SimulationState a -> IO (Torus a)
loop state = do
start <- SDL.getTicks
-- get an event and process it
event <- SDL.pollEvent
case event of
SDL.Quit -> SDL.quit
SDL.Quit -> SDL.quit >> return (getSpace state)
SDL.MouseButtonUp _ _ SDL.ButtonLeft -> loop state { inserting = False, inserted = [] } -- stop changing cell states
SDL.MouseMotion x y _ _
| inserting state -> insert state x y -- inserting new cells
Expand Down Expand Up @@ -159,7 +159,7 @@ loop state = do
_ -> loop state

-- | change the cell state at a given pixel coordinate
insert :: Cell a => SimulationState a -> Word16 -> Word16 -> IO ()
insert :: Cell a => SimulationState a -> Word16 -> Word16 -> IO (Torus a)
insert state x y
| not isOutside && cellIndex `notElem` inserted state =
loop state { getSpace = setCell
Expand Down
2 changes: 1 addition & 1 deletion GameOfLife.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import GUI

-- | Run a simple glider simulation.
main :: IO ()
main = runCellularAutomata2D golRule glider
main = runCellularAutomata2D golRule glider >> return ()

glider :: Torus Bool
glider = initBoolSpaceWithCells (20, 20) [(0,2),(1,2),(2,2),(2,1),(1,0)]
Expand Down
1 change: 1 addition & 0 deletions RockPaperScissors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Data.Ix
-- Simulate the rock paper scissors automaton
main :: IO ()
main = runCellularAutomata2D rockPaperScissorsRule (initSpaceWithCells (50, 50) (RPSCell White maxLives) [])
>> return ()

-- | Every cell can have a color of be white (empty)
data CellColor = Red | Green | Blue | White deriving (Show, Eq, Bounded, Enum, Ord, Ix)
Expand Down
2 changes: 1 addition & 1 deletion Seeds.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CellularAutomata2D
import GUI

main :: IO ()
main = runCellularAutomata2D seedsRule s3
main = runCellularAutomata2D seedsRule s3 >> return ()

s1, s2 :: Torus Bool
s1 = initBoolSpaceWithCells (40, 40) [(0,1),(1,1)]
Expand Down
1 change: 1 addition & 0 deletions Voting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ main :: IO ()
main =
-- randomSpace size [True, False] >>= runCellularAutomata2D anneal
randomSpace size [True, False] >>= runCellularAutomata2D banks
>> return ()

banks :: Rule Bool
banks = Rule neumannIndexDeltas $ \self neighbors ->
Expand Down
2 changes: 1 addition & 1 deletion WireWorld.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import GUI
data Wire = Empty | Conductor | ElectronHead | ElectronTail deriving (Show, Eq, Enum, Bounded)

main :: IO ()
main = runCellularAutomata2D wireWorldRule (initSpaceWithCells (50, 50) Empty [])
main = runCellularAutomata2D wireWorldRule (initSpaceWithCells (50, 50) Empty []) >> return ()

-- | A empty wire stays an empty wire forever.
-- An electron head becomes on electron tail (the electron moves)
Expand Down

0 comments on commit 48f1508

Please sign in to comment.