Home Lawrence's Labyrinth Deconstructing B/X D&D Adventure Generator! B/X D&D Character Generator Other Contact
 

First PagePrevious PageBack to overviewNext PageLast Page

Expanded Enumerations and Gender Specific Pronouns

Well! We're getting closer to having a silly combat “game” going! We have characters being randomly rolled, and a quickie little character sheet printing to the console!

So let's start thinking about the first iteration of our combat system!

It is going to have to print messages to us as the combat progresses.

Hopefully interesting messages! Things like “Alaric the Awful strikes a mighty blow to Piotr the Pious, nearly severing his arm!”

If we read back over that example combat message, we see that it has a pronoun in it! We can't just refer to everyone as “he”! That Brynhildr from the last chapter will come to our Nerd Cave and stuff us into a sack if we do that! D:

So, we are going to have to find some kind of elegant way to associate a set of gender-specific pronouns with a critter. Otherwise our code will be chalk full of if() conditionals testing critter gender every time we try to print something!

Fortunately, the little-known and seldom used features of Java enumerations come to the rescue!

Enumeration Types are Actually Classes

Earlier, we defined the Gender enumeration:

	public enum Gender 
	{ 
		/** 
		 * Critter has no gender, and is referred to as 'it/it/its'. 
		 */ 
		NONE, 
		/** 
		 * Critter is male, and is referred to as 'he/him/his'. 
		 */ 
		MALE, 
 
		/** Critter is female, and is referred to as 'she/her/hers'. */ 
		FEMALE, 
		/** 
		 * I am not sure what to do with this, or if anything will even use it, 
		 * but for now we'll refer to dual-gendered critters as 'it/it/its'. 
		 */ 
		BOTH 
	}

That's pretty straightforward, right? We can define a Gender variable and assign some gender to it.

Gender gender = Gender.FEMALE;

If you're coming from a language like C++, you may be used to enumerations just being a fancy (if non-type-safe) way to define named integer constants.

But in Java, enums are really Classes, just like most everything else. And the fields/constants that the enum defines are instantiated as Objects of the enum's class!

	public enum Gender // <--- This is really a class! 
	{ 
		NONE,      // <--- This gets instantiated as an Object of the class Gender! 
		MALE,      // <--- This gets instantiated as an Object of the class Gender! 
		FEMALE,    // <--- This gets instantiated as an Object of the class Gender! 
		BOTH       // <--- This gets instantiated as an Object of the class Gender! 
	}

So! Just like with regular classes, we can define private variables and accessor methods on our enum! We can even define a constructor to initialize those private variables! Hold onto your hat!

	public enum Gender 
	{ 
		/** 
		 * Critter has no gender, and is referred to as 'it/it/its'. 
		 */ 
		NONE( "it", "it", "its", "its", "itself" ), 
		/** 
		 * Critter is male, and is referred to as 'he/him/his'. 
		 */ 
		MALE( "he", "him", "his", "his", "himself" ), 
		/** Critter is female, and is referred to as 'she/her/hers'. */ 
		FEMALE( "she", "her", "her", "hers", "herself" ), 
		/** 
		 * I am not sure what to do with this, or if anything will even use it, 
		 * but for now we'll refer to dual-gendered critters as 'it/it/its'. 
		 */ 
		BOTH( "it", "it", "its", "its", "itself" ); 
 
		private Gender( String sHeSheIt, String sHimHerIt, String sHisHerIts, String sHisHersIts, String sHimHerItself ) 
		{ 
			this.sHeSheIt = sHeSheIt; 
			this.sHimHerIt = sHimHerIt; 
			this.sHisHerIts = sHisHerIts; 
			this.sHisHersIts = sHisHersIts; 
			this.sHimHerItself = sHimHerItself; 
		} 
 
		final private String	sHeSheIt; 
		final private String	sHimHerIt; 
		final private String	sHisHerIts; 
		final private String	sHisHersIts; 
		final private String	sHimHerItself; 
 
		public String heSheIt() 
		{ 
			return sHeSheIt; 
		} 
 
		public String himHerIt() 
		{ 
			return sHimHerIt; 
		} 
 
		public String hisHerIts() 
		{ 
			return sHisHerIts; 
		} 
 
		public String hisHersIts() 
		{ 
			return sHisHersIts; 
		} 
 
		public String himHerItself() 
		{ 
			return sHimHerItself; 
		} 
	}

Shazam! Now each field in the enumeration gets a different list of pronouns when it is instantiated!

So we can do something like……

io.println( character.getGender().heSheIt() ); 

If the character is male, the word “he” will be printed to the console. Likewise, if the character is female, the word “she” will be printed instead.

Ok! So next let's --- Oh Shiny!

Just for giggles, I revisited that random table roller that we touched on in the last chapter, and converted some old PHP code for generating random adventure site names to the XML table specification format, and wrote a wrapper class extending Table for it.

You can take a gander at the XML file in the SVN repository.

We'll generate a random adventure site name in a bit!

Test It!

Let's add a little code to the printCritter() method of our Test.java.

	private static void printCritter( SimpleIO io, Critter critter ) 
	{ 
		// ... <snip> ... 
 
		io.println(); 
		Table tAdventureSites = new TableNamesAdventureSites(); 
		String adventureSiteName = tAdventureSites.random(); 
 
		StringBuilder sb = new StringBuilder(); 
		sb.append( caps( critter.getGender().heSheIt() ) ); 
		sb.append( " will soon gird " ); 
		sb.append( critter.getGender().himHerItself() ); 
		sb.append( " with sword and shield, preparing to test " ); 
		sb.append( critter.getGender().hisHerIts() ); 
		sb.append( " mettle in the dark cavernous depths of the " ); 
		sb.append( adventureSiteName ); 
		sb.append( "!" ); 
 
		io.println( sb.toString() ); 
	}

What's Next?

Having to use the StringBuilder to print just that one line of output is a total pain! In the next chapter, we'll do some work with advanced string processing, and write some code to make these gender-specific pronouns easier and cleaner to use!

Source Code

The source code for this chapter can be found in the Subversion Repository.

If you are interested in changing/modifying the code, you can download it into your Eclipse workspace using Subclipse as detailed here: TUTORIAL: Using Subclipse.

First PagePrevious PageBack to overviewNext PageLast Page

rustybox/book/008_expanded_enumerations_and_gender_specific_pronouns.txt · Last modified: 2012/09/25 09:13 by leaf

Copyright © 2009, 2012-2014 by L. Adamson, unless otherwise stated.
If you see something here that you like, and find it useful or learn something, please consider making a quick, easy, and secure donation via PayPal.
Your support is what keeps this whole thing going!