Created
January 27, 2014 17:57
-
-
Save raimohanska/8653902 to your computer and use it in GitHub Desktop.
Princess vs Lion in Elm
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 | |
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