Separated agents for separated files

Hi, as always I post here a question I have to warn that english is not my main language.

I am using Elixir for developing games, specifically my game is a “Guess the flag”, the game shows you a flag and gives you four options, one is (of course) the right one and the other three are the distractors or the wrong answers.

When I thought about the design of the game, I decided not to use a DB, but use different Agents holding the list of the possible answers (kept in json files), and when a player plays a new round of the game, the main process sends a message to each one of the Agents and they reply back with the distractors and with the correct answer, and the main process build the list of possible options.

My question is, is this a good pattern… I mean holding a list of words in each Agent and query them?.

Thanks and sorry again for my poor english.


Is there a reason you chose to use multiple agents instead of just storing all of the state in one agent?


I need to access randomly to the list, I think that is faster 4 parallel random on different lists that sequentally.

Maybe I am wrong.



Let me see if I get this:

It’s a game of guessing the correct “door”, there are four “doors” where one is the “correct” one and the remaining are “false.” The guesser can pick one “door” and the game will end if the “door” is not the correct one.

When a new round start the game process will send a state to each of the processes representing the doors. When the user pick a door the manager ask the door if it is the correct one and it will reply.

If this is the case I think that a list stored in the manager process would be sufficient; even just storing a number between 1 and 4, or how many you need—or should the processes hold more information/state than wether they are the correct door or not?


I will try to explain my best the procedure of a round
When the player start a game, round one is sent to the player, the round is composed of a image of a flag, and four country names… one is the correct and the other three not.
I thought getting the three wrong options from three differents list of words, that will be in three different Agents, and the way of getting each one is sending a message asking for a random value of each list.

I hope that is clear now.


Ah, I see.

I suggest keeping it in one process, and have a list of countries where you take a correct and three wrong answers from.

countries = ["denmark", "belgium", "russia", "france", "albania", "spain"]
# entering new round
[correct, wrong1, wrong2, wrong3 | _rest] = Enum.shuffle(countries)

That would provide you with four unique countries (ie, “denmark” would not be both correct and wrong at the same time…)


Oh man, I love elixir!.. so clean and obvious.

One more question, if the list of counties gets bigger (maybe with 100.000 entries between countries and another types of flags, thinking in different levels of difficulty), using shuffle is better than three paralell randoms in three lists of 33.333?

Many thanks


Could you store what you need to access in a Map instead? Random access of a Map is O(1).


Mmh both are nice ways, I didn’t know that feature of Map


Okay, 100_000 entries, then we have a different kind of beast :slight_smile:

You could shuffle once on initialisation, and just take from the top like you would with a deck of cards and store the rest as the state for the next round. Then you would only have to do the heavy operation of shuffling once—shuffling is not a problem for small lists, but if you do it all the time for bigger you might get into trouble performance wise.

I don’t know about the different levels of difficulties.


Another option is mnesia. I guess that would provide really fast lookups and random access to fairly big datasets.

mnesia is part of OTP, and there is an Elixir wrapper called Amnesia


I will try with this way, sorry for the bad english… when I said about differente levels of difficulty I mean that I will add a lot of another flags (think of Red Cross and like that) this is the explanation of why it will grow near 100.000



Your English is fine. No need for apologies :slight_smile: