| Home | Let's Build an RPG! | Deconstructing B/X D&D | Adventure Generator! | B/X D&D Character Generator | Various Java | Contact |
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.
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... }
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; } }
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!
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!
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.