CMPT 225 : Lab 4

in numerous situations that go well beyond storing a key and an element. ... The language at the end of this section is like a conversation: you say different things ...
36KB taille 4 téléchargements 339 vues
CMPT 225 : Lab 4 Executing a script through a Tree Due date: July 1st 11:59PM. Online submission only. I. Outline We have mainly used trees in order to store data and implement the Dictionary ADT. Trees are used in numerous situations that go well beyond storing a key and an element. For example, to interpret a program, we convert it to a representation based on trees and going through the tree corresponds to executing one instance of the program. In order to represent a program, we associate an action to a Node, and two nodes are linked by a transition that may be satisfied depending on the value of some variables. In this lab, you will create actions and transitions of increasing complexity, which goes from a toy language to a small scripting language. II. I’m too young to die! The language at the end of this section is like a conversation: you say different things according to what the user tells you. The nodes show messages and the transitions are based on the user’s answer. 0.5 pt. In a package Transition, write an interface Condition with one function “public boolean isSatisfied(String s)”. Write a class TextCondition that implements this interface: the constructor takes a String as argument and the condition is satisfied if the strings are the same. 0.75 pt. In the package Transition, write a class Transition that has a Node (given by the constructor) and a list of Condition. Write the methods addCondition that will add a condition to the list, isSatisfied(String s) that returns true when at least one condition is satisfied and false otherwise, and destination that returns the Node. 0.75 pt. In a package Node, write an interface Node with two methods public Node execute(); and public void addTransition(Transition t). Write a class DialogNode that implements the interface, has a list of transitions and takes a String in the constructor. The method execute will print the String given in the constructor and take a line from the keyboard (i.e. the user will write a line); if any transition satisfies the line, then the method returns the Node associated with that transition, and null otherwise. If you don’t know how to get a line from the user, Google is your friend. 0.5 pt. In the Main, write a function test1. Use the previous classes to obtain the following behaviour: a question is asked to the user, a message A of your choice is displayed upon one answer, and a message B is displayed for another answer. In other words, you have a starting node that asks a question, and from which two transitions go to two nodes that will print a message. III. Hurt me plenty. Some of you have asked to write a game. A game has a graphic engine and a script engine. We will extend the actions and conditions to come closer to a basic script engine acting on a player. 1 pt. In the package Actor write a class Player. A player has a health, a total of coins and a list of items simply given by their name. Write the accessors for the health and the coins, and a method hasItem(String item) that returns true if an item is in the list. Write two functions damage and heal that respectively decrease and increase the health by a specified amount. Write two functions moneyOut and moneyIn that respectively decrease and increase the amount of coins by a specified amount. 1 pt. The following conditions all need to be at least provided the player in the constructor. Additional variables depend on the expected behaviour. The condition ItemCondition will be satisfied if the player has a given item, the condition MoneyCondition is satisfied if the player has at least some amount of coins, and the condition LifeCondition is satisfied if the player has at least the specified health. The condition trueCondition is always satisfied.

Philippe Giabbanelli

1

CMPT 225

0.5 pt. Create a TakeMoney node that, instead of printing a message, will take a specified amount of money from the user. Similarly, create an AddItem node that will add an item to the player’s list, and a TakeItem that takes an item from the list. 1 pt. In the Main, write a function Node scriptSell that builds a tree involving selling an item to the user and returns the root. Write a function runScript that takes a Node as an argument and will follow the nodes executed until the end (i.e. until the last Node, which will be null). IV. Nightmare! 4 pts. Write a function Node getScript(String file) that will build a script from the specified file and returns the root for execution. The syntax in the file is as follows: Show: Would you like to buy a sword? IfAnswer: Yes :and: IfMoney: 40 Show: Thank you AddItem: Sword TakeMoney: 40 Show: Do you want anything else? IfAnswer: Yes Show: Too bad… Else: Show: Good! Else: Show: Too poor. Some of the things that you’ll need. To divide a line into words, you need a StringTokenizer. This takes the line as an argument and provides you with each word, using an empty space to separate words. http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html You can easily find examples of how to read a file, such as: http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml A strategy to write it Converting a line starting with AddItem: or TakeMoney: into a Node is the easiest case. For a Show: you may assume that it behaves similarly by just displaying the message and going to the next one; it is only when a Show: is followed by an If that includes IfAnswer: that the user’s input is required. You may want a node dedicated to an If/Else: this is the main difficulty because it is what introduces branches in the tree. Turning a line starting with If into a list of conditions also makes it easier. Finally, you will need to use a Stack to handle If/Else: you may have an If, then a bunch of lines, another If, some lines, and an Else. The conditions in the else should be attached to the last If. Don’t try to do all of it all at once. Do as much as you can and be structured (write functions). The breakup of the points is: 0.75 pt for having AddItem, TakeMoney and TakeItem work 1 pt for having Show work (including when we need the user’s input) 2.25 pts for having If/Else work

Philippe Giabbanelli

2

CMPT 225