Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP 202

Fall 2022

Assignment 2

Part 1 (0 points): Warm-up

Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have diiculties with the questions of Part 1, then we suggest that you consult the TAs during their oice hours; they can help you and work with you through the warm-up questions. You are responsible for knowing all of the material in these questions.

Warm-up Question 1 (0 points)

Write a function swap which takes as input two int values x and y. Your function should do 3 things:

1. Print the value of x and y

2. Swap the values of the variables x and y, so that whatever was in x is now in y and whatever was in y is now in x

3. Print the value of x and y again.

Here is an example of what your function should print when called (in the shell):

>>> swap(3, 4)

inside swap: x is:3 y is:4

inside swap: x is:4 y is:3

Warm-up Question 2 (0 points)

Consider the program you have just written.  Create two global integer variables in the main body of your program.  Call them x and y.  Assign values to them and call the swap function you wrote in the previous part using x and y as input parameters.

After calling the swap() function —inside the main body— print the values of x and y. Are they different than before? Why or why not?

Warm-up Question 3 (0 points)

Create a function called counting that takes as input a positive integer and counts up to that number.

For example:

>>> counting(10)

Counting up to 10: 1 2 3 4 5 6 7 8 9 10

Warm-up Question 4 (0 points)

Modify the last function by adding an additional input that represents the step size by which the function should be counting. For example:

>>> counting(25, 3)

Counting up to 25 with a step size of 3: 1 4 7 10 13 16 19 22 25

Warm-up Question 5 (0 points)

Write a function replace_all which takes as input a string and two characters.   If the second and third input string do not contain exactly one character the function should raise a ValueError.  Oth- erwise, the function returns the string composed by the same characters of the given string where all occurrences of the first given character are replaced by the second given character.   For exam- ple, replace_all("squirrel", "r" , "s") returns the string "squissel", while replace_all("squirrel", "t", "a") returns the string "squirrel". Do not use the method replace to do this.

Warm-up Question 6 (0 points)

Write a module with the following global variables:

lower_alpha = "abcdefghijklmnopqrstuvwxyz"

upper_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

In this module write a function make_lower which takes a string as input and returns a string containing the same characters as the input string, but all in lower case. For example, make_lower("AppLE") returns the string "apple". Do not use the method lower to do this. Hint: note that characters from the English alphabet appear in the same position in the two global variables.

Part 2

The questions in this part of the assignment will be graded.

The main learning objectives for this assignment are:

• Correctly define and use simple functions.

• Solidify your understanding of the difference between return and print.

• Generate and use random numbers inside a program.

• Understand how to test functions that contain randomness.

• Correctly use loops and understand how to choose between a while and a for loop.

• Solidify your understanding of how to work with strings: how to check for membership, how to access characters, how to build a string with accumulator patterns.

• Learn how to use functions you have created in a different module.

Note that this assignment is designed for you to be practicing what you have learned in our lectures up to and including Lecture 14 (Debugging). For this reason, you are NOT allowed to use anything seen after that lecture or not seen in class at all. You will be heavily penalized if you do so.

For full marks, in addition to the points listed on page 1, make sure to add the appropriate documentation string (docstring) to all the functions you write. The docstring must contain the following:

• The type contract of the function.

• A description of what the function is expected to do.

• At least three (3) examples of calls to the function. You are allowed to use at most one example per function from this PDF.

Examples

For each question, we provide several examples of how your code should behave. All examples are given as if you were to call the functions from the shell.

When you upload your code to codePost, some of these examples will be run automatically to test that your code outputs the same as given in the example. However, it is your responsibility to make sure your code/functions work for any inputs, not just the ones shown in the examples. When the time comes to grade your assignment, we will run additional, private tests that will use inputs not seen in the examples. You should make sure that your functions work for all the different possible scenarios. Also, when testing your code, know that mindlessly plugging in various different inputs is not enough—it’s not the quantity of tests that matters, it’s having tests that cover all of the possible scenarios, and that requires thinking about possible scenarios.

Furthermore, please note that your code files should not contain any function calls in the main body of the program (i.e., outside of any functions).  Code that contains function calls in the main body will automatically fail the tests on codePost and thus be heavily penalized. It is OK to place function calls in the main body of your code for testing purposes, but if you do so, make certain that you remove them before submitting. You can also test your functions by calling them from the shell. Please review what you have learned in our lecture on Modules if you’d like to add code to your modules which executes only when you run your files.

Safe Assumptions

For all questions on this assignment, you can safely assume that the type of the inputs (both to the functions and those provided to the program by the user) will always be correct. For example, if a function takes as input a string, you can assume that a string will always be provided to it during testing. The same goes for user input. At times you will be required to do some input validation, but this requirement will always be clearly stated.  Otherwise, your functions should work with any possible input that respect the function’s description. For example, if the description says that the function takes as input a positive integer, then it should work with all integers greater than 0. If it mentions an integer, then it should work for any integer. Make sure to test your functions for edge cases!

Code Repetition

One of the main principles of software development is DRY: Don’t Repeat Yourself. One of the main ways we can avoid repeating ourselves in code is by writing functions, then calling the functions when necessary, instead of repeating the code contained within them.  Please pay careful attention in the questions of this assignment to not repeat yourself, and instead call previously-defined functions whenever appropriate.  As always, you can also add your own helper functions if need be, with the intention of reducing code repetition as much as possible.

Question 1: Dungeons! (15 points)

Remember how it was like living in the 80s?  I don’t but I heard a lot about it.  This was a magical time when there was no internet as we know it and most computers could not even display graphics on screen. They could only display text. Doesn’t it sound great?

I know your first question –‘but how can I play games if there is no graphics?’ Well, it’s great that you asked that, and it’s also a big coincidence, because we are going to learn all about that in this question.

Games back in the day were completely text-based. One popular style of game was known as‘interactive fiction.’These games would tell a story in which you were the main character and progressed through a narrative by typing in commands. It was sort of like a Choose Your Own Adventure book, but instead of only having the choice to flip to different pages, the player could type in all kinds of commands and different things would happen, as long as the programmer had written code to handle them.

One of the first interactive fiction games ever made was called Colossal Cave Adventure, written by Will  Crowther for his children in 1976. In the game, you explore a cave system. The cave is described to you  through text, and you play the game by entering commands in natural language. You could type‘west’ to move in a westward direction, perhaps to a different part of the cave; you could type‘get lamp’ to  pick up a lamp nearby (and could then subsequently light it, letting you see more of the cave); you could  type throw eggs at troll’ to vanquish an enemy blocking your path.  The game had 66 rooms in total  that you could navigate through, and 193 different verbs (‘get’, ‘throw’, etc.) you could use in different  situations.

Crowther worked on the game as a hobby.  His main job was in developing the ARPANET, one of the first computer networks and a precursor to the Internet. One day he went on vacation, leaving a copy of the game on the computer mainframe he had been working on. While he was away, people from across the network, from other companies and universities, began to find the game and play it. It was a massive hit.  Don Woods, a graduate student at Stanford, would build upon the code and extend the game. It could be found everywhere in the late 1970s, and is considered to this day one of the most influential video games ever made.

Wow!  That was a big tangent.  Anyway, for this question, you will write your own small interactive fiction game.  To reduce the scope, we are going to write an ‘escape room’ interactive fiction game. Surely you know of escape rooms. Those places you go to with your friends and try to solve puzzles in a closed room in order to escape. Well - now we can play an escape room without leaving the comfort of our own home!

You will design your own escape room by writing a function escape_room, a void function that takes no inputs, in a file dungeon .py. You will come up with your own scenario, and implement a number of valid commands that the player could type in. Your function must satisfy the following requirements:

• When the function runs, a textual description of the escape room should be displayed to the screen. The description should list various objects in the room.

• The user should then be prompted to enter a command that interacts with an object in some way (e.g., ‘examine table’ or ‘push button’).

• If the user enters a valid command, some message relating to the object should display that contains some hint about how to escape the room; then the user should be prompted for another command.

• If the user enters an invalid command, a message should display alerting them that they have entered an invalid command; then the user should be prompted for another command.

• If the user enters a special command, deduced from the hints given to them by interacting with objects, then a message should print stating that they have escaped the room, and the function should return (i.e., the game should end).

• If the user enters list commands’, then all valid commands should be displayed.

• Commands should be able to be expressed in different ways.  For instance, ‘examine book’,‘look at book’, ‘read book’should all be valid for interacting with a book. Furthermore, the commands should be able to be inferred by the room description (e.g., if an object is mentioned in the room, you should be able to examine it, possibly pick it up, and so on, depending on the object).

• Commands typed in the user by should be case-insensitive. That is, ‘Examine book’, ’EXAMINE BOOK’ and ’eXaMiNe BoOk’ should all do the same thing.

• There must be at least three objects in the room that the player can interact with in some way (by examining, etc.).

• Note that although this function is open-ended, we will as usual be paying close attention to code style. Make sure to follow our style guidelines as discussed in class, including providing a docstring

in the usual format and avoiding repetition as much as possible.

Finally, at the top of your code file, define three global variables:  ROOM_NAME, a string for the name of your escape room; AUTHOR, a string for your name or nickname; and PUBLIC, a boolean indicating whether you would be comfortable for the room to be displayed publicly in some fashion along with your AUTHOR

name.

Any submission meeting these requirements will obtain full marks, but you are encouraged to go beyond them. Our TAs will be showcasing their favourite submissions in the Slack.

Here is an example of an escape room.  Note that this example is too simple for our requirements – it has only two objects. Also, do not simply copy the example; you should come up with your own idea!

>>> escape_room()

You have walked deep into a cave, looking for treasure . You wander down a passage that turns out to be a dead-end, and all of a sudden you hear the rumble of rocks from behind you . You are trapped!

In front of you, the rock face is smooth and glistens with moisture . Moss of many different hues grows on the surface . There is an eerie silence, punctuated only by the drops of water from the ceiling into a small pond next to you . What do you do?

> list commands

examine moss

examine pond

> examine moss

You look at the rock wall closely . Clumps of moss grow in strange formations . Most are green,

but there is a small purple area . It's a very strange shape, almost like a keyhole .

> examine pond

You kneel down and peer into the pond . In the reflection of the water, you see yourself with

a key in your hand . Your reflection then smiles and puts the key in their

pocket . Startled, you knew where to

you stand up and feel inside your pocket -- there is a key there! If only use it!

> put key in keyhole

A secret door opens in the rock wall . . .you climb through and escape!

Question 2: Treasure! (85 points)

In this question, we will go on a hunt for treasure!!!  We will follow the trail in a treasure map, which we will represent as a big string. Here is an example treasure map string:

' . .vv .<> . . '