Chris Bongers

Posted on • Originally published at daily-dev-tips.com

# Public Solving: Earth, Fire, Snow game

The elves love making up games, and they have their own version of Rock, Paper, Scissor.

Their version includes Earth, Fire, and Snow.
Let me quickly tell you how you can win with this game:

• Fire melts snow
• Snow covers earth
• Earth extinguishes fire

Alright, let's get right into coding this fun game for the elves π

I think it's safe to say there are only three options that win for this game.

Then there is a tie (both the same)

That's actually all there is, and it makes our program a bit easier to create.

Let me show you how:

## Building the earth, fire, snow game in JavaScript

Let's first define an object with the winning combinations.

``````const winMatchUp = {
fire: 'snow',
snow: 'earth',
earth: 'fire',
};
``````

There is no need to define the other way around as we can abstract it, seeing we only have two players.

First, let's look at a draw. This means both players picked the same element.

``````export const selectWinner = (user1, user2) => {
if (user1.choice === user2.choice) return null;
};
``````

Then we can check if user1's choice match up is equal to user2's choice. This would mean user 1 one.

Let's me check an example to explain a bit more:

• user one picked snow
• user two picked earth

We then query our match-up table and say give us the matchup object for `snow`. This will return `earth`.

So if we now compare this to user two choice, we won!

In our code we can do it like so:

``````if (winMatchUp[user1.choice] === user2.choice) return user1;
``````

This automatically means, if user 1 did not win, user 2 must have won!

``````export const selectWinner = (user1, user2) => {
if (user1.choice === user2.choice) return null;
if (winMatchUp[user1.choice] === user2.choice) return user1;
return user2;
};
``````

And there you go!

A super simple game, but yet so much fun.

I've run the test as a sanity check, and they turn green β.

Let me know what you think of my solution and how you would do differently.

### Thank you for reading, and let's connect!

Alex Lohr

You can make this a no-branch solution with a slightly more complex object:

``````const results = {
fire: { fire: 0, earth: 2, snow: 1 },
earth: { fire: 1, earth: 0, snow: 2 },
snow: { fire: 2, earth: 1, snow: 0 }
}
const selectWinner = (user1, user2) =>
[null, user1, user2][results[user1.choice][user2.choice]]
``````

Chris Bongers

Wow damn, that's next level abstraction.
Really cool we can do that actually, although it might not be the most "readable" option.

Alex Lohr

I'd usually add an comment, but I wanted you to figure it out on your own:

``````// Selection out of an array [null, user1, user2] based on their choices:
const results = {
fire: { fire: 0, earth: 2, snow: 1 },
earth: { fire: 1, earth: 0, snow: 2 },
snow: { fire: 2, earth: 1, snow: 0 }
}
const selectWinner = (user1, user2) =>
[null, user1, user2][results[user1.choice][user2.choice]]
``````

Want to join the conversation?

It's easy! Become a DEV member to follow this post, comment, and more.