DEV Community

Discussion on: #30daysofelm Day 5: "Lights Out" game

Collapse
 
wolfadex profile image
Wolfgang Schuster

The last line with List.repeat doesn't feel right to me. It does the job, but only one of the checkboxes will have an XY position of (0, 0).

I completely agree, especially since you're not using the value when you use List.indexedMap. I'd probably just use List.map (\index -> ...) List.range 0 24 for building the initialstate.


I just used a tuple because I saw it in the documentation. Is there a practical difference between tuples and a list of length 2?

There's a huge difference! The best example probably being that with the tuple you can do

let
    ( x, y ) = tuplePosition
in
Enter fullscreen mode Exit fullscreen mode

but with a List Int you'd have to do something like

case listPosition of
    [ x, y ] -> -- use x and y
    _ -> -- handle an impossible case
Enter fullscreen mode Exit fullscreen mode

For both your Svelte and Elm code, you might be interested in Manhattan distance. Using this would allow you to just check the distance between 2 points being equal to 1 and not have to check the x and y distances separately.


Maybe there's a way I could have achieved what I wanted to anyway.

There totally is a way! You could change your Msg from PressButton ( Int, Int ) to PressButton ( Int, Int ) Bool and then in your view do

input
    [ type_ "checkbox"
    , onCheck (PressButton ( checkbox.x, checkbox.y ))
    , checked checkbox.checked
    , style "transform" "scale(2)"
    ]
     []
Enter fullscreen mode Exit fullscreen mode

You'll see that I changed from onClick to onCheck which is expecting a function that takes a Bool and returns a Msg. By doing only PressButton ( checkbox.x, checkbox.y ) I've created a function that is expecting a Bool and returns a Msg. But your solution also works great.


Can't wait to read more!