Building a name generator for weapons. [Tutorial]

Hi folks, this will be my first tutorial on this site. We’ll be making a name generator for weapons. Useful if you don’t know what to name a weapon in your program.

We’ll use:

  • Lists.
  • Switch Case.
  • Random Class.

This is what our final solution will look like: Note, the magnifying glass is a picturebox.

Download the final solution here.

The final solution outcome.

The final solution outcome.

Look out for the complete tutorial after the break.

First, we’ll want to build three string Lists<>, one for each of the characters archetypes most RPG’s have: The Nimble Thief/Bowman, The Average Adventurer and the Hulking Giant. Then another three lists for the weapon type of each type of character. A nimble uses a katana type weapon, a buff dude uses a two-handed axe, you get the gist of it? Finally, three more lists for the suffix a weapon might have.

The finished product we’re looking for is something like:

Swift Jian of Lament.

Cumbursome Claymore of The Ancients.


Create a new class for your solution called ItemLists. In that class write:

Declare your code:

List<string> SizeLight = new List<string>();
List<string> SizeMedium = new List<string>();
List<string> SizeHeavy = new List<string>();

List<string> TypeWeaponLight = new List<string>();
List<string> TypeWeaponMedium = new List<string>();
List<string> TypeWeaponHeavy = new List<string>();

List<string> WeaponSuffixEsoteric = new List<string>();
List<string> WeaponSuffixTechnologic = new List<string>();
List<string> WeaponSuffixNaturalistic = new List<string>();

With this bit of code you are creating nine lists of type string. One for each character type.

In that same class, create a void method called GenerateTheLists().

Write the following in that Method:

SizeLight.Add(“Swift”);
SizeLight.Add(“Light”);
SizeLight.Add(“Double-Quick”);
SizeLight.Add(“Supersonic”);
SizeLight.Add(“Hasty”);
SizeLight.Add(“Agile”);
SizeLight.Add(“Hypersonic”);
SizeLight.Add(“Nimble”);
SizeLight.Add(“Rapid”);
SizeLight.Add(“Velocious”);
SizeLight.Add(“Racing”);
SizeLight.Add(“Flashing”);

SizeMedium.Add(“Sturdy”);
SizeMedium.Add(“Metallic”);
SizeMedium.Add(“Hardy”);
SizeMedium.Add(“Weighted”);
SizeMedium.Add(“Hilted”);
SizeMedium.Add(“Sharpened”);
SizeMedium.Add(“Devious”);
SizeMedium.Add(“Chromed”);
SizeMedium.Add(“Casted”);
SizeMedium.Add(“Polished”);
SizeMedium.Add(“Detailed”);
SizeMedium.Add(“Ornamented”);

SizeHeavy.Add(“Heavy”);
SizeHeavy.Add(“Cumbersome”);
SizeHeavy.Add(“Titanic”);
SizeHeavy.Add(“Two-Handed”);
SizeHeavy.Add(“Collosal”);
SizeHeavy.Add(“Gold-plated”);


TypeWeaponLight.Add(“Katana”);
TypeWeaponLight.Add(“Rapier”);
TypeWeaponLight.Add(“Tachi”);
TypeWeaponLight.Add(“Jian”);
TypeWeaponLight.Add(“Thinsword”);
TypeWeaponLight.Add(“Twig”);

TypeWeaponMedium.Add(“Gladius”);
TypeWeaponMedium.Add(“Broadsword”);
TypeWeaponMedium.Add(“Small-sword”);
TypeWeaponMedium.Add(“Dao”);
TypeWeaponMedium.Add(“Excalibur”);
TypeWeaponMedium.Add(“Sabre”);
TypeWeaponMedium.Add(“Two-handed sword”);

TypeWeaponHeavy.Add(“Claymore”);
TypeWeaponHeavy.Add(“Chained-ball”);
TypeWeaponHeavy.Add(“Mace”);
TypeWeaponHeavy.Add(“Trunk”);
TypeWeaponHeavy.Add(“Sword”);

WeaponSuffixEsoteric.Add(“of Lament.”);
WeaponSuffixEsoteric.Add(“of Spectres.”);
WeaponSuffixEsoteric.Add(“of The Reaper.”);
WeaponSuffixEsoteric.Add(“of Baphomet.”);

WeaponSuffixTechnologic.Add(“2.0”);
WeaponSuffixTechnologic.Add(“of The Internet.”);
WeaponSuffixTechnologic.Add(“of Electricity.”);
WeaponSuffixTechnologic.Add(“of Intranetz.”);
WeaponSuffixTechnologic.Add(“of Leeroy Jenkins.”);

WeaponSuffixNaturalistic.Add(“of Gaia.”);
WeaponSuffixNaturalistic.Add(“of Mother Earth.”);
WeaponSuffixNaturalistic.Add(“of The Land.”);
WeaponSuffixNaturalistic.Add(“of Trents.”);

What we’re doing with those long lines of code is adding a string each time we call on the .Add methodn of each list. We’re populating the lists with bits and pieces.

Now we have our lists, we have our name, good, great, excellent! However since we created the lists and it’s contents in another class we won’t be able to access them from our main Form, “Form1”.

We must create access properties. Write this down in your ItemLists class.

public List<string> pSizeLight
{
get { return SizeLight; }
set { SizeLight = value; }
}

public List<string> pSizeMedium
{
get { return SizeMedium; }
set { SizeMedium = value; }
}

public List<string> pSizeHeavy
{
get { return SizeHeavy; }
set { SizeHeavy = value; }
}

/************************************/

public List<string> pTypeWeaponLight
{
get { return TypeWeaponLight; }
set { TypeWeaponLight = value; }
}

public List<string> pTypeWeaponMedium
{
get { return TypeWeaponMedium; }
set { TypeWeaponMedium = value; }
}

public List<string> pTypeWeaponHeavy
{
get { return TypeWeaponHeavy; }
set { TypeWeaponHeavy = value; }
}

/************************************/

public List<string> pWeaponSuffixEsoteric
{
get { return WeaponSuffixEsoteric; }
set { WeaponSuffixEsoteric = value; }
}

public List<string> pWeaponSuffixTechnologic
{
get { return WeaponSuffixTechnologic; }
set { WeaponSuffixTechnologic = value; }
}

public List<string> pWeaponSuffixNarutalistic
{
get { return WeaponSuffixNaturalistic; }
set { WeaponSuffixNaturalistic = value; }
}


What this code is doing is letting us access the lists from anywhere. That’s all you need to know. 😀

Ok, we’re done working on the ItemLists class. Now let’s move on to Form1.

First things first, we need to create an instance of the ItemLists class to use all the methods and attributes in that class. Write this in your Form1 Class:

ItemsLists oItemLists = new ItemsLists();

Now we can use the Lists we create in the other class by calling on the oItemLists object.

We want the program to randomly select items from the list and use them, so let’s create a Random object. Write this in your Form1 Class:

Random RandomSelector = new Random();


We also want the final string that our program generates to be saved, so create a string array. Write this in your Form1 Class:

string[] GeneratedWeaponName = new string[2];


Now let’s get down and dirty, go the design tab of your form and double click on your picturebox. Program the following in the Click event:

switch (comboBox1.SelectedIndex)
{
case 0:
int a = RandomSelector.Next(0, oItemLists.pSizeLight.Count);
int b = RandomSelector.Next(0, oItemLists.pTypeWeaponLight.Count);
string SizeLight = oItemLists.pSizeLight[a];
string WeaponLight = oItemLists.pTypeWeaponLight[b];
string GeneratedNameA = SizeLight + ” ” + WeaponLight;
GeneratedWeaponName[0] = GeneratedNameA;
break;

case 1:
int c = RandomSelector.Next(0, oItemLists.pSizeMedium.Count);
int d = RandomSelector.Next(0, oItemLists.pTypeWeaponMedium.Count);
string SizeMedium = oItemLists.pSizeMedium[c];
string WeaponMedium = oItemLists.pTypeWeaponMedium[d];
string GeneratedNameB = SizeMedium + ” ” + WeaponMedium;
GeneratedWeaponName[0] = GeneratedNameB;
break;

case 2:
int f = RandomSelector.Next(0, oItemLists.pSizeHeavy.Count);
int g = RandomSelector.Next(0, oItemLists.pTypeWeaponHeavy.Count);
string SizeHeavy = oItemLists.pSizeHeavy[f];
string WeaponHeavy = oItemLists.pTypeWeaponHeavy[g];
string GeneratedNameC = SizeHeavy + ” ” + WeaponHeavy;
GeneratedWeaponName[0] = GeneratedNameC;
break;
}

switch (comboBox2.SelectedIndex)
{
case 0:
int a = RandomSelector.Next(0, oItemLists.pWeaponSuffixEsoteric.Count);
string WeaponSuffixA = oItemLists.pWeaponSuffixEsoteric[a];
GeneratedWeaponName[1] = WeaponSuffixA;
break;

case 1:
int b = RandomSelector.Next(0, oItemLists.pWeaponSuffixTechnologic.Count);
string WeaponSuffixB = oItemLists.pWeaponSuffixTechnologic[b];
GeneratedWeaponName[1] = WeaponSuffixB;
break;

default:
int c = RandomSelector.Next(0, oItemLists.pWeaponSuffixNarutalistic.Count);
string WeaponSuffixC = oItemLists.pWeaponSuffixNarutalistic[c];
GeneratedWeaponName[1] = WeaponSuffixC;
break;
}

Oh shit! Don’t be intimidated by this code, it’s incredibly simple to understand once you follow along with it.

Let’s look at the first part. It’s a switch code, that accepts a number from the SelectedIndex from a ComboBox.

Ok, when a user clicks on our pictureBox1, we’ll act according to what the selectedIndex is. Simple enough.

Now let’s look at our cases, if the selectedIndex is zero, the program does the following:

Gets a random number from o to the ammount of items our list has. So if our list has 10 items, it will generate a number from 0 to 10.

It does the same for the other list as well, saving the number to variables a and b respectively.

Then using those numbers as indexes it retrieves the item from the list in position a, and saves the string to a string variable.

After that the program simply concatenates both retrieves strings and saves it to another string, which in turn is saved to the string array we declared earlier in our program.

Up to this point, we have generated the first part of our weapon name. So if we generated the name, the output would be:

Cumbersome Two-Handed Axe

Do the same for the weapon suffixes and we have ourselves a fully fledged name generator!

I hope you guys have enjoyed reading this tutorial. I encourage reader to please send me feedback so I can improve in areas I’m lacking. This is my first blog after all.

See ya soon!

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