Roman Numeral Generator Part 1

Hi guys. I have had an idea for a roman numeral generator method for a while now and thought I would share it with you. The idea is to write a method so that you can pass it an int and it will output the roman numerals which represent that number. This tutorial series will probably only be 2-3 parts long but it might work well with the 4X Space Game tutorial for ship/planet/leader names etc… It could also be useful in other projects.

Firstly what are the roman numerals? Well 1 to 10 are:

Number Numeral
1 I
2 II
3 III
4 IV
5 V
6 VI
7 VII
8 VIII
9 IX
10 X

These are the ones we will begin with in this part. We can add the higher numbers later.

Note: There is no 0 (zero) in roman numerals.

In Unity create a new script called RomanNumerals. Make it a pure class by removing the “: Monobehavior” next to the class name and deleting the Start and Update methods.

Part001Pic001
RomanNumerals Class

Next create a new public static method called RomanNumeralGenerator. Make it return a string and pass it an int.

Part001Pic002
Setting Up RomanNumeralGenerator Method

Declare a string called roman and return it.

Part001Pic003.JPG
roman

We are going to be using switch/case to set the value of roman. This is essentially a more efficient if/else statement.

Between declaring roman and returning it, create a Switch which is passed number then cases for each of the above numbers. Don’t forget to add a default case at the end.

Part001Pic004.JPG
Switch/Case

Now for each case we need to set roman to the appropriate roman numeral.

Part001Pic005.JPG
Setting roman

But what about our default case? Well for now we will set roman equal to the number passed to the switch (converted to a string). That way if a number is passed to the method which isn’t between 1 and 10, it will just return that number as a string.

Part001Pic006
default Case

Great we can now convert the numbers 1 – 10 into roman numerals. But how can we tell this is working?

  • If you have been following the 4X Space Game tutorial and have got to the part where we name the planets see the example immediately below (titled Example 1). Make sure the RomanNumerals is added somewhere in the assets folder (I added it to folder path Assets/Scripts/Data Handlers for those who have reached part 17).
  • If you have not been following the 4X Space Game tutorial create a new Unity project, add the RomanNumerals script to the assets folder and then skip to the example below the 4X Space Game example (titled Example 2)

Example 1

Open up the Galaxy script and go to the CreatePlanetData method. Where we set the name of the planet, we currently get the Star name and then concatenate that with a number. We want to keep the Star name bit but change it so the number displays as a roman numeral instead.

Call the RomanNumeralGenerator method and pass it the planetList.Count plus 1.

Part001Pic007.JPG
Using it to Name Planets

Hitting play now produces something like this:

Part001Pic008.JPG
Planets with Roman Numerals

Example 2

Create a new script called Test and delete the Update method. In the Start method add a for loop where int i starts at 1 and finishes at 10. 

Part001Pic009
Setting Up the For Loop

In the loop add a Debug.Log that outputs the roman numeral for i.

Part001Pic010.JPG
Debug.Log

Now in Unity create a new empty object and attach the Test script to it. Press play and you should see this in the console:

Part001Pic011.JPG
Numerals Displayed in the Console

That concludes this part. In the next part we will add the higher roman numerals to our Generator.

Part 2

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s