DEV Community

klofnod
klofnod

Posted on

2 1

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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay