Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

approach to take and drop

By: ace on Aug 11th, 2010  |  syntax: None  |  size: 4.35 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. General approach to implementing commands take and drop:
  2. 1. Add the commands, without any content. This involves:
  3.         - add the words to the list of command words
  4.         - add the words to the list of commands in the processCommand method of Game
  5.         - add method signatures for take() and drop() to Game. Make the method body something trivial like a println.
  6.         - test to make sure this works before proceeding.
  7. 2. In this check I notice that the items are not showing up in the pub as expected. Fix this bug.
  8. 3. Consider what I want take() to do: I want the PLAYER to be able to choose an item from the room and TAKE it by name. This means that the user will need to be able to type a command like "take beertap". The flow of control will go something like this: the USER gets a list of items (for example from the look() command). The USER types "take beertap." The PLAYER asks the currentRoom (which is a member of the ROOM class) if it has an item named "beertap." If it does it returns the item to the player. If it does not then it returns NULL and the take() fails.
  9. 4. Consider how this is similar to the drop() command. In that case we want to check if the item is in the PLAYER's inventory, and if it is transfer it to the currentRoom (which is of the ROOM class). If it is not then the drop() fails.
  10. 5. It is probably easiest to start at the end of the chain of events described in (3). This means adding a method to the ROOM class which enables us to ask for an item and get it if the room has it (or get NULL if it doesn't.) The checkItem() method almost does this already; modify it to return the item rather than returning true and return null rather than returning false. We also need it to remove the item from the ROOM's array list.
  11. 6. Now we add a method to Player which allows it to check the currentRoom for an item.
  12. 7. In writing this method we note that the PLAYER has nowhere to store items. Add some storage for the player's items (an ArrayList will do). Right now items can only be added to it via TAKEing them, but that's OK for now.
  13. 8. Now we need the UI command to reach the PLAYER. This will be in the UI layer in the Game class, which means we finally modify our trivial take() command. Look at other methods which use a second command word to get an idea of what to do. We see that we will need to pass the Command object to take() as a parameter, so it becomes take(Command command) . (We should change drop() at the same time to drop(Command command).) In goRoom the command fails if there is no second word. We probably want similar behavior for our TAKE command so we duplicate that code. If we have a second command word then we just want to pass it to the PLAYER by calling the method we wrote in step 6. However, we want the UI to respond appropriately if the take was successful or not, so we should change the method in PLAYER to return something (either a String or a boolean) which tells us if the TAKE succeeded. I will opt for a boolean and decide what to tell the player on the UI level.
  14.         - change the method in PLAYER to return a boolean
  15.         - change the take(Command command) method in GAME to print a message like "you add ITEM to your inventory" if the method in PLAYER returns true, and "There is no ITEM in here" if it returns false.
  16. 9. Test the TAKE command.
  17. 10. Consider the DROP command. This will work similarly, except we want the PLAYER to check if they have the item and if they do, to give it to the currentRoom.
  18. 11. The ROOM class will need to be able to accept the item. It already has a setItems method so we do not need to add anything, we will have PLAYER call this when they drop the item.
  19. 12. This means most of the changes will happen in PLAYER. Add a DROP method to PLAYER which takes an Item a a parameter, gives it to the currentRoom, and removes it from the player's inventory (ArrayList).
  20. 13. Now you clearly need a method which looks very much like the checkItem method in ROOM. It simply checks to see if the item is in the PLAYER's ArrayList. The difference is, instead of returning the ITEM, it calls DROP with it. We can name this method dropItem (similar to the takeItem method we wrote earlier).
  21. 14. Now we want the game class to know whether the dropItem succeeded. Let's make it a boolean, like takeItem.
  22. 15. Now all we have to do is change the trivial drop(Command command) method in GAME to call the dropItem method in PLAYER and print something sensible to the UI.