I was thinking of a solution like yours after my previous mail. I ended up
with something more or less similar to your solution. And then, I went to
this solution :
filterRow: Int -> List Box -> Maybe Box
filterRow boxId row =
row
|> List.filter (\box -> box.id == boxId && not (box.player == Nothing))
|> List.head
update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
None ->
(model, Cmd.none)
ClickBox boxId ->
let
currentBox = List.map (filterRow boxId) model.cells |>
List.head
updateRow row =
let
updateBox box =
if boxId == box.id then
{box | player = Just model.current}
else
box
in
List.map updateBox row
in
case currentBox of
Nothing ->
({model | cells = List.map updateRow model.cells, current =
changePlayer model.current, boxClicked = Just boxId}, Cmd.none)
Just _ ->
(model, Cmd.none)
The idea is to return in currentBox a box which is not occupied by a
player. So I added the following test : List.filter (\box -> box.id ==
boxId && not (box.player == Nothing)) in filterRow.
But It seems that, in the update function, currentBox always return
something because it doesn't update my model... I'm obviously doing
something wrong. But, for me, as all cells are unoccupied, this test should
return Nothing. What's wrong ?
I also fixed the boxStyle function in order to display the shapes correctly
:
boxStyle player =
case player of
Nothing ->
[
("border", "1px solid black")
,("width", "64px")
,("height", "64px")
,("display", "inline-block")
]
Just p ->
[
("border", "1px solid black")
,("width", "64px")
,("height", "64px")
,("display", "inline-block")
,("background-size", "64px 64px")
,("background-image",
"url(http://jimdscott.com/static/images/tic-tac-toe-" ++ p.shape ++ ".png)")
]
I'm sorry, but I don't know how to debug elm code in general, and in
particular with the elm lang try editor (I'm at work and I don't have elm
working on my machine...)
Le mardi 25 octobre 2016 23:30:45 UTC+2, Peter Damoc a écrit :
>
>
>
> On Tue, Oct 25, 2016 at 11:12 PM, Did <[email protected] <javascript:>>
> wrote:
>
>> Now is the hardest part for me : the update function. Since elm is a
>> functional language, I suppose I have to recreate a new cells list with the
>> value of the box concerned by the click. In OOP languages, I'd just take
>> the reference of the box from the list, change the player and that's it...
>> Here is the update function with some not working changes...
>>
>> CELLS_WITH_BOX_UPDATED would be the new cells list updated but I don't
>> understand how to do this...
>>
>
> `cells` is a list of lists.
> updated cells would mean that you would have to update all the list in the
> main list, in other words, update every row
>
> let
> currentBox = List.map (filterRow boxId) model.cells
> updateRow row = List.map (\box -> if boxId == box.id then {box |
> shape = Just model.current } else box) row
> in
> case currentBox of
> Nothing ->
> { model | cells = List.map updateRow model.cells, current =
> changePlayer
> model.current }
> Just _ ->
> model -- nothing changes (cannot click over a previous ticked
> cell)
>
>
>
> --
> There is NO FATE, we are the creators.
> blog: http://damoc.ro/
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.