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

Gender and Alignment

Well! We have these characters, but they are boring and faceless! Let's fix that!

Characters need a gender and alignment. That will go a good way in making them “real” in the player's mind!

This stuff is pretty straightforward! We've messed with enumerations and accessor methods before! I'm not going to post complete listings here, but you can get the full code for this chapter out of the Subversion Repository.

Critter.java (Interface) Modifications

Let's add some gender and alignment enumerations and accessor method prototypes to our Critter interface!

// ...snip... 
 
public interface Critter 
{ 
 
// ...snip... 
 
	public enum Gender 
	{ 
		NONE, 
		MALE, 
		FEMALE, 
		BOTH 
	} 
 
	public enum Alignment 
	{ 
		LAWFUL, 
		NEUTRAL, 
		CHAOTIC 
	} 
 
// ...snip... 
 
	public Gender getGender(); 
 
	public Alignment getAlignment(); 
 
// ...snip... 
}

PlayerCharacter.java (Class) Modifications

And here are the changes to the concrete PlayerCharacter class!

// ...snip... 
 
public class PlayerCharacter implements Critter 
{ 
 
// ...snip... 
 
	private Gender		gender; 
	private Alignment	alignment; 
 
// ...snip... 
 
	public PlayerCharacter() 
	{ 
		// Roll stats.  Straight 3d6. 
		for( int i=0; i<stats.length; i++ ) 
			stats[i] = Dice.d(3,6); 
		// Roll randomly for gender. 
		if( Dice.d( 2 ) == 1 ) 
			gender = Gender.MALE; 
		else 
			gender = Gender.FEMALE; 
		// Roll randomly for alignment. 
		int roll = Dice.d( 3 ); 
		if( roll == 1 ) 
			alignment = Alignment.LAWFUL; 
		else if( roll == 2 ) 
			alignment = Alignment.NEUTRAL; 
		else 
			alignment = Alignment.CHAOTIC; 
	} 
 
// ...snip... 
 
	@Override 
	public Gender getGender() 
	{ 
		return gender; 
	} 
 
	@Override 
	public Alignment getAlignment() 
	{ 
		return alignment; 
	} 
} 

Test it!

To test it, we'll just add a little code to our Test class!

 ...snip... 
 
	@Override 
	public void run( SimpleIO io ) 
	{ 
		// Generate a character. 
		Critter character = new PlayerCharacter(); 
		// Print the character's level, alignment, gender, and class. 
		// (Everyone is a barbarian for now!) 
		io.print( "Level 1 " ); 
		io.print( caps(character.getAlignment().name() ) ); 
		io.print( " " ); 
		io.print( caps(character.getGender().name() ) ); 
		io.println( " Barbarian" ); 
		io.println(); 
		// Loop through all of the character's stats and print them. 
		for( Stat stat : Stat.values() ) 
		{ 
			// The name of the stat, with capitalization fixed, followed by the stat's value. 
			io.print( caps( stat.toString() ) + ": " + character.getStat( stat ) ); 
			// If the stat modifier is nonzero, then we print it, too. 
			int mod = character.getStatModifier( stat ); 
			if( mod != 0 ) 
			{ 
				io.print( " (" ); 
				// Positive numbers don't automatically print with a "+" in front of them, so we add it. 
				if( mod > 0 ) 
					io.print( "+" ); 
				io.print( mod ); 
				io.print( ")" ); 
			} 
			io.println(); 
		} 
	} 
 
...snip...

Easy peasy!

What's Next?

In the next chapter, we'll go out on a limb and implement something a bit advanced! I'll leave you in suspense for now, but it'll be cool!

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/005_gender_and_alignment.txt · Last modified: 2012/09/09 00:29 (external edit)

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!