Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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