Skip to content

Instantly share code, notes, and snippets.

@raimohanska
Created January 26, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raimohanska/8639343 to your computer and use it in GitHub Desktop.
Save raimohanska/8639343 to your computer and use it in GitHub Desktop.
Sketch for a Lion Chases Princess Game
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 }
@raimohanska
Copy link
Author

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

@raimohanska
Copy link
Author

Pow pow pow, now it's restartable.

https://gist.github.com/raimohanska/8653902

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment