Swin-Adventure C# Implementation Plan
Swin-Adventure C# Implementation Plan
Identifiable Object
-_identifiers: List<string>
+ Identifiable Object(string[] idents)
+ AddIdentifier ( string id )
The player needs to be able to "identify" a number of things in the game. This includes com-
mands the user will perform, items they will interact with, locations, paths, etc. The Identifiable
Object role was created to encapsulate this functionality.
■ Identifiable Object
■ The constructor adds identifiers to the Identifiable Object from the passed in array.
■ Are You checks if the passed in identifier is in the _identifiers
■ First Id returns the first identifier from _identifiers (or an empty string)
■ Add Identifier converts the identifier to lower case and stores it in _identifiers
Object Oriented Programming Swin-Adventure C# Implementation Plan
Use the following unit tests to create the Identifiable Object class and ensure that it is working
successfully.
Page 2 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Identifiable
Object
Inventory
- _items : List<Item>
+ Inventory( )
Item
+ HasItem( string id ) : bool
+ Item(string[] idents, string name, string desc) *
+ Put(Item itm) : void
+ Take(string id) : Item
+ Fetch(string id) : Item
Player + ItemList : string <<readonly, property>>
- _inventory: Inventory
Notes:
■ The Player constructor will call the GameObject constructor and pass up identifiers for
"me" and "inventory".
Page 3 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
This iteration adds several classes that help create a number of key abstractions for the game.
The Game Object class will be used to represent anything that the player can interact with.
■ Game Object - "anything" the player can interact with
■ Name - this is a short textual description of the game object
■ Description - a longer textual description of the game object
■ Short Description - returns a short description made up of the name and the first id of
the game object. This is used when referring to an object, rather than directly examin-
ing the object.
■ Long Description - By default this is just the description, but it will be changed by child
classes to include related items.
Example from requirements document:
Command -> inventory
You are Fred the mighty programmer.
You are carrying
a shovel (shovel)
a bronze sword (sword)
a small computer (pc)
The inventory command is the same as "look at me", so this shows the Full Description of the
Player. It includes the short description if items the player is carrying, for exam "a shovel" is the
name of an item the player is carrying. The first id of this item is "shovel", so its Short Descrip-
tion is "a shovel (shovel)".
In the design the Item class is a kind of Game Object. This is an abstraction of the "Items" the
player can interact with in the game. Example item:
Player is also a kind of Game Object. This will be a object through which the player will inter-
act with the game world.
■ Player - the players avatar in the game world
■ An Inventory object is used to manage the Player's items
■ Full Description is overridden to include details of the items in the player's inventory.
■ Locate "finds" a GameObject somewhere around the player. At this stage that includes
the player themselves, or an item the player has in their inventory. See next page...
A number of GameObjects will need to contain Items. This functionality is encapsulated in the
Inventory class. This provides a managed list of items.
■ Inventory - a managed collection of items
■ Items can be added using Put, or removed by id using Take
■ Fetch locates an item by id (using AreYou) and returns it
Page 4 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
The following UML sequence diagrams shows the sequence of messages involved in locating
the player and their items.
The player can "locate" themselves.
p : Player
Yes / True
The player can locate items in their inventory. Note: pi is the player's inventory object.
No / False
Fetch "sword"
AreYou "sword"
No / False
AreYou "sword"
Yes / True
sword
sword
No / False
Fetch "club"
null / nil
null / nil
Page 5 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Page 6 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Iteration 3 - Bags
In this iteration you will add a Bag class to make it possible to have items that contain other
items.
Item
Inventory
Bag
- _inventory: Inventory
The Bag abstraction is a special kind of item, one that contains other items in its own Invento-
ry. This is a version of the composite pattern, which allows flexible arrangements of bags and
items, for example a bag to contain another bag.
Page 7 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Iteration 4 - Looking
In this iteration you will add the first of the commands, the look command. This will give you
sufficient code to create a small application where the user can look at items they have.
<< abstract >> Identifiable
Command Object
+ Look Command ( )
- LookAtIn ( string thingId, IHaveInventory container ) : string << implements >> << implements >>
uses
As there will be a number of Commands, an abstract Command class has also been added.
This class inherits from Identifiable Object, as each of the commands needs to be identifi-
able. When data is entered by the user, each Command object will be asked “Are You” and the
first work of the command. For example, with “look at pen” each Command would be asked
“Are You ‘look’ ” to locate the Command to process the text. As a result the Look Command
should be identified by “look”.
Page 8 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
To handle this the Look Command will need to perform a set of action:
1. Locate the “container” where the item resides. For example the bag in “look at pen in
bag”, or the player in “look at pen”.
■ Return “I cannot find the <<container text>>” if the container cannot be found. For ex-
ample, “look at pen in bag” may return “I cannot find the bag”.
2. Locate the item from the “container”
■ Return “I cannot find the <<item text>> in the <<container name>>” if the item cannot
be found. For example, “look at pen in bag” may return “I cannot find the pen in the
small bag” (in this case “small bag” is the name of the Bag container).
3. Return the full description of the Game Object found.
For this to work the code needs a way of treating Bags and Players in the same way: as ob-
jects that container other items. To handle this the I Have Inventory interface/protocol has
been added. Objects that implement this must be able to perform the following tasks for the
Look Command:
■ Locate and item given its id. Once the container is located from the Player, it is then
asked to locate the item from the text id the user entered.
■ The container needs to be able to return its Name. This can then be used when the item
cannot be found in the container.
The command input will be supplied as a list of individual words. For example: “look at pen in
bag” will be passed in as the list [ “look”, “at”, “pen”, “in”, “bag” ]. The conversion of the text
from a single string to a list will be handled elsewhere.
Processing the array will be performed as follows:
1. There must be either 3 or 5 elements in the array, otherwise return “I don’t know how to
look like that”
2. The first word must be “look”, otherwise return “Error in look input”
3. The second word must be “at”, otherwise return “What do you want to look at?”
4. If there are 5 elements, then the 4th word must be “in”, otherwise return “What do you
want to look in?”
5. If there are 3 elements, the container is the player
6. If there are 5 elements, then the container id is the 5th word
1. Call FetchContainer, and have this method retrieve the container from the Player.
7. The item id is the 3rd word
8. Perform the look at in, with the container and the item id
Page 9 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
The following shows the sequence of messages involved in performing the command "look at
gem". In this case the container is the player themselves, though the logic is the same as
when it is another object that matches the I Have Inventory interface/protocol.
Locate "gem"
gem
Full Description
"…"
"…"
"…"
Page 10 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
The following sequence diagrams shows the command "look at gem in bag". When the com-
mand is "look at x in y" then the Locate Container method asks the player to locate "y", and
returns this as the container to locate "x".
bag
Locate "gem"
gem
Full Description
Notes:
■ You will need to cast the GameObject to IHaveInventory use the following to per-
form a safe type cast. This will set container to null if obj is not an object that implements
the I Have Inventory interface/protocol.
Page 11 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Page 12 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Tasks:
■ Draw a UML class diagram to show what needs to be added
■ Draw a UML sequence diagram to explain how locate works in the Player, with the newly
added Location aspect to the search.
■ Implement the unit tests, and features to support them.
Page 13 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Tasks:
■ Draw a UML class diagram to show what needs to be added
■ Draw a sequence diagram to explore how moving will work. For example: execute "move
north" is sent to a Move Command.
■ Implement the unit tests, and features to support them.
Page 14 of 15
Object Oriented Programming Swin-Adventure C# Implementation Plan
Tasks:
■ Draw a UML class diagram to show what needs to be added
■ Draw a sequence diagram to explore how executing a command works (ignoring the in-
ternal details of the Commands). For example "look at gem in bag" being sent to the
Command Processor.
■ Implement the unit tests, and features to support them.
■ Convert the application to use the Command Processor
Iteration 9+
Plan out iterations for:
■ Transfer Command to perform put and take
■ Maze loading from text file
■ Quit Command
■ GUI anyone? Creating a simple GUI using Windows Forms in C# is not overly challeng-
ing, even without the visual design tools from Visual Studio.
Page 15 of 15