Created
April 5, 2021 21:02
-
-
Save infiniteregrets/0e66ffa6500e554b772872f32f22a1e9 to your computer and use it in GitHub Desktop.
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 Main exposing (..) | |
import GraphicSVG exposing (..) | |
import GraphicSVG.App exposing (..) | |
myShapes model = [ background, | |
(model.state) | |
|> initializeShapes | |
|> spreadShapes | |
|> group | |
|> move (-10,-10) | |
|> scale 2, | |
model |> checkWin | |
] | |
checkWin: Model -> Shape Msg | |
checkWin model = if model.state == List.repeat 9 False | |
then text "LIGHTS OUT" | |
|> sansserif | |
|> filled white | |
|> move (-35, 40) | |
else text "" |> filled white | |
spreadShapes: List (Shape Msg) -> List (Shape Msg) | |
spreadShapes = List.indexedMap | |
(\x y -> y |> move(10*toFloat (xVal x), 10*toFloat(yVal x))) | |
initializeShapes: List (Bool) -> List (Shape Msg) | |
initializeShapes = List.indexedMap createLights | |
createLights: Int -> Bool -> Shape Msg | |
createLights index bool = group [ | |
square 10 | |
|> filled (if bool == True then yellow else grey), | |
square 10 | |
|> outlined (solid 1) black | |
] |> notifyTap (FlipState (xVal index, yVal index)) | |
type Msg = Tick Float GetKeyState | |
| FlipState (Int, Int) | |
background: Shape Msg | |
background = square 200 | |
|> filled black | |
yVal: Int -> Int | |
yVal x = x//3 | |
xVal: Int -> Int | |
xVal x = modBy 3 x | |
update: Msg -> Model -> Model | |
update msg model = case msg of | |
Tick t _ -> { model | time = t } | |
FlipState (x, y) -> {model | state = | |
model.state | |
|> List.indexedMap (\i j ->flipStateIndex i j (x,y))} | |
flipStateIndex: Int -> Bool -> (Int, Int) -> Bool | |
flipStateIndex i j (x,y) = if xVal i == x && yVal i == y | |
then not j | |
else if (xVal i) + 1 == x && yVal i == y | |
then not j | |
else if (xVal i) - 1 == x && yVal i == y | |
then not j | |
else if xVal i == x && (yVal i) + 1 == y | |
then not j | |
else if xVal i == x && (yVal i) - 1 == y | |
then not j | |
else j | |
type alias Model = | |
{ | |
time : Float, | |
state: List Bool | |
} | |
init: Model | |
init = { time = 0, state = List.repeat 9 True } | |
main = gameApp Tick { model = init, view = view, update = update, title = "Game Slot" } | |
view model = collage 1920 1080 (myShapes model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment