I’m using GenServer to create the state for the card game, “War”. It’s a single player game where the cards are randomly divided between the computer and user. Then the cards are compared one at a time and the higher value card “wins”. Since the user is dealt half the cards and the computer must have the other half I’m initializing the game when the user visits the GameController#new action.
Now I have the state of the game, two lists of cards, user_cards & computer_cards, but I can’t figure out how to compare the cards one by one, moving the lower value to the opposite list.
If i just wanted to run through the cards I would do something like:
def handle_call({:take_card}, _from, [card | rest]) do
{:reply, card, rest}
end
But this won’t work if I’m storing both the user cards and computer cards together in one state.
How can I manage the game in such a way where I can run through both lists, comparing the values of each card and moving the lower card to the higher card’s list?