Off The Clock: Discovering a Solution for Generating 2D Maps in Gaming

When I’m not building fintech solutions at Kunai, I’m often engaged in other types of creative problem solving in my free time. One great example of this is my years-long endeavor to create a simple computerized clone of an old Hasbro board game.

Off The Clock: Discovering a Solution for Generating 2D Maps in Gaming

When I’m not building fintech solutions at Kunai, I’m often engaged in other types of creative problem solving in my free time. One great example of this is my years-long endeavor to create a simple computerized clone of the old Hasbro board game “Black Tower.”

Over time, my efforts to replicate the board game produced a full-fledged, 2D, Zelda-like Role Playing Game, or ZRPG. And what does any good 2D RPG game need? An unknown world for players to explore.

In gaming, this is usually accomplished with procedurally generated maps. A program is written to automatically produce random areas and terrain as players enter them. I wanted to do the same. I planned to write a program that mimicked real-world terrains and looked like what you'd see in the “Legend Of Zelda” or “Age of Empires” games. However, I had no idea how to accomplish this.

My first attempt involved using simple terrain tiles. This didn’t go well. If you randomly place the tiles you get something similar to white noise, which wouldn't work for my game and didn't look realistic at all. Here is a generated map of random tiles to give you an idea of what that attempt looked like:

Not very good-looking, right? It certainly doesn't mimic actual world terrains. I needed something less granular, so I tried a different approach.

Mapping by Numbers

With some help from Google searches and various online game development communities, I came up with a solution to  my problem: a 2D array of  random numbers.

Here’s how it works. First, you create a 2D array that is the same  height and width as the tile map you wish to generate. Fill this array with random integers.

These integers can be anything, but if you're trying to generate a realistic-looking terrain map I recommend keeping the gap between the minimum and maximum values small. I like to use a range of 1-20, but of course this all depends on how many levels of height and types of terrain you want. Dramatic cliffs, for example, will require dramatic gaps in height.

It’s also important to consider whether you're having certain types of terrain correspond to a single number or range of numbers. You could use 1 for water, 2 for dirt, or 1-4 for water, and 5-10 for dirt, for example. The second gives you more flexibility, and I’ll get into more detail on it further down.

Once you’ve created this array, loop through all the cells and set the value of each cell to the average of the cells around it.

In the above loop, for example, we would set the value of the top left green cell to the average of the numbers adjacent to it  rounded to the nearest whole number. In this instance, 19, 0, and 43 give us an average of 21.

Since the green cell is a corner cell, it only gets the average of three surrounding cells. Elsewhere in the array, non-edge cells will take on the average of eight adjacent cells. The red cell containing the number 35 with yellow around it would be set to 27, for example, which is the average of the total of all 8 squares adjacent to it.

Do this for each cell in the grid. It may not seem like much now, but we’re getting there!

More Looping Means More Realism

After looping through the cells of your array once, do it again. In fact, you should repeat the process at least twice. The more times you average the array of cells, the larger the areas of each number gets and the less different numbers there are.

This sounds confusing, but it’s important. Each number corresponds to a different kind of texture image. Without repeat looping, you’ll simply generate maps that look like the white noise map I posted above. Repeated looping solves this problem.

With just one additional loop of cell averaging, we get maps that look somewhat like this:

After five more loops of averaging, however, we get maps that look like this. Notice how some of the terrain types are completely gone:

Fill Gaps and Create More Detailed Maps with Ranges

If you have more numbers than tile textures, or even if you want to play with how your maps look, you can use numerical ranges for setting textures instead of single numbers.

Say, for example, that you assigned the following numbers to these terrain types:

  • 1-4: water
  • 5-10: dirt
  • 11-15: grass
  • 16+: stone

With this method, you can have a much larger gap between the minimum and maximum numbers and guarantee that all terrains will exist on the map. This will generate more diverse geography than the map above, which has each tile type corresponding to a single number. With such limited range, not all of the possible types of terrains made it to the map.

Other Ideas

For even more unique maps, you could try setting a few horizontal, diagonal, or vertical lines of cells to very low or very high numbers to create mountains, rivers, or other geographic features. Another idea is to have different number maps used in tandem, and use them to automatically generate other items like trees or rocks. For example, say you create a nitrogen map and use both the height map and nitrogen map in your game. You could set it up so that if the value of a tile on the height map is between five and ten and corresponds with a nitrogen map tile that is greater than five, it automatically generates a bush or a tree on that tile.


You know a developer loves what they do when instead of taking their work home with them, they find ways to create work in the games they play. I hope that you've learned something in this tutorial that will be useful in 2D map making or any other creative development endeavors.



Sandeep: Tell me a bit about the early part of your career.

Tom: I spent a decade helping to build start-ups focused on application and database software. This was where I learned how to sell and do business development. I was fortunate to be part of one company going public and another being sold to IBM.

Sandeep: What is something you learned during this time that helped you with consulting?

Tom: I began to appreciate how different customers achieved varying levels of success with the same foundational technology. This made me understand just how critical getting your team and process right can be.

Sandeep: This is something I only came to appreciate years into consulting, especially after the sale of my first consultancy to Capital One.I saw teams in different parts of the company trying to solve challenges like real-time messaging. Same corporate culture, same technology, same internal support mechanisms. Night and day outcomes.

Tom: We saw a lot of the same thing after selling our practice to EMC (sold to Dell in 2015). This is probably the thing I'm most proud of when it comes to the teams I've helped to build: the ability to perform well in a variety of contexts, sometimes in ways that inspires the client team to up their game as well.

Sandeep: Yes. It's particularly cool to see your team succeed in individual ways after an skills definitely translate into the corporate environment.

Tom: Totally. We have people who've stayed on at Dell and risen up the ranks, while others took the opportunity to become successful executives at other Fortune 100 companies....or to start their own agencies and startups.

Sandeep: We've both been around a while. My first consulting project was a Y2K thing for Cisco back in 1998. You've been around a little longer than that :). How do you think consulting has changed most during the past five years?

Tom: I think because there is so much infrastructure available now, consulting has become more delivery and outcome-oriented. A better blend of strategic and tactical. Public Cloud has also enabled velocity to increase at a pace unfathomable 5 years ago.

Sandeep: What has stayed the same?

Tom: It's still mostly about people. People who thrive on change and are focused on their personal and professional development. I love that this has not and will not's what I love about consulting.

Sandeep: I know you're adjusting your work style to COVID. You're still a dude who clearly prefers to drive an hour for a socially-distanced hike or outdoor meeting over Zoom any day of the week :) But personal styles aside, what is specifically compelling about a remote agency during the era of COVID?

Tom: Kunai has been remote for years, which gives them an inherent advantage. There is something about the communication and management styles that just works in a way that other organizations are still figuring out.

Sandeep: Yeah, I think what a lot of people fail to realize is that remote work isn't just office work over Zoom. it's an entirely new paradigm. There needs to be an understanding for asynchronous efficiency...and this just takes time and effort to develop. How do you approach remote work and family? What are you learning about separating work and personal time?

Tom: No matter what the form of interaction, Focus. Be present. Quality over quantity. The best weeks are the weeks where I proactively schedule work and personal time. Neil (Kunai's Head of Delivery) shared a great quote with me "With discipline comes freedom." When I am proactively addressing the majority of my professional and personal commitments, I find I earn a little flexibility. A little freedom.

Sandeep: Tell us about a business hero of yours that I may not have heard of before.

Tom: Paul O'Neill is someone you may not know. His work in both the public and the private sector created a profound impact

Sandeep: We are both over forty years old :). How have you learned how to work smarter during the past decade or so? What do you wish you knew about consulting when you were 25 that you know now?

Tom: Consultants want to make lasting change. Lasting change is often not the act of a single person. Today I work much harder bringing others along on the journey.

Sandeep: Last question. What are you doing here? :) Why join a small consulting company this late in your career when you could have a cushy job somewhere else?

Tom: I love a good challenge personally and professionally. When I turned 40, I decided I would run a 10K every Thanksgiving weekend and try to have my finishing time be less than my age. With the exception of one year where I did not run due to a health issue, I have met the goal. I also recently completed the Leadville 100 Mountain Bike race. So, I guess I'm here because I'm a glutton for punishment :) Jokes aside, our customers have a job to do and I intend to put Kunai in a position to execute flawlessly on their behalf. I love committing jointly to audacious goals for our customers and our business.

Keep Reading

See All →