The Mighty Warrior Models Classes
The Mighty Warrior Models Classes
The mighty warrior reached into her pack, and pulled out her favorite weapon, the mace Firestarter that would burst into flame upon command. She had acquired it from a store she had visited back in the city.
She walked carefully down the hallway of the ruined castle, as quietly as she could while wearing plate armor. She reached a doorway, and gently pushed the great stone door open. A zombie stood just inside the room. She quickly dispatched it with the mace.
The room contained some tattered furniture, including a bed, a small table, and two chairs. There was also a container, a fine solid (and locked) chest.
Candidate Classes
“Wait!” cried the analyst. “There are too many things to keep track of here. What kinds of things are there in this place?”
Our heroine decided that it was time to create a class model. How could she figure out what classes the game would include? She remembered that a class represents a kind of thing, and kinds of things are described in English using common nouns. It was time to underline all of the common nouns in the opening paragraphs.
The mighty warrior reached into her pack, and pulled out her favorite weapon, the mace Firestarter that would burst into flame upon command. She had acquired it from a store she had visited back in the city.
She walked carefully down the hallway of the ruined castle, as quietly as she could while wearing plate armor. She reached a doorway, and gently pushed the great stone door open. A zombie stood just inside the room. She quickly dispatched it with the mace.
The room contained some tattered furniture, including a bed, a small table, and two chairs. There was also a container, a fine solid (and locked) chest.
She then took the underlined words, removed duplicates, and made a list:
Warrior
Pack
Weapon
Mace
Firestarter
Store
City
Hallway
Castle
Armor
Doorway
Door
Zombie
Room
Furniture
Bed
Table
Chair
Container
Chest
What a fine list of candidate classes! Something bothered her about Firestarter though. It wasn’t a kind of thing. It was a very particular special one. Then she remembered! Candidate classes should only be common nouns. Firestarter was a proper noun: it was the name of a specific object of the class Mace, not a class of its own.
Inheritance Hierarchies
Wait a second! Was it really an object of the class Mace, or of the class Weapon? It seemed to be both! How could that be?
Suddenly, she had it. Mace was a kind of Weapon. That meant that Mace was a subclass of Weapon, which would have all of the facts and behaviors common to Weapon, and some that were special to Mace.
Were there other subclasses of Weapon? Sure there were! There were swords, and axes, and bows, and slings, and several more. Perhaps each of these should be a subclass as well.
Yet somehow bows and slings were more like one another than they were like maces and swords and axes. They each had to use projectiles (arrows and rocks), while the others didn’t. The inheritance hierarchy was more complex yet! Weapons could be Ranged or Melee. Ranged Weapons could be bows or slings. Melee Weapons could be maces or swords or axes.
Now some facts and behaviors would belong to Weapon (e.g., the behavior attack), some would belong to Ranged Weapons (e.g., how far the weapon could shoot, or what projectiles it took) or Melee, and some to specific kinds of weapon (e.g., the sword could have sharpness).
Were there other subclasses on the list! Yes! Bed, Chair and Table are kinds of furniture, and Chest is a kind of container. She would have to think about other kinds of furniture and containers too.
Associations
Was that all she needed to do? Our heroine thought back to the hallway:
The mighty warrior reached into her pack, and pulled out her favorite weapon, the mace Firestarter that would burst into flame upon command. She had acquired it from a store she had visited back in the city.
She walked carefully down the hallway of the ruined castle, as quietly as she could while wearing plate armor. She reached a doorway, and gently pushed the great stone door open. A zombie stood just inside the room. She quickly dispatched it with the mace.
The room contained some tattered furniture, including a bed, a small table, and two chairs. There was also a container, a fine solid (and locked) chest.
Some of these clearly suggested an association between objects, and some an action taken. She thought for a while, and organized them into two lists.
Associations:
Warrior is wearing armor
Zombie stands inside room
Room contains furniture
Actions:
Warrior reaches into pack
Warrior pulls out weapon
Mace bursts into flame
Warrior reaches doorway
Warrior pushes door open
Warrior dispatches zombie
What object had these actions? In some sense, most of them were about unnamed associations. A warrior could only reach into a pack if the warrior was wearing the pack, and could only pull out a weapon that was contained in that pack. The action acted upon the object, such as the door that was pushed open. So she needed to assign those as methods on the object class, where they could change the state of the object.
Now she was making progress! All she need to do was start figuring out what facts she needed to track about the different classes, and then she could proceed to unlock and loot that chest!