Assignment 6 Walkthrough Adventure CS 106 AX November
Assignment 6 Walkthrough: Adventure! CS 106 AX November 7, 2019 Slides by Anand Shankar
Plan for Today Classes in Python Tuples in Python Adventure Overview
Plan for Today Classes in Python Tuples in Python Adventure Overview
What are classes? • Classes: create a template for a new type of object • Object: combines state and behavior using attributes and methods
What are classes? • Classes: create a template for a new type of object • Object: combines state and behavior using attributes and methods • Attributes (aka “fields” or “instance variables”) define state: what information do you need to remember? • Methods that define behavior: what operations would you want to do on your class?
What are classes? • Classes: create a template for a new type of object • Object: combines state and behavior using attributes and methods • Attributes (aka “fields” or “instance variables”) define state: what information do you need to remember? • Methods that define behavior: what operations would you want to do on your class? • Example: Employee class • Attributes: name, salary, title • Behavior: get. Name, get. Salary, get. Title, give. Promotion
What are classes? • Classes: create a template for a new type of object • Object: combines state and behavior using attributes and methods • Attributes (aka “fields” or “instance variables”) define state: what information do you need to remember? • Methods that define behavior: what operations would you want to do on your class? • Example: Employee class • Attributes: name, salary, title • Behavior: get. Name, get. Salary, get. Title, give. Promotion • An object is an instance of a class • You have been using classes this whole quarter without realizing it! • When you create a GRect object, you’re creating an instance of the GRect class
Why bother with classes? • Client code: code that uses a class that you have defined
Why bother with classes? • Client code: code that uses a class that you have defined • The goal is to write client code that looks like this: rat 1 = Rational(3, 4) rat 2 = Rational(10, 53) total = rat 1 + rat 2 s = 'How are you today? ' scanner = Token. Scanner(s) while scanner. has. More. Tokens(): print(scanner. next. Token()) nums = [1, 2, 3, 4, 5] nums. append(6) last = nums. pop() frequencies = {'a': 10, 'b': 22, 'c': 43} for letter, value in frequencies: print(letter, value)
Why bother with classes? • Client code: code that uses a class that you have defined • The goal is to write client code that looks like this: rat 1 = Rational(3, 4) rat 2 = Rational(10, 53) total = rat 1 + rat 2 s = 'How are you today? ' scanner = Token. Scanner(s) while scanner. has. More. Tokens(): print(scanner. next. Token()) nums = [1, 2, 3, 4, 5] nums. append(6) last = nums. pop() frequencies = {'a': 10, 'b': 22, 'c': 43} for letter, value in frequencies: print(letter, value) • Notice how the details of how rational numbers, token scanners, lists, etc. are abstracted away from the client • As a result, you get nice, friendly syntax and don’t have to worry about how lists or dictionaries are implemented behind the scenes
Plan for Today Classes in Python Tuples in Python Adventure Overview
Tuples • A collection of values stored in an immutable list • If you try to change a tuple after creation, you get a Type. Error • Tuples can have mixed types • Convenient when you need to store a few values together, e. g. x and y coordinates • Live demo using Python interpreter! my_tuple = (1, 2, 3, 4, 5) coordinates = (40, 50) mixed_type_tuple = ('hello', 'world’, 47) my_tuple[0] = 106 # Type. Error: cannot modify tuples
Plan for Today Classes in Python Tuples in Python Adventure
Adventure Overview • Text-based adventure game • You’ll code up a framework for playing games based on text file input • Idea is that you move between rooms and can pick up items • Some locked passages require you to have a certain item to enter • Strongly recommend reading the whole assignment handout (even though it’s long) before writing a single line of code
Data-Driven Programs • Before, you built a program to do one thing, e. g. play Breakout/Hangman or reassemble text strings • Now, your program will be data-driven: source of data is external to program, i. e. not part of the code • How do you do that?
Data-Driven Programs • Before, you built a program to do one thing, e. g. play Breakout/Hangman or reassemble text strings • Now, your program will be data-driven: source of data is external to program, i. e. not part of the code • How do you do that? 1. Build a framework that consumes data from external sources (text files) 2. Convert external data into an internal representation using data structures 3. Use internal representation to run the program
Data-Driven Programs • Before, you built a program to do one thing, e. g. play Breakout/Hangman or reassemble text strings • Now, your program will be data-driven: source of data is external to program, i. e. not part of the code • How do you do that? 1. Build a framework that consumes data from external sources (text files) 2. Convert external data into an internal representation using data structures 3. Use internal representation to run the program • Why is this useful?
Data-Driven Programs • Before, you built a program to do one thing, e. g. play Breakout/Hangman or reassemble text strings • Now, your program will be data-driven: source of data is external to program, i. e. not part of the code • How do you do that? 1. Build a framework that consumes data from external sources (text files) 2. Convert external data into an internal representation using data structures 3. Use internal representation to run the program • Why is this useful? • Instead of just making a program that can Hangman, you build a framework that can play any number of games • You’re not designing just one Adventure; you’re designing a program that can play any adventure
Teaching Machine: Data-Driven • One approach: each function asks a question and then calls another function
Teaching Machine: Data-Driven • One approach: each function asks a question and then calls another function • Q: What’s bad about this approach?
Teaching Machine: Data-Driven • One approach: each function asks a question and then calls another function • Q: What’s bad about this approach? • A: Not data driven, program specific to one context
Teaching Machine: Data-Driven • One approach: each function asks a question and then calls another function • Q: What’s bad about this approach? • A: Not data driven, program specific to one context • Instead, we built a general tool that takes all data pertaining to the programmed instruction course from a data file • This approach lets us generalize our program to multiple contexts; user just has to change the input files • In other words, our approach is data-driven
Teaching Machine: Purpose 1. Ask student the current question
Teaching Machine: Purpose 1. Ask student the current question 2. Request answer from student
Teaching Machine: Purpose 1. Ask student the current question 2. Request answer from student 3. Look up answer in list of possible answers
Teaching Machine: Purpose 1. Ask student the current question 2. Request answer from student 3. Look up answer in list of possible answers • If user’s answer is in the list, consult the data structure to choose what question should be the new current question • If the student’s answer does not match any of the possibilities provided by the course data file, inform the student of that fact and give them another chance at the same question • Custom types—TMCourse and TMQuestion—to store data associated with courses and questions respectively
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted Room name
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted Room name Room short description
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted Room name Room short description Room long description (can span multiple lines)
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted Room name Room short description Room long description (can span multiple lines) Separator denotes end of long description
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted Room name Room short description Room long description (can span multiple lines) Separator denotes end of long description Directions and identifiers
Adventure: Rooms File Outside. Building Outside building You are standing at the end of a road before a small brick building. A small stream flows out of the building and down a gully to the south. A road runs up a small hill to the west. ----WEST: End. Of. Road UP: End. Of. Road NORTH: Inside. Building IN: Inside. Building SOUTH: Valley DOWN: Valley Room name Room short description Room long description (can span multiple lines) Separator denotes end of long description Directions and identifiers Blank line denotes end of room End. Of. Road End of road You are at the end of a road at the top of a small hill. You can see a small building in the valley to the east. ----EAST: Outside. Building DOWN: Outside. Building Rest of file omitted
Adventure: Objects File KEYS a set of keys Inside. Building LAMP a brightly shining brass lamp Beneath. Grate ROD a black rod with a rusty star Debris. Room WATER a bottle of water PLAYER Name of object
Adventure: Objects File KEYS a set of keys Inside. Building LAMP a brightly shining brass lamp Beneath. Grate ROD a black rod with a rusty star Debris. Room WATER a bottle of water PLAYER Name of object Description of object
Adventure: Objects File KEYS a set of keys Inside. Building LAMP a brightly shining brass lamp Beneath. Grate ROD a black rod with a rusty star Debris. Room WATER a bottle of water PLAYER Name of object Description of object Initial location of object (i. e. room)
Adventure: Objects File KEYS a set of keys Inside. Building LAMP a brightly shining brass lamp Beneath. Grate ROD a black rod with a rusty star Debris. Room WATER a bottle of water PLAYER Name of object Description of object Initial location of object (i. e. room) Blank line denotes separator between objects
Adventure: Objects File KEYS a set of keys Inside. Building Name of object Description of object Initial location of object (i. e. room) Blank line denotes separator between objects LAMP a brightly shining brass lamp Beneath. Grate ROD a black rod with a rusty star Debris. Room WATER a bottle of water PLAYER Special case – object starts in player’s inventory
Adventure: Synonyms File N=NORTH S=SOUTH E=EAST W=WEST U=UP D=DOWN Q=QUIT L=LOOK I=INVENTORY CATCH=TAKE RELEASE=DROP BOTTLE=WATER Each line of the form shortname=longname One synonym per line
Adventure: Starter Files • Adventure. py • Runnable Python file – entry point of your program • Similar to Teaching. Machine. py • Shouldn’t need to modify anything except ADVENTURE_PREFIX constant
Adventure: Starter Files • Adventure. py • Runnable Python file – entry point of your program • Similar to Teaching. Machine. py • Shouldn’t need to modify anything except ADVENTURE_PREFIX constant • Adv. Game. py • Implements the game • Analogous to TMCourse. py • Build out the run method throughout various milestones, adding helpers when needed
Adventure: Starter Files • Adventure. py • Runnable Python file – entry point of your program • Similar to Teaching. Machine. py • Shouldn’t need to modify anything except ADVENTURE_PREFIX constant • Adv. Game. py • Implements the game • Analogous to TMCourse. py • Build out the run method throughout various milestones, adding helpers when needed • Adv. Room. py • Represents a single room in the game • Analogous to TMQuestion. py • Contains headers needed for Milestone 1; add more public headers in future milestones
Adventure: Starter Files • Adventure. py • Runnable Python file – entry point of your program • Similar to Teaching. Machine. py • Shouldn’t need to modify anything except ADVENTURE_PREFIX constant • Adv. Game. py • Implements the game • Analogous to TMCourse. py • Build out the run method throughout various milestones, adding helpers when needed • Adv. Room. py • Represents a single room in the game • Analogous to TMQuestion. py • Contains headers needed for Milestone 1; add more public headers in future milestones • Adv. Object. py • Represents an object in the game • Implement provided public headers in Milestone 4
Milestone 1: Leverage Code from Teaching Machine • Modify Teaching Machine code to work with Adventure • Teaching Machine code is provided with assignment starter code
Milestone 1: Leverage Code from Teaching Machine • Modify Teaching Machine code to work with Adventure • Teaching Machine code is provided with assignment starter code • Leverage TMCourse. py to write Adv. Game. py • Leverage TMQuestion. py to write Adv. Room. py • You’ll need to change around variable names and function names
Milestone 1: Leverage Code from Teaching Machine • Modify Teaching Machine code to work with Adventure • Teaching Machine code is provided with assignment starter code • Leverage TMCourse. py to write Adv. Game. py • Leverage TMQuestion. py to write Adv. Room. py • You’ll need to change around variable names and function names • Before moving on, run the code: you should be able to head up to the top of the hill, inside the building, and down to the grate • Pro tip: copy from Teaching Machine file to Adventure file and then make modifications instead of modifying the Teaching Machine files directly
Milestone 2: Short Descriptions of Rooms • Don’t make the user read the long room description each time they enter the room
Milestone 2: Short Descriptions of Rooms • Don’t make the user read the long room description each time they enter the room • Once the user has visited a room, print the short description instead of the long description
Milestone 2: Short Descriptions of Rooms • Don’t make the user read the long room description each time they enter the room • Once the user has visited a room, print the short description instead of the long description • Observe that the short description of the room is printed the second time
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) • has. Been. Visited()
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) – takes a Boolean (T/F) and updates the room depending on the Boolean value • has. Been. Visited()
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) – takes a Boolean (T/F) and updates the room depending on the Boolean value • has. Been. Visited() – returns True or False depending on if the room has been visited
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) – takes a Boolean (T/F) and updates the room depending on the Boolean value • has. Been. Visited() – returns True or False depending on if the room has been visited • But how will Adv. Room remember whether a room has been visited?
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) – takes a Boolean (T/F) and updates the room depending on the Boolean value • has. Been. Visited() – returns True or False depending on if the room has been visited • But how will Adv. Room remember whether a room has been visited? • Maintain a self. _visited variable, which is set by set. Visited and returned by has. Been. Visited
Milestone 2: Short Descriptions of Rooms • Add two new methods to the Adv. Room class: • set. Visited(visited) – takes a Boolean (T/F) and updates the room depending on the Boolean value • has. Been. Visited() – returns True or False depending on if the room has been visited • But how will Adv. Room remember whether a room has been visited? • Maintain a self. _visited variable, which is set by set. Visited and returned by has. Been. Visited • Underscore in self. _visited denotes that this field should only be changed by the Adv. Room class, not by external clients of the class
Milestone 3: QUIT, HELP, and LOOK Commands • QUIT: end the program by calling Python’s quit() method • HELP: print instructions for the game – use provided HELP_TEXT constant in Adv. Game. py • To print the contents of a list, one element per line, you can do 'n'. join(your_list) • LOOK: print complete description of room and its contents, even if the user has already visited it
Milestone 3: QUIT, HELP, and LOOK Commands • QUIT: end the program by calling Python’s quit() method • HELP: print instructions for the game – use provided HELP_TEXT constant in Adv. Game. py • To print the contents of a list, one element per line, you can do 'n'. join(your_list) • LOOK: print complete description of room and its contents, even if the user has already visited it • First, use provided Token. Scanner or str. split method to user input into individual words • Then, see if their first word is one of the action verbs • If so, take appropriate action
Milestone 3: QUIT, HELP, and LOOK Commands • QUIT: end the program by calling Python’s quit() method • HELP: print instructions for the game – use provided HELP_TEXT constant in Adv. Game. py • To print the contents of a list, one element per line, you can do 'n'. join(your_list) • LOOK: print complete description of room and its contents, even if the user has already visited it • First, use provided Token. Scanner or str. split method to user input into individual words • Then, see if their first word is one of the action verbs • If so, take appropriate action • Before moving on, test thoroughly – use the sample run on the spec AND try making other test scenarios
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided):
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here?
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here?
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here? Return the appropriate property from self
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here? Return the appropriate property from self • read. Object – static method that parses lines from a file and creates a new Adv. Object • How should you create a new Adv. Object?
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here? Return the appropriate property from self • read. Object – static method that parses lines from a file and creates a new Adv. Object • How should you create a new Adv. Object? Use the constructor!
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here? Return the appropriate property from self • read. Object – static method that parses lines from a file and creates a new Adv. Object • How should you create a new Adv. Object? Use the constructor! • Use Adv. Room as a guide, which solves a more complicated version of a similar problem
Milestone 4: Objects, Part 1: Read in the objects from a file • Methods to implement (provided): • Constructor – takes name, description, and location as parameters • What should you do here? Remember the arguments passed to constructor as fields in self • get. Name, get. Description, get. Initial. Location • What should you do here? Return the appropriate property from self • read. Object – static method that parses lines from a file and creates a new Adv. Object • How should you create a new Adv. Object? Use the constructor! • Use Adv. Room as a guide, which solves a more complicated version of a similar problem • Modify Adv. Game to read in the objects from file and store them in a dictionary – same as what you do for rooms • Missing Object file just means there are no objects; not an error
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this?
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods:
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name)
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name)
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list • contains. Object(object. Name)
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list • contains. Object(object. Name) – returns whether a room contains the given object name
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list • contains. Object(object. Name) – returns whether a room contains the given object name • get. Contents(object. Name)
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list • contains. Object(object. Name) – returns whether a room contains the given object name • get. Contents(object. Name) – returns a copy of the list of objects
Milestone 4: Objects, Part 2: Assign objects to rooms • Rooms need to store the objects they contain • How to do this? Make Adv. Room store a list of Adv. Objects • Instead of exposing that list publicly, allow clients to access underlying data with the following public methods: • add. Object(object. Name) – adds the provided object name to the list • remove. Object(object. Name) – removes the provided object name from the list • contains. Object(object. Name) – returns whether a room contains the given object name • get. Contents(object. Name) – returns a copy of the list of objects • Note: if you return the actual list (not a copy), you’re returning a reference to the list stored by Adv. Room • Analogy: you ask me for a book, and I give you my original book. Then you could burn it, and I would be sad. By returning a copy, you could destructively modify it, but I don’t care.
Milestone 4: Objects, Part 3: Modifying Adv. Game • Modify Adv. Game to place objects in the appropriate rooms • How to do this?
Milestone 4: Objects, Part 3: Modifying Adv. Game • Modify Adv. Game to place objects in the appropriate rooms • How to do this? • Iterate over the dictionary containing all objects, call add. Object on behalf of each object, and place it in the room specified by its initial location • Notice how we’re leveraging other methods written in previous parts
Milestone 4: Objects, Part 4: Modify code that prints room description • Modify the code that prints a room description to also print the objects in that room, one per line
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room • INVENTORY: print description of each object in the user’s inventory
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room • INVENTORY: print description of each object in the user’s inventory • How should you implement this?
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room • INVENTORY: print description of each object in the user’s inventory • Maintain notion of inventory by adding a field to Adv. Game, namely a list of objects in the user’s inventory
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room • INVENTORY: print description of each object in the user’s inventory • Maintain notion of inventory by adding a field to Adv. Game, namely a list of objects in the user’s inventory • Implement PLAYER special case in the code for Milestone 4 • If an object’s initial location is PLAYER, this means the object starts off in the player’s inventory, not in a room
Milestone 5: TAKE, DROP, and INVENTORY • TAKE object: if object is in the room, remove it from the room and add it to player’s inventory • DROP object: opposite of TAKE – remove object from inventory if present and add it to the room • INVENTORY: print description of each object in the user’s inventory • Maintain notion of inventory by adding a field to Adv. Game, namely a list of objects in the user’s inventory • Implement PLAYER special case in the code for Milestone 4 • If an object’s initial location is PLAYER, this means the object starts off in the player’s inventory, not in a room • Change your code from Milestone 3 to recognize the new commands • Note that TAKE and DROP require you to read the object name from the
Milestone 6: Synonyms • It’s annoying to type NORTH and SOUTH; would rather type N and S, etc. • Read through Synonyms data file • If no file is present, that just means there are no synonyms; not an error
Milestone 6: Synonyms • It’s annoying to type NORTH and SOUTH; would rather type N and S, etc. • Read through Synonyms data file • If no file is present, that just means there are no synonyms; not an error • Make a dictionary of synonym definitions in Adv. Game. py
Milestone 6: Synonyms • It’s annoying to type NORTH and SOUTH; would rather type N and S, etc. • Read through Synonyms data file • If no file is present, that just means there are no synonyms; not an error • Make a dictionary of synonym definitions in Adv. Game. py • When you read a token, if that token is a synonym, substitute that token with its standard definition
Milestone 7: Locked Passages • To store information about passages, you likely made a dictionary mapping direction names to room names – that’s what Teaching Machine does
Milestone 7: Locked Passages • To store information about passages, you likely made a dictionary mapping direction names to room names – that’s what Teaching Machine does • However, that doesn’t work here because each direction could have multiple room names • e. g. DOWN maps to both Beneath. Grate and Missing. Keys
Milestone 7: Locked Passages • To store information about passages, you likely made a dictionary mapping direction names to room names – that’s what Teaching Machine does • However, that doesn’t work here because each direction could have multiple room names • e. g. DOWN maps to both Beneath. Grate and Missing. Keys • Also, you might need a particular object in order to go through a certain passage – called a locked passage which requires a key for access
Milestone 7: Locked Passages • To store information about passages, you likely made a dictionary mapping direction names to room names – that’s what Teaching Machine does • However, that doesn’t work here because each direction could have multiple room names • e. g. DOWN maps to both Beneath. Grate and Missing. Keys • Also, you might need a particular object in order to go through a certain passage – called a locked passage which requires a key for access • e. g. you can only go to Beneath. Grate if you have KEYS in player’s inventory • A slash will appear after the room name, followed by the object
Milestone 7: Locked Passages • However, that doesn’t work here because each direction could have multiple room names • e. g. DOWN maps to both Beneath. Grate and Missing. Keys • Also, you might need a particular object in order to go through a certain passage – called a locked passage which requires a key for access • e. g. you can only go to Beneath. Grate if you have KEYS in player’s inventory • A slash will appear after the room name, followed by the object • If the user types DOWN and has KEYS in inventory, go to
Milestone 7: Locked Passages • You’ll need to change the passages data structure from a dictionary to a list
Milestone 7: Locked Passages • You’ll need to change the passages data structure from a dictionary to a list • Each element in the list is a tuple of size 3 storing: • Direction name • Name of destination room • Key required to traverse passage, or None if a key isn’t needed
Milestone 7: Locked Passages • You’ll need to change the passages data structure from a dictionary to a list • Each element in the list is a tuple of size 3 storing: • Direction name • Name of destination room • Key required to traverse passage, or None if a key isn’t needed • Code that moves from one room to another based on the player’s input must search through the array to find the first option that applies
Milestone 7: Locked Passages • You’ll need to change the passages data structure from a dictionary to a list • Each element in the list is a tuple of size 3 storing: • Direction name • Name of destination room • Key required to traverse passage, or None if a key isn’t needed • Code that moves from one room to another based on the player’s input must search through the array to find the first option that applies • You’ll need to change Adv. Room and Adv. Game • Change read. Room to store passage data in a list instead of a dictionary • Change get. Next. Room to take the user’s inventory into account • But Adv. Room doesn’t know the user’s inventory, since that’s stored in Adv. Game
Milestone 7: Locked Passages • Problem: Adv. Room doesn’t know the user’s inventory, since that’s stored in Adv. Game • What should we do?
Milestone 7: Locked Passages • Problem: Adv. Room doesn’t know the user’s inventory, since that’s stored in Adv. Game • Solution: Move the logic for figuring out which room to move to based on direction to Adv. Game
Milestone 7: Locked Passages • Problem: Adv. Room doesn’t know the user’s inventory, since that’s stored in Adv. Game • Solution: Move the logic for figuring out which room to move to based on direction to Adv. Game • To do so, you’ll need to add a get. Passages method to Adv. Room so the list of passages in the Adv. Room is exposed to its clients
Milestone 7: Locked Passages • Problem: Adv. Room doesn’t know the user’s inventory, since that’s stored in Adv. Game • Solution: Move the logic for figuring out which room to move to based on direction to Adv. Game • To do so, you’ll need to add a get. Passages method to Adv. Room so the list of passages in the Adv. Room is exposed to its clients • Before moving on, test thoroughly • You should be able to move about the world, except for going through a locked passage without the necessary key
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • How should we do this?
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • How should we do this? • Option 1: design a whole new data structure to represent such messages • Option 2: make a small change to the existing program
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • How should we do this? • Option 1: design a whole new data structure to represent such messages • Option 2: make a small change to the existing program
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • Solution: create new rooms whose descriptions contain the messages you want to deliver
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • Solution: create new rooms whose descriptions contain the messages you want to deliver • When player enters such a room, print the description as normal • However, you want to force the user to move to a different room
Milestone 8: Forced Motion • If a user tries to go through a locked passage without the required key, your program needs to indicate that this motion is prohibited • Solution: create new rooms whose descriptions contain the messages you want to deliver • When player enters such a room, print the description as normal • However, you want to force the user to move to a different room • This idea is known as forced motion and uses the FORCED command
Milestone 8: Forced Motion If a passage’s direction is FORCED: 1. Print out room description (always long description) 2. Immediately move player to specified destination without waiting for player to enter a command • This lets you display a message to the user and then move on
Milestone 8: Forced Motion If a passage’s direction is FORCED: 1. Print out room description (always long description) 2. Immediately move player to specified destination without waiting for player to enter a command • This lets you display a message to the user and then move on • In this example, when the user enters Missing. Keys, you immediately print the message (i. e. long description) and then immediately set the current room to Outside. Grate
Parting Thoughts • Start early! • Make a schedule for yourself • Read the entire handout before writing a single line of code • Before diving into writing code for each milestone, be able to describe what you’re trying to do in English • TEST each milestone thoroughly before moving on to the next • If you get stuck on milestone n, don’t move to milestone n + 1 thinking “oh, I’ll fix it later” – this will come back to haunt you • You can do it!
- Slides: 114