Created
January 26, 2014 20:54
-
-
Save raimohanska/8639343 to your computer and use it in GitHub Desktop.
Sketch for a Lion Chases Princess Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PrincessVsLion where | |
import Keyboard | |
-- character positions | |
princess = foldp inc 4 (pressesOf Keyboard.space) | |
lion = foldp inc 0 (fps 2) | |
-- combined game state | |
gameState = lift2 makeState princess lion | |
-- main function | |
main = lift asText gameState | |
-- helpers | |
pressesOf key = keepIf id False key | |
inc _ prev = prev + 1 | |
makeState p l = { princess= p, lion= l } |
Now it works!
@phadej you're right, there has to be a function proceeding the "whole world state". And you have to forget almost everything you've learn about Rx/Bacon.js conventions. There's no temporal composition. Just a big state machine. Yet, the end result is quite nice. Except it's still not restartable.
module PrincessVsLion where
import Keyboard
data GameState = Ongoing Int Int | LionWon | PrincessWon
proceedGame keyPresses state = case state of
LionWon -> LionWon
PrincessWon -> PrincessWon
Ongoing princess lion ->
check ((keyPresses `div` 2) + 4) (lion + 1)
check princess lion =
if | lion == princess -> LionWon
| princess >= 13 -> PrincessWon
| otherwise -> Ongoing princess lion
input = sampleOn (fps 2) (count Keyboard.space)
initState = Ongoing 4 0
gameState = foldp proceedGame initState input
main = lift asText gameState
Pow pow pow, now it's restartable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I understand Elm right, you just end up having single "whole world state", if you want to be able to alter it as single entity (e.g. reset to defaults). Using
Bacon.js
vocabulary: you work (almost) only with Properties. You don't really haveflatMap
for Properties there either.And you can accumulate state with
Applicative
instance ofSignal
only.I guess monadic bind would make sense in use cases if eg
HTTP.sendGet
would be of typestring -> Signal (Response String)
, but it's cleverly made intoSignal string -> Signal (Response String)
.Applicative
is enough there as well.