As the title says, how would I compare each element of a map to the rest of the map? And then update the map in the same loop?
Consider the following:
board_position = {
"battle_ship": {"x":5.0, "y":5.0, "alive": true},
"destroyer_1": {"x":15.0, "y":15.0, "alive": true},
"destroyer_2": {"x":0.0, "y":0.0, "alive": true},
"carrier_1": {"x":15.0, "y":0.0, "alive": true},
"carrier_2": {"x":0.0, "y":15.0, "alive": true}
}
And here’s what little code I have to show for it:
new_board_position = Enum.map(result, fn {ship_a, body_a} ->
Enum.map(result, fn {ship_b, body_b} ->
if ship_a != ship_b do
has_collided = checkShipClass(body_a, body_b)
# returns {"body_a": true/false, "body_b": true/false} depending if X and Y values match
# also depends on ship class
if has_collided == true do
# update body_a alive
# update body_b alive
else
# body_a = body_a?
# body_b = body_b?
# keep current values
end
end
end)
end)
The problem I’m immediately foreseeing from my above code is that it would probably return a list, if not a list of lists - I need it to stay the same format as the initial board_position
. And I’m also not sure how to update a value from within a nested .map
I’m still new to elixir, so I do apologize if this seems trivial; however, all my recent searches either result in samples that either compares two different maps, or just loops over the map without performing any updates, or doesn’t care much for the format.
Is there anything else I can try? Thanks!