DEV Community

klofnod
klofnod

Posted on

Adding Unique Outcomes With Random Numbers

The Idea

I decided to create a rogue-like text based adventure game for my final capstone project while I was in Flatiron. It was a ton of fun, and a good learning experience. I was challenged to find ways to keep elements randomized but not to the point that the game would be impossible. It helped me grow my understanding and ability in Ruby as well as React.

Getting Started

Most coding languages have a nice and easy way to create a random number upon being called. This is a great start but has some issues. One is if you want a progression system you will have to tweak your formula in order to things relevant to your progression. There are many different approaches to this problem. Here I am going to show how I tackled this issue.

Basic Set Up

In my game I was allowing a user to create a character, I wanted their starting stats to be randomized but within a range. Ruby makes this super easy.
rand(3..8)
rand(3...8)

Both of these methods allow us to create a random number within the range specified. The difference between them are .. runs from the beginning to the end inclusively where as ... excludes the end value. This is an easy solve and doesn't require and additional logic to be checked.

Next Challenge

Now I wanted to be able to present that character with a encounter and an enemy to fight. This is where it gets a bit tricky but not crazy. For the encounter I set a Round to be incremented and then checked, with a simple case statement. When the round was 1..3 I would select from a sorted array of encounters by index, and then just repeated that till round 12. For the enemy spawn It was basically the same thing. Sort enemies by power and then select weaker enemies when the round was low and stronger enemies when the round was higher.

Loot

What would the game be without loot? Now I wanted to create a chest system that would reward completed encounters with loot. I was able to solve this using most of the same logic from my encounter and enemy logic so that was a nice moment. When could you wrote before was reusable for another function with just some minor modifications. The game itself is linked in linkedin profile which can be found at jeremy-francis-klofnod on linkedin. Feel free to reach out and ask and questions or comments you have about the project

Top comments (0)