Skip to content

Instantly share code, notes, and snippets.

@raimohanska
Created January 27, 2014 17:57
Show Gist options
  • Save raimohanska/8653902 to your computer and use it in GitHub Desktop.
Save raimohanska/8653902 to your computer and use it in GitHub Desktop.
Princess vs Lion in Elm
module PrincessVsLion where
import Keyboard
data GameState = Ongoing Int Int | LionWon | PrincessWon
proceedGame keyPresses state = case state of
Ongoing princess lion ->
checkEnd (princess + (keyPresses `div` 2)) (lion + 1)
x -> checkRestart keyPresses x
checkEnd princess lion =
if | lion == princess -> LionWon
| princess >= 13 -> PrincessWon
| otherwise -> Ongoing princess lion
checkRestart keyPresses state =
if | keyPresses > 0 -> initState
| otherwise -> state
input = diff 0 (sampleOn (fps 2) (count Keyboard.space))
initState = Ongoing 4 0
gameState = foldp proceedGame initState input
main = lift asText gameState
diff initValue signal =
let roll2 x3 (x2, x1) = (x3, x2)
withPrevious = foldp roll2 (initValue, initValue) signal
diff2 (x2, x1) = x2 - x1
in lift diff2 withPrevious
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment