at-m42:casestudies:cs03
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:casestudies:cs03 [2009/04/12 14:34] – eechris | at-m42:casestudies:cs03 [2011/01/14 12:59] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Adventure Game (Objects) ===== | ||
+ | This case study is intended to be used for self study. It is designed to reinforce the materials covered in lectures and practised in the laboratories. It also forms the basis for the [[project|project work]]. | ||
+ | |||
+ | |||
+ | An index to the source code for all the examples in this case study is [[/ | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | The adventure game first appeared in [[at-m42: | ||
+ | |||
+ | This case study is applied to the same problem domain using the object-oriented concepts introduced in the first part of [[at-m42: | ||
+ | |||
+ | |||
+ | ===== Specification ===== | ||
+ | |||
+ | We assume a sufficient familiarity with the operation of a text-based adventure game to understand the following description: | ||
+ | > //The game has a name, holds a number of items each of which has a name, optional description, | ||
+ | |||
+ | These requirements are captured in the use cases: | ||
+ | * Add a new item to the game | ||
+ | * Record the pick-up and dropping of an item | ||
+ | * Display the details of the stock of items | ||
+ | * Register a new player | ||
+ | * Display details of the players | ||
+ | | ||
+ | The adventure game system we are asked to develop is relatively non-trivial and so it merits developing it in small iterations. For the first two iterations, we aim to demonstrate that our model is a good reflection of the problem domain. Clearly, if is not, then the rest of the development effort is severely jeopardized. In the [[# | ||
+ | |||
+ | ===== Iteration I: An Initial Model ===== | ||
+ | |||
+ | The specification mentions that that the game holds items. This suggests a class diagram similar to that shown in Figure 1 (seen also in [[at-m42: | ||
+ | |||
+ | In the example in the [[at-m42: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | **Figure 1**: initial class diagram | ||
+ | |||
+ | <code groovy 1| Game 01: Initial object configuration (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | Note that we have the closure' | ||
+ | |||
+ | <code groovy> | ||
+ | inventory.each { id, item -> println item } | ||
+ | </ | ||
+ | even though we only make use of the value. This helps to make it clear that '' | ||
+ | |||
+ | When we execute the Groovy script the output is: | ||
+ | |||
+ | Game: The Discworld | ||
+ | ===================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | Item: 3; name = A bag of gold: value = 100; | ||
+ | |||
+ | The display reveals that the correct architecture is established and that we have the correct behaviour from our two classes. Therefore we consider the first iteration to be complete. | ||
+ | |||
+ | ===== Iteration II: Augment the Model ===== | ||
+ | |||
+ | We now need to introduce the notion of registered players into our model. The specification states that they are defined by a unique identification number, a nickname and an email address. Players are permitted to pick up and drop items. We capture this information with a class diagram as shown in Figure 2. The '' | ||
+ | |||
+ | {{: | ||
+ | |||
+ | **Figure 2**: Introducing players | ||
+ | |||
+ | The implementation of Figure 2 involves introducing the '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code groovy 1| Game 02: Text-based user interface (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | As with the previous iteration, when we run this script, the results reveal that the classes behave as expected. | ||
+ | | ||
+ | | ||
+ | | ||
+ | Game: The Discworld | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | Item: 3; name = A bag of gold: value = 100; | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Player Details | ||
+ | ========================================== | ||
+ | Player: 1; Rincewind (p1@diskworld.com) | ||
+ | Player: 2; Twoflower (p2@diskworld.com) | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Available items | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | Item: 3; name = A bag of gold: value = 100; | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Available items | ||
+ | ========================================== | ||
+ | Item: 3; name = A bag of gold: value = 100; | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Items being carried | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Player Details | ||
+ | ========================================== | ||
+ | Player: 1; Rincewind (p1@diskworld.com) | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | Player: 2; Twoflower (p2@diskworld.com) | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Available items | ||
+ | ========================================== | ||
+ | Item: 2; name = Pointy hat: value = 10; | ||
+ | Item: 3; name = A bag of gold: value = 100; | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Items being carried | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | | ||
+ | | ||
+ | Game: The Discworld : Player Details | ||
+ | ========================================== | ||
+ | Player: 1; Rincewind (p1@diskworld.com) | ||
+ | Player: 2; Twoflower (p2@diskworld.com) | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | |||
+ | ===== Iteration III: Reimplement the user interface ===== | ||
+ | |||
+ | The previous iteration exercised the code by a " | ||
+ | |||
+ | The menu is readily implemented with some simple procedural code. The method '' | ||
+ | |||
+ | Although the interface is not especially difficult to implement, in the future, we may need to convert the application to have a graphical user interface or a web interface. If we consider the model developed in the previous iteration, the a design weakness becomes apparent. It is that the '' | ||
+ | |||
+ | A better design would be to ensure that the classes such as '' | ||
+ | |||
+ | A useful approach is to introduce a new object whose responsibility is to interact with the domain model and also be responsible for input and output, For this purpose, we introduce an '' | ||
+ | |||
+ | {{: | ||
+ | |||
+ | **Figure 3**: Incorporating the '' | ||
+ | |||
+ | The final listing is given in Game 03. The main application code handles the presentation of the menu and the selection made by the user. The user choice is then routed to one of the '' | ||
+ | |||
+ | |||
+ | |||
+ | <code groovy 1| Reimplement the user interface (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | Of course we should test that we have the same functionality as before. This is easily accomplished by making the menu choices correspond to the " | ||
+ | |||
+ | < | ||
+ | <pre> | ||
+ | e: | ||
+ | |||
+ | 0: Quit | ||
+ | 1: Add new item | ||
+ | 2: Display inventory | ||
+ | 3: Display available items | ||
+ | 4: Display items being carried | ||
+ | 5: Register new player | ||
+ | 6: Display players | ||
+ | 7: Pick up an item | ||
+ | 8: Drop an item | ||
+ | |||
+ | Enter choice>>>> | ||
+ | Enter item id:< | ||
+ | Enter item name:< | ||
+ | Enter item value:< | ||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | Enter player id:< | ||
+ | Enter player nickname:< | ||
+ | Enter player email address:< | ||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld : Player Details | ||
+ | ========================================== | ||
+ | Player: 1; Twoflower (tf@discworld.com) | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | Enter item id:< | ||
+ | Enter player id:< | ||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld : Available items | ||
+ | ========================================== | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld : Items being carried | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Enter item id: < | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld : Available items | ||
+ | ========================================== | ||
+ | Item: 1; name = Luggage: value = 1000; description: | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game: The Discworld : Items being carried | ||
+ | ========================================== | ||
+ | |||
+ | // ... | ||
+ | // Present menu to the user | ||
+ | Enter choice>>>> | ||
+ | |||
+ | Game closing ... thanks for playing | ||
+ | |||
+ | e: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Having established that we have the same outcomes, we consider this iteration and therefore Case Study 3 complete. | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | [[at-m42: |
at-m42/casestudies/cs03.txt · Last modified: 2011/01/14 12:59 by 127.0.0.1