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.
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 space- separated 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. 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 (Remember that the Classroom and Student classes prints error messages).
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.
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)
The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.
Get Free Quote!
265 Experts Online