Python代写 | CSE 231 Spring 2020 Computer Project #11

这个作业是用Python实现简单的上学场景
CSE 231 Spring 2020
Computer Project #11
This assignment is an exercise to complete Python classes used in a game. It is worth 60 points
(6% of the course grade) and must be completed and turned in before 11:59 on Friday April
24, 2020.
Assignment Overview
This assignment will give you more experience with writing classes
You will write four classes that implement a hectic morning of getting to class to do the CSE231
Final exam.
Background
Taking an exam can be extremely frustrating, especially if you were up all night studying and
doing practice questions. The interface with the student using only text, to describe locations and
useful items and allowing the user to type a set of commands to interact with one’s thoughts. As
a frantic student you can run back and forth from class to class countless times.
Project Description / Specification
You will write a program to implement a hectic morning of getting to class for credit.
Your program must consist of three classes: Student, Classroom, Rush, where you
must also have a main() function designed to run the morning escapade.
Student is a fairly simple class. A student knows the id of the room they are currently in,
and has a list of its inventory. It contains these methods:
__init__(self, item_list, classroom_id): Provided. This method initializes the
student with a classroom id and an item list. By default the student’s backpack is empty, and the
classroom id is -1.
__repr__(self): Provided.
__str__(self): Provided.
place(self, classroom_id): Places a student in a classroom, but is implemented by
adding a classroom ID to the student instance (hint: simply assign to the classroom_id attribute,
but remember “self”)
__add_item(self, item): Adds the item to the student’s backpack. If the backpack
already has 6 items in it, print “Backpack is full.” (item basically disappears since it
was picked up but not put in a backpack). Otherwise, add the item to the backpack (hint: the
backpack is a list, so how do you add an item to a list?)
__remove_item(self, item): Removes the item from the student’s backpack. If the
item is in the backpack, remove it (hint: how do you remove an item from a list?), else
print “Failed to remove item from backpack.”
__eq__(self, S): Returns True if Student ‘s classroom id and backpack are equal to
the classroom id and backpack of S. Otherwise, returns False.
Classroom is the class that represents a single classroom at a time. Associated with each
classroom is a unique id, an int, and a course, a string such as “CSE231” (no spaces). It may also
have one or more directions to other rooms in the hallway – which gives many options to run to
other classrooms – and there are one or more items in the classrooms. It contains these methods:
__init__(self, text_desc =”0 empty”): We provide the beginning of this method.
This method initializes the classroom from a text string which describes it. The string contains,
in order, each separated by a space, the integer id (as string digits) of the room, the course that
describes the classroom (no spaces inside the course string), and an optional mix of spaceseparated strings which describe directions and items in the room. Exit strings are always of the
form , where the capital letter gives the direction (UDLR) of the next
classroom and the number gives the id of that room. For example, the string ‘U13’ indicates that
there is a classroom in the “U” (up) direction and that the classroom has id 13. (Any string that is
not a direction is an item; directions always start with U, D, R, or L, and items are lowercase.)
The exits and items can appear in any order. Loop through the text_desc string assigning
exits to the exit dictionary and items to the classroom “backpack” list. See the provided .txt files
and be sure to check the Notes and Hints section below for some advice on how to store all this
information. The default value of text_desc is “0 empty”.
Store items into a “backpack” that is a list of items. Assume that any string that does not start
with a capital letter in “UDLR” will be a string of lowercase letters (i.e. no error checking
necessary)
Store directions into a dictionary with keys “U”, “D”, “L”, and “R” with values that are integers.
There is at most one integer for any direction. Assume that there are no errors to be checked, i.e.
if string starts with a capital letter from “UDLR”, it is followed immediately by one or more
digits.
__repr__(self): Provided.
__str__(self): Provided.
add_item(self, item): Adds an item to the classroom’s inventory, its “backpack” (one
line of code is sufficient).
remove_item(self, item): Removes an item from the room’s inventory, “backpack”, if
it is there, else print “Failure to find the item in the classroom.” (one
simple if statement is sufficient)
get_room(self, direction): Returns the room id in the given direction, or False if
there is no such room. The direction must be a valid key into self.exits, i.e. U, D, R, L;
returns False, if not valid.
__eq__(self, C): Returns True if Classroom id, course, exits and backpack are equal
to the id, course, exits and backpack of C. Otherwise, returns False.
Rush is the class that governs the escapade itself. It is responsible for interactions between
the user, the character, and the rooms. It has these methods:
__init__(self, filename): We provide the beginning of this method. This method
creates a default Student and reads a file to create a dictionary of Classrooms: each line of
the file will be one Classroom instance. The key is the classroom id (the first item in a line).
The value is a Classroom instance using the entire line as its argument. The student starts the
escapade with nothing in their backpack and starts in a classroom with the lowest id number (that
will always be the classroom in the first line of the file).
__repr__(self): Provided.
__str__(self): Provided.
intro(self): Provided. This method prints an introduction to the search for items because
you are late.
print_help(self): Provided. This method prints a list of the valid commands.
prompt(self): Provided. This method prompts the user for a command, processes the
command (usually using one of the other Rush methods below), and either carries it out (if it’s
valid) or prints an error message. No re-prompting for invalid commands is necessary. Here are
the valid commands:
search(self): Provided. This method prints the description of the current classroom (where
the student is in), using the method you wrote for Classroom.
backpack(self): This method prints the student’s inventory in their backpack, using the
method you wrote for Student.
pickup(self, item): This method coordinates the student with their current classroom to
remove the item from the classroom and add it to the student’s backpack. If the item is not
present in the classroom (removal returns False), it must not be added to the student’s backpack,
and an exception must be raised. It is possible to pick up an item and find that the student’s
backpack is full, resulting in the item disappearing. Either way, a message must print.
drop(self, item): This method coordinates the student with their current classroom and
removes the item from the student’s backpack and places it in the classroom (classroom’s
“backpack”). If the item is not present in the backpack, it must not be added to the classroom (the
student’s remove_item method will print the error message).
move(self, direction): This method moves the student in the specified direction if the
current classroom has that direction in its attributes. If not, the move fails, and this method will
print an error message (the message is in the provided skeleton code).
win(self): This method checks that the student has entered the CSE231 classroom and has
in their backpack the cheatsheet, eraser, paper, and pencil. If so, it returns True, else it returns
False. We provide a list of the correct backpack in the skeleton code (hint: note that it is sorted).
main(): Provided. This simple function runs the escapade as it prompts the student for a file
name, and then uses that file to create a Rush. It then prints a short introductory message
(including a list of commands) and uses the Rush’s prompt() method until the student gives
up. You do not need to do error checking on the format of the input file, but if the user enters a
bad file name, you should recover and prompt the user for a good one (a file that can be opened
for reading).
There should be nothing in your code outside of your classes and functions except a single call to
main(). Specifically, do not use global variables (we do provide the MAP dictionary as a
global constant).
Each class, method, and function (including main()) must have a docstring. See the course
coding standards for more about docstrings.
Downloadables:
Sample .txt files: room_win.txt, rushing.txt
Output examples: input_win.txt, output_win.txt, input_rush.txt, output_rush.txt,
input_rush_error.txt, output_rush_error.txt (these are the test case files)
Notes and Hints:
1. For big projects like this, frequent testing is an excellent idea. For example, you can write
and test the Student class without any of the other classes. You can also write and test the
Room class alone. Even the Rush class can be broken up into pieces; write and test the
various command methods separately, then incorporate them into the prompt() method.
2. We have provided several different .txt documents describing various scenarios. You
should test your code with each, from most simple to the most complex. Do not hardcode
a particular file name – your TA may also test with multiple different escapades.
3. You may find it helpful to write a few extra methods for Classoom and Student dealing
with the pickup and drop commands. For each class, add_item(self, item) and
remove_item(self, item) methods can be written very simply. For example, add_item(self,
item) for the Student class can simply add the item, not checking to see if it actually
exists. The relevant Rush method (e.g., pickup()) handles the coordination, only calling
add_item() for the Student if it can also call remove_item() for the current Classroom.
Your backpack is endless; however, it begins to get heavy after 6 items so grabs the 6
items you need for a test from their respective classes i.e. pencil, calculator, paper, pen
eraser and cheatsheet.
4. Classroom ids are designed to allow the Rush class to keep track of classrooms – so use a
dictionary there with classroom ids as the keys and Classrooms as the values. Classroom
ids never overlap, so there’s no need to worry about that.
5. Your friends do not know what other rooms they are close, only the ids of those
classrooms. So it is the responsibility of the Rush class to do all id-to-Classroom
translations, and hence to update the Student’s current classroom id.
6. The Rush class doesn’t really need to do much parsing of the file it reads – just create a
new Classroom from each non-blank line in the file by passing that line in to
Classroom().
7. When creating a Classroom from a text string, the first word is the id and the second is
the course descriptor. After that, directions to the next class and items may come in any
order. Design a loop to go through these remaining strings, and remember directions start
with U, D, R, or L, and items are lowercase. When you exit into the hallway, you’ll need
to associate the correct direction with the id number of the linked room. A dictionary with
directions as keys and ids as values is a good idea here. Items are simple strings, and it’s
probably best to store them in a list.
8. For the purpose of Rush’s move() method, you may find it helpful to write a function in
Classroom which, given a direction, returns either the id of the attached room in that
direction or False, if there is no classrooms in that direction.
9. General advice: Start early. Read over the Project Description carefully and double-check
you have met all the requirements. Don’t forget your algorithms and docstrings.
Following is a sample interaction with our solution:
Test 1:
Enter a text filename: room_win.txt
AHHHH! I’m late for class
*runs out the house to catch the bus with an empty backpack*
You’re popular and have friends in many classes. Find and collect any
items you find useful for your exam.
You are already late, and have a CSE231 Final Exam in 10 mins.
Use your instincts:
*thinks*.. *thinks*.. what to do?!?!?!?!
*running*
S or search — prints a description of the classroom you ran into
B or backpack – prints a list of items in your backpack
P pencil or pickup pencil – *mental* instruction to pick up an item
called pencil
DR pencil or drop pencil – *mental* instruction to drop off an item
called pencil
U or up – *mental* instruction to up the hallway to find another
classroom
D or down – *mental* instruction to down the hallway to find another
classroom
R or right – *mental* instruction to right in the hallway to find
another classroom
L or left – *mental* instruction to left in the hallway to find
another classroom
G or giveup – I have no more time, I need to get to class!!!
H or help – prints this list of options again
Remember that uppercase and lowercase SHOULD NOT matter.
JUST GRAB WHAT YOU NEED AND GET TO CLASS TO START YOUR FINAL EXAM!!!
HURRYYYY!!!
You see a empty classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Up.
In room 1 with course empty
Backpack: Empty
Enter a command (H for help): U
You went Up and found a new classroom.
In room 2 with course MTH132
Backpack: Empty
Enter a command (H for help): P pencil
In room 2 with course MTH132
Backpack: pencil
Enter a command (H for help): P paper
In room 2 with course MTH132
Backpack: pencil, paper
Enter a command (H for help): R
You went Right and found a new classroom.
In room 4 with course MTH235
Backpack: pencil, paper
Enter a command (H for help): P cheatsheet
In room 4 with course MTH235
Backpack: pencil, paper, cheatsheet
Enter a command (H for help): U
You went Up and found a new classroom.
In room 3 with course CM161
Backpack: pencil, paper, cheatsheet
Enter a command (H for help): P eraser
In room 3 with course CM161
Backpack: pencil, paper, cheatsheet, eraser
Enter a command (H for help): U
You went Up and found a new classroom.
You succeeded!
Test 2:
Enter a text filename: rushing.txt
AHHHH! I’m late for class
*runs out the house to catch the bus with an empty backpack*
You’re popular and have friends in many classes. Find and collect any
items you find useful for your exam.
You are already late, and have a CSE231 Final Exam in 10 mins.
Use your instincts:
*thinks*.. *thinks*.. what to do?!?!?!?!
*running*
S or search — prints a description of the classroom you ran into
B or backpack – prints a list of items in your backpack
P pencil or pickup pencil – *mental* instruction to pick up an item
called pencil
DR pencil or drop pencil – *mental* instruction to drop off an item
called pencil
U or up – *mental* instruction to up the hallway to find another
classroom
D or down – *mental* instruction to down the hallway to find another
classroom
R or right – *mental* instruction to right in the hallway to find
another classroom
L or left – *mental* instruction to left in the hallway to find
another classroom
G or giveup – I have no more time, I need to get to class!!!
H or help – prints this list of options again
Remember that uppercase and lowercase SHOULD NOT matter.
JUST GRAB WHAT YOU NEED AND GET TO CLASS TO START YOUR FINAL EXAM!!!
HURRYYYY!!!
You see a empty classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Up.
In room 1 with course empty
Backpack: Empty
Enter a command (H for help): U
You went Up and found a new classroom.
In room 2 with course MTH132
Backpack: Empty
Enter a command (H for help): R
You went Right and found a new classroom.
In room 4 with course MTH235
Backpack: Empty
Enter a command (H for help): S
You see a MTH235 classroom. On the desk you see a cheatsheet. Run
through the classroom grab what you need (if possible). Now, run into
the hallway and go Left or Up.
In room 4 with course MTH235
Backpack: Empty
Enter a command (H for help): P cheatsheet
In room 4 with course MTH235
Backpack: cheatsheet
Enter a command (H for help): U
You went Up and found a new classroom.
In room 7 with course MMG201
Backpack: cheatsheet
Enter a command (H for help): U
You went Up and found a new classroom.
In room 10 with course CSE231
Backpack: cheatsheet
Enter a command (H for help): R
You went Right and found a new classroom.
In room 11 with course EGR100
Backpack: cheatsheet
Enter a command (H for help): R
You went Right and found a new classroom.
In room 12 with course ME201
Backpack: cheatsheet
Enter a command (H for help): D
You went Down and found a new classroom.
In room 8 with course ME211
Backpack: cheatsheet
Enter a command (H for help): S
You see a ME211 classroom. On the desk you see a pencil, a dress, and
a boot. Run through the classroom grab what you need (if possible).
Now, run into the hallway and go Up.
In room 8 with course ME211
Backpack: cheatsheet
Enter a command (H for help): P pencil
In room 8 with course ME211
Backpack: cheatsheet, pencil
Enter a command (H for help): U
You went Up and found a new classroom.
In room 12 with course ME201
Backpack: cheatsheet, pencil
Enter a command (H for help): R
You went Right and found a new classroom.
In room 5 with course BS161
Backpack: cheatsheet, pencil
Enter a command (H for help): P paper
In room 5 with course BS161
Backpack: cheatsheet, pencil, paper
Enter a command (H for help): R
You went Right and found a new classroom.
In room 6 with course MSE250
Backpack: cheatsheet, pencil, paper
Enter a command (H for help): P eraser
In room 6 with course MSE250
Backpack: cheatsheet, pencil, paper, eraser
Enter a command (H for help): U
You went Up and found a new classroom.
You succeeded!
Test 3:
Enter a text filename: rushing.txt
AHHHH! I’m late for class
*runs out the house to catch the bus with an empty backpack*
You’re popular and have friends in many classes. Find and collect any
items you find useful for your exam.
You are already late, and have a CSE231 Final Exam in 10 mins.
Use your instincts:
*thinks*.. *thinks*.. what to do?!?!?!?!
*running*
S or search — prints a description of the classroom you ran into
B or backpack – prints a list of items in your backpack
P pencil or pickup pencil – *mental* instruction to pick up an item
called pencil
DR pencil or drop pencil – *mental* instruction to drop off an item
called pencil
U or up – *mental* instruction to up the hallway to find another
classroom
D or down – *mental* instruction to down the hallway to find another
classroom
R or right – *mental* instruction to right in the hallway to find
another classroom
L or left – *mental* instruction to left in the hallway to find
another classroom
G or giveup – I have no more time, I need to get to class!!!
H or help – prints this list of options again
Remember that uppercase and lowercase SHOULD NOT matter.
JUST GRAB WHAT YOU NEED AND GET TO CLASS TO START YOUR FINAL EXAM!!!
HURRYYYY!!!
You see a empty classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Up.
In room 1 with course empty
Backpack: Empty
Enter a command (H for help): D
Unfortunately, you went Down and there was no classroom.
In room 1 with course empty
Backpack: Empty
Enter a command (H for help): u
You went Up and found a new classroom.
In room 2 with course MTH132
Backpack: Empty
Enter a command (H for help): p book
In room 2 with course MTH132
Backpack: book
Enter a command (H for help): l
You went Left and found a new classroom.
In room 3 with course CM161
Backpack: book
Enter a command (H for help): p testtube
In room 3 with course CM161
Backpack: book, testtube
Enter a command (H for help): s
You see a CM161 classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Right or Up.
In room 3 with course CM161
Backpack: book, testtube
Enter a command (H for help): u
You went Up and found a new classroom.
In room 6 with course MSE250
Backpack: book, testtube
Enter a command (H for help): p pen
In room 6 with course MSE250
Backpack: book, testtube, pen
Enter a command (H for help): s
You see a MSE250 classroom. On the desk you see a book and a eraser.
Run through the classroom grab what you need (if possible). Now, run
into the hallway and go Down, Up, or Left.
In room 6 with course MSE250
Backpack: book, testtube, pen
Enter a command (H for help): p eraser
In room 6 with course MSE250
Backpack: book, testtube, pen, eraser
Enter a command (H for help): s
You see a MSE250 classroom. On the desk you see a book. Run through
the classroom grab what you need (if possible). Now, run into the
hallway and go Down, Up, or Left.
In room 6 with course MSE250
Backpack: book, testtube, pen, eraser
Enter a command (H for help): u
You went Up and found a new classroom.
In room 10 with course CSE231
Backpack: book, testtube, pen, eraser
Enter a command (H for help): s
You see a CSE231 classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Left, Down, or
Right.
In room 10 with course CSE231
Backpack: book, testtube, pen, eraser
Enter a command (H for help): dr book
In room 10 with course CSE231
Backpack: testtube, pen, eraser
Enter a command (H for help): s
You see a CSE231 classroom. On the desk you see a book. Run through
the classroom grab what you need (if possible). Now, run into the
hallway and go Left, Down, or Right.
In room 10 with course CSE231
Backpack: testtube, pen, eraser
Enter a command (H for help): d
You went Down and found a new classroom.
In room 15 with course HNF400
Backpack: testtube, pen, eraser
Enter a command (H for help): p towel
In room 15 with course HNF400
Backpack: testtube, pen, eraser, towel
Enter a command (H for help): p bathrobe
In room 15 with course HNF400
Backpack: testtube, pen, eraser, towel, bathrobe
Enter a command (H for help): s
You see a HNF400 classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Left or Down.
In room 15 with course HNF400
Backpack: testtube, pen, eraser, towel, bathrobe
Enter a command (H for help): d
You went Down and found a new classroom.
In room 10 with course CSE231
Backpack: testtube, pen, eraser, towel, bathrobe
Enter a command (H for help): p book
In room 10 with course CSE231
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): s
You see a CSE231 classroom. Run through the classroom grab what you
need (if possible). Now, run into the hallway and go Left, Down, or
Right.
In room 10 with course CSE231
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): l
You went Left and found a new classroom.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): s
You see a MMG201 classroom. On the desk you see a microscope, a glass,
a glove, and a stapler. Run through the classroom grab what you need
(if possible). Now, run into the hallway and go Down or Up.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): p stapler
Backpack is full.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): s
You see a MMG201 classroom. On the desk you see a microscope, a glass,
and a glove. Run through the classroom grab what you need (if
possible). Now, run into the hallway and go Down or Up.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): dr stapler
Failed to remove item from backpack.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): s
You see a MMG201 classroom. On the desk you see a microscope, a glass,
and a glove. Run through the classroom grab what you need (if
possible). Now, run into the hallway and go Down or Up.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): r
Unfortunately, you went Right and there was no classroom.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): s
You see a MMG201 classroom. On the desk you see a microscope, a glass,
and a glove. Run through the classroom grab what you need (if
possible). Now, run into the hallway and go Down or Up.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): p pencil
Failure to find the item in the classroom.
In room 7 with course MMG201
Backpack: testtube, pen, eraser, towel, bathrobe, book
Enter a command (H for help): q
Unfortunately, that’s not a valid option.
Thank you for playing
Grading Rubric
Computer Project #11
Scoring Summary
General Requirements:
(5 pts) Coding Standard 1-9
(descriptive comments, mnemonic identifiers, format, etc…)
Implementations:
(10 pts) Student class
(2 pts) place
(3 pts) add_item
(3 pts) remove_item
(2 pts) __eq__
(15 pts) Classroom class
(4 pts) __init__
(3 pts) add_item
(3 pts) remove_item
(3 pts) get_room
(2 pts) __eq__
(20 pts) Rush class
(4 pts) __init__
(2 pts) backpack
(3 pts) pickup
(3 pts) drop
(4 pts) move
(3 pts) win
(3 pts) Test 1
(4 pts) Test 2
(3 pts) Test 3