How I solved Bomberman challenge on HackerRank

I found a challenge called “The Bomberman Game” on HackerRank and I found it pretty interesting.

It boils down to something like this:

The Bomberman Game


  • Bombs will explode in 3 seconds
  • When a bomb explodes it clears its tile and 1 tile up, down, left, right
  • Bombs that are about to go off explode at the same time
  • There are no chain reactions
  • If a bomb explodes next to another that is not about to go off then the nearby bomb is just cleared


  1. Bomberman plants some bombs (you’re given an input with the map)
  2. A second passes
  3. A second passes. Bomberman plants bombs on all empty slots
  4. A second passes
  5. Repeat 3 and 4 until N seconds passes


  • R = number of rows
  • C = number of cols
  • S = number of seconds
  • O = bomb
  • . = empty


Return the map after N seconds have passed.


The first thing I though was to solve it with a generator (well, there was no real need to use it, but since it is about a game I thought it would be fun to be able to access any state of the game).

The challenge calls a function named processData passing the input. The input parameter is a string and within it there are data you need to extract, so the first thing I did was to create a function to parse the input. This way I'm separating concerns and avoiding the GOD anti-pattern.

A little disclaimer before showing the code: I implemented it in a way that it’s easier to implement, understand and is maintainable (not perfect, I could still improve it), I could improve the performance a lot by implementing without a matrix and doing some different things. But in any case it did pass all performance tests either way.

This is how I started my function to parse the input:

With that I could build a unit test for this function and make it fail.

Wait, what? Make it fail? — Yes. Making a test fail will help you check that the test is running correctly. Sometimes it is possible to write a test that will always pass (by accident or something) and you will get a false positive.

Once I got this test setup I started to implement extractParams.

What could’ve been better? — I could’ve used substring and replace instead of splitting the string and mapping the matrix.

I got the first line from the input and extracted the 3 numbers into 3 variables and then converted it from strings to numbers.

With the map I converted it to a matrix of numbers, replacing bombs with 3 (3 seconds to explode) and empty slots with -1 which I called emptySlot (no magic numbers).

Magic numbers? — From Wikipedia: “Unique values with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants”

The reason I choose to create a matrix was to be able to simulate the game, so I can know the exact state of the game at any second (for the end goal it doesn’t matter to know if a bomb has 1, 2 or 3 seconds, it just matters if a bomb is there or not, but for fun it’s cool).

Now that I had the parsed parameters I created another function, I called it tick. This is my generator function where each step in it is a second passing.

Step 1: Make the function fail a test.

Step 2: Test a scenario that doesn’t exist then implement it.

I made so the function would decrement a second from the total and run some code returning a result each time a second pass.

The first scenario I implemented was the explosion, so when I’m iterating the matrix and find a 0 the "next state" of it is an explosion. The tick function doesn't need to know how to explode a bomb, it just has to set the map to a new state. So to handle the explosion I created a function explode.

In my first version I was mistakenly exploding bombs at 1 second… Off by 1 errors are kinda annoying right?

In the explode function I basically got the map and position where the bomb had to go off. So I started (after the failing test) to test exploding the slot the bomb is in.

There were few things to watch out when I was implementing the other scenarios:

  • Boundaries: I shouldn’t change a position that doesn’t even exist in the matrix (ex: col -1)
  • Bombs explode at the same time: I can’t clear the position of a bomb that is also exploding, because it is exploding “at the same time”, so I need to keep it there until I get to it and make it explode

The final version of explode became:

There are a lot of scenarios to test in this case:

  • Bomb could be on
  • top left (no neighbors to the left or top)
  • middle left
  • bottom left
  • center
  • and so on…

By having a function to handle nextStateAfterExplosion we can test the change of state in there and not have to worry about it here. We can just test with the correct slots were changed and test edge cases.

An edge test case for explode (test if explodes correctly not having anything to the top and left):

nextStateAfterExplosion was created to get the state the slot should be after the explosion.

If the neighbor bomb is exploding don’t touch it, else clear whatever is in there.

With tests covering all scenarios

At this point I had all the application flow required to make bombs explode. Next I had to go back to tick and make a second pass to empty slots and to bombs that were not yet ready to explode.

For empty slots and other bombs

nextStateAfterTick is a function to get the next state of a empty slot or bomb (that is not exploding).

It might look like it is the same as nextStateAfterExplosion, but it isn't. Because when a explosion happen and the slot is a bomb in 2 seconds it will set the slot to empty (-1) instead of reducing a second from the bomb (element - 1).

One possible alternative would be having the function take an extra parameter indicating if it is exploding or not. I decided to leave this way.

And I added tests for all paths on it.

Now the code is handling bombs exploding, bombs not ready, empty slots. Great!

If we go back to the rules, there is one more thing that needs to be done. In a certain step Bomberman plants bombs in all slots without bombs.

To implement that I had to change tick.

Now I had all pieces and I could put them together:

Good practices

S from SOLID stands for Single responsibility and I think it was the key to build a nice solution to this challenge.

Breaking down the code in smaller functions that are responsible for specific things makes tasks way easier to accomplish, understand, test and change.

TypeScript also helped (it always helps). You can avoid a significant amount of issues and time loss while coding just by having your project in TypeScript.

I didn’t have anyone to make a Code Review before I sent the code, that would probably make me write somethings different to improve my code.

I also didn’t have, but I like, a “Tech Review”. Before starting to implement something talk to a teammate about the thing you want to do, you might realize something way before it gets in the Code Review.

This challenge reminded me of <2 kyu> Binary Genetic Algorithms kata that a friend and I broke into a series of 5 katas. A really big and complex challenge was broken down into 5 easy ones.

The funny thing about this is that it also applies to things in life, big problems can be broken down in smaller ones.


With the tests I created and TypeScript I feel extremely confident to change anything, refactor at will.


Well, one cool thing about generator function is that you can take a single step and check what is going on.

In the tick function we can set up yield after a single explosion, or second pass for a single bomb:

This way each step would be an interaction on a single slot, which might help to find problems (since exploding everything and checking the result later might be difficult).


I ❤ Paint!

By drawing the board in different states we can start to see some patterns. But don’t assume that all patterns you see really exist from a single drawing, consider that different scenarios can change the results of what you see in this drawing.

Drawing is also good to get a better picture of what you’re trying to do.

Hint: if you want to find the fastest way possible, try drawing like that.


Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store