# Embedded Elixir Multidimensional Map

I would like some help on embedded elixir. I’m unfortunately coming from an object oriented world, but would like to learn more about functional programming. I have a problem that is pretty easy to solve in the oo world, but is proving to be quite challenging functionally. I have a game board that I would like to render in a phoenix view. In C# this would have been a multidimensional array and outputted using a nested foreach statement. So far in Phoenix, I’m struggling to find a way to express the same thing. The closest i’ve come up with is using a map similar to %{0 => %{0=> “X”, 1=>“O” . 2=> "X} , 1=> %{ 0 => “X”, 1=> “X”, 2=>“X”}}. This only an example as the data for my game board is a little more complex, but hopefully get’s the point across.

Probably the most natural way to do this with maps is to use tuple keys:

``````%{
{0, 0} => "X",
{0, 1} => "O",
# ...etc
}
``````
3 Likes

Or an ETS table, that uses tuple keys.

1 Like

You might like looking at the Tensor Hex.pm package. It allows the creation of (sparse) multidimensional arrays, especially for things like either mathematical matrices or game boards.

It has many built-in functions to query the state of certain positions and to iterate in various ways over the stored data. I hope it helps with your problem domain, and I’d love to get some feedback on how to improve it .

2 Likes

I keep forgetting about that due to its newness! Yes, look at it too!

1 Like

Dump into a `:digraph`, with the vertices as coordinate tuples? You’d need to run a function that builds the correct edges, + write some functions for serializing the board, but it massively simplifies updates, and can completely eliminate edge-of-the-game-board boundary issues. You get pathfinding included for free as well this way. Dunno how well this approach scales - works pretty well with small grids.

That does seem helpful. Thanks for responding. I will give a try.

I had saw your github repo and plan to give it a try. The bigges problem I had with maps was using a nested for statement in Phoenix … the output was always blank

Sorry, I keep meaning to update that, it’s a bit of a mess, I never bothered to complete the one on GH - I’ve got ~5 other versions locally in various states of disrepair, was planning to try to push up a more finished version later this week.