Assignment 2
Introduction Assignment Overview
The assignment is an individual assessment. It contributes 40% of your �nal marks.
The assignment consists of a programming exercise and a skill-based test (viva).
The due date for the programming exercises is on 9 May 2023, 11.59 pm (Sydney time).
The skill-based test will be held on weeks 12-13, refer to your own timetable for assessment time.
Restrictions Keywords
in global lambda nonlocal
Built-in Functions
This is an assignment, and sta� are not permitted to give guidance on your code or how to solve the speci�c problem. That is the purpose of the assessment that you are required to perform to achieve the grade.
You may ask clari�cation questions about the assignment description. This is often necessary to implement functionality that is otherwise ambiguous.
If you have a question to ask on Ed please search before asking. However, remember that you should not be posting any assignment code publicly, as this would constitute academic dishonesty. Also, do not wait too long before starting. This assignment needs time and sustained e�ort.
Late submissions are not accepted unless an approved special consideration is granted.
The following are not permitted to be used when completing this assignment:
dir() eval() enumerate() filter() globals() locals() map()
Only the following is allowed:
import math import datetime import sys import time import os import shutil import random import unittest
any module you have written, e.g. import shop
Everything else is restricted.
A submission containing a �le longer than 3000 lines or larger than 500kB will not be accepted. To check the �le sizes of your �les, use the bash command ls -s –block-size=KB
Programming Exercises
All submissions must be made via Ed, including any supporting documentation that is produced during the planning of the program design such as �owcharts, pseudocodes, and UML class diagrams.
To make a submission, you will need to press the “Mark” button. You may submit as many times as you wish before the due date.
Computer Science Tutoring
After each submission, you will be able to view all the previous submissions and their �les in the code submission section.
The following rules apply to your submission:
Your submission must be able to compile and run within the Ed environment provided.
Only the �les given in the sca�old codes will be started by the auto-marker. The marking system will automatically check your code against the public test cases.
Ensure that you have submitted your codes with the correct �le name as given in the questions’ sca�old. The auto-marker will only start the �les that are speci�ed in the given sca�old.
The Python version that is presently being used on the Ed system:
$ python3 –version Python 3.10.9
Help and feedback
You are encouraged to ask questions about the assignment during the Helpdesk and on the Ed discussion board; however, remember that you should not be posting any assignment code publicly, as this would constitute academic dishonesty.
Skill-based test
The maximum duration of the test is exactly 20 minutes. You will be asked questions to demonstrate, debug, trace and explain your work during the session. There will be sample questions provided for you to prepare and practice for the session. The examiner may ask questions that will not be included in this list of sample questions.
浙大学霸代写 加微信 cstutorcs
You must have your webcam turned on during the session and will be asked to show your Ed submission during the session. All sessions will be recorded and kept for documentation purposes in accordance with the university requirements regarding oral examinations.
Marking Criteria
Your marks for the assignment will be determined based on the number of test cases passed for each question as well as your performance during the skills-based test according to the following weights:
Automatic Tests (10/40)
Your marks for this component will be the ratio of the number of test cases passed to the sum of all the public test cases administered. It is important that you thoroughly test your own code.
Skills-based Test (30/40)
This is broken down into two parts:
Viva test cases (10/40)
There will be public, hidden, and private test cases administered on your submission. The private test cases will only be applied after the deadline and will be di�erent from the examples given. There will be public test cases available for feedback purposes – to allow you to test the conformance of your program with respect to the description but these do not test all the functionalities described in the assignment.
Viva interview (20/40)
The examiner may ask you questions that require you to explain, trace and debug your codes. Marks will be awarded for each question based on the following descriptors:
The candidate is unable to answer the question or the response is mostly incoherent.
Partial marks:
The candidate can respond to the question but after multiple attempt probes or some programming terminologies used in their explanation are incorrect.
Full marks:
The candidate is able to correctly respond to the question using the correct programming terminologies in their explanation.
Any attempts to deceive the automatic marking will result in an immediate zero for the entire assignment. Negative marks can be assigned if you do not properly follow the assignment description, or your code is unnecessarily or deliberately obfuscated.
The public test cases for the PIAT questions will be released in stages over the upcoming weeks. It is recommended that you write your own test cases without relying on the public test cases to check that your program satis�es the given requirements.
Student Declaration
By submitting this assignment, you declare the following:
I declare that I have read and understood the University of Sydney Student Plagiarism: Coursework Policy and Procedure, and except where speci�cally acknowledged, the work contained in this assignment/project is my own work and has not been copied from other sources or been previously submitted for award or assessment.
I understand that failure to comply with the Student Plagiarism: Coursework Policy and Procedure can lead to severe penalties as outlined under Chapter 8 of the University of Sydney By-Law 1999 (as amended). These penalties may be imposed in cases where any signi�cant portion of my submitted work has been copied without proper acknowledgement from other sources, including published works, the Internet, existing programs, the work of other students, or work previously submitted for other awards or assessments.
I realise that I may be asked to identify those portions of the work contributed by me and required to demonstrate my knowledge of the relevant material by answering oral questions or by undertaking supplementary work, either written or in the laboratory, in order to arrive at the �nal assessment mark.
I acknowledge that the School of Computer Science, in assessing this assignment, may reproduce it entirely, may provide a copy to another member of faculty, and/or communicate a copy of this assignment to a plagiarism checking service or in-house computer program, and that a copy of the assignment may be maintained by the service or the School of Computer Science for the purpose of future plagiarism checking.
Log of Fixes
A log of �xes made to Assignment 2. These consist of:
Buggy test cases.
Typos in the question description.
The question description lacking su�cient detail.
Pinned Fixes
This was originally in the assignment description.
Before we begin, let’s train you up!
Press “Enter” to start training or “skip” to Start Game: #^[
This has now been removed.
New information callouts have been added to the top of 8. PIAT: Something Blue question description.
Additionally, import unittest has been allowed, however, this is purely an auxiliary for students who want to use this module. No part of the assignment is required to use this.
New information callouts have been added to the top of 7. PIAT: “Improved” Full Game question description.
The following requirement has been removed from Q2:
If the example output in the question description and the output checked by test cases don’t match, only the test cases will be changed to make it match.
Entering ^[ when prompted to skip will not be tested.
IftheuserentersESC+Enter,theyareautomaticallygiventheCardboard and Hook Trap,andthegame immediately loads and players are taken to the game menu.
Questions no longer have a requirement to be run in the main function apart from train.py in Q2 and the overall question in Q6. However, the code for most questions (speci�ed at the top) must only be run if __name__ is equal to __main__ , so ideally you should have one anyway.
10th April:
Added the following to 2. New Game Features .
14th April:
Changed 5.6 to make the requirements clearer.
19th April:
Added the example outputs to 7.1 Verify Installation.
Example output of the function run_setup for di�erent scenarios leading towards the launch of the game:
Tampered and repair game �les
Do you want to repair the game? no
Game may malfunction and personalization will be locked. Are you sure you want to proceed? yes
You have been warned!!!
Launching game
Clarify parameters for function personalization and return type of the function.
Implement the function personalization in game_final.py to allow players to select their own identi�er in the game if the selected name is valid. The function has a single parameter that determines if the �les in the default directories are tampered and returns the selected identi�er by the player.
21st April:
One-time enchantment should wear o� after a round of hunting. The typo One-Time Enchanted High Strain Steel Trap is armed with Cheddar! is updated to High Strain Steel Trap is armed with Cheddar! in Q7 full game output to re�ect this.
The function consume_cheese in game_final.py in Question 7.5 C is updated to remove the return value.
Fixes 5th April
7:24PM: Added in 1. Upgraded Cheese Shop at the bottom that a main function must be implemented, with it only being called if the __name__ value is ‘__main__’ . This has already been stated at the very top of the question description, however we have now added the function to the description.
7:27PM: Added in 5. Kids Friendly at the bottom that a main function must be implemented, with it only being called if the __name__ value is ‘__main__’ . This has already been stated at the very top of the question description, however we have now added the function to the description.
10:32PM: Added You do not have to arm the trap after every single hunt. Example: If you have 10 cheddar, arming the trap with cheddar will allow you to hunt 10 times (assuming valid inputs) until you can no longer hunt. to make it more explicit that arming the trap only has to be done once per intended uses of a cheese type. It does not have to be done after every single hunt.
2:10PM: Changed the output for 1. Upgraded Cheese Shop slide such that inputs such as CheDDar will show output such as Successfully purchase 1 cheddar if the user inputs 1 CheDDar .
3:11PM: Description in 1. Upgraded Cheese Shop said Must purchase positive amount of cheese. but the test cases tested Must purchase a positive amount of cheese. The test cases were changed to match the description, such that the test cases matched the description.
11:19PM: Fixed output snippet in 2. New Game Features that accidentally had it so not having a trap and sounding the horn did catch a mouse. This has been �xed to show the tip by Larry.
9th April:
2:30PM: Made the location of the config.txt �le clear in 6. PIAT: Check Setup , and added an
empty con�g �le for reference.
10th April:
3:19PM: Fixed veri�cation successful output to check 2 directories and not 3 .
3:35PM: Fixed output snippet such that train.py is not responsible for prompting the user to skip. This keeps the code consistent with Assignment 1.
11th April:
4:39PM: Fixed bug in 2. New Game Features where it did not check the output Do nothing. if invalid commands were to be entered.
9:10PM: The function change_cheese in the docstring has trap_stats as str . This was changed to be bool .
Assignment Overview
Your task in this assignment is to expand the game that you have from Assignment 1 to add additional features to the game. The game will remain a single-player desktop game but will now have the following features:
three types of cheeses: Cheddar , Marble and Swiss . Players must be able to select the correct type of cheese to buy in the 1. Upgraded Cheese Shop. They can then arm the cheese in 2. New Game Features – 2.2 How do I arm my trap with new cheese?
�ve types of mouse: Brown , Field , Grey , White , and Tiny . It became a celebratory event when this happened, see 3. New Mouse Release. But the game with new mouse wasn’t released until you could con�rm that the cheese shop and mouse randomisation are actually working in 4. Testing.
a conscious e�ort to be politically correct by making sure the game generates a neutral name when any profanities are found in 5. Kids Friendly.
and a mechanism to check that the game is set up correctly for your random name generator and profanity feature to work correctly in 6. PIAT: Check Setup.
three types of traps: High Strain Steel Trap , Hot Tub Trap , and Cardboard and Hook with short-term unique e�ects in 7. PIAT: “Improved” Full Game.
An improved user interface showing each mouse’s coat of arms if they are caught during the hunt and better handling of user input errors in the �nal version of the game.
Being superstitious, the game creators requested that you follow the adage Something Old, Something New and Something Borrowed while putting the whole game together using your solutions from the previous parts in Assignment 2. Hence, the last part of the last question 7. PIAT: “Improved” Full Game is split into:
Something “Old” – Rehash of the old game menu, dialogue and gameplay.
Something “New” – New mouse with new cheese and new user-friendly game interface Something “Borrowed” – Larry’s enchanted trap with special e�ects on one hunt
Once you have completed the game and con�rmed that it works, you realise that you forgot Something “Blue” while watching your favourite show, Dr Who. In 8. PIAT: Something Blue – Game Redux OO, you decide to turn yourself blue, by re-hashing the whole game without changing its current behaviour, using object-oriented (OO) design by implementing four new classes:
Trap class Hunter class CheeseShop class Interface class
This assignment assumes you are comfortable with importing modules and reusing functions. This assignment will be nightmarish if you require copying all the code from your previous questions into
We highly recommend you spend some time being comfortable with concepts such as: 1. Using functions from imported modules.
2. The use of the if __name__ == ‘__main__’ condition.
3. Decomposing your code into functions such that it’s highly reusable and easy to debug.
Code Help
1. Upgraded Cheese Shop (2 marks)
This question requires contents from Week 6.
Write your solutions into shop.py .
You must only run your code in shop.py if the expression if __name__ == ‘__main__’ is True .
Failure to do so will cause your program to not pass any of the test cases.
The Cheese Shop now carries two additional type of cheese: Marble and Swiss . Each costs 50 and 100 gold respectively.
1.1 Update Cheese Menu
Modify the shop.py from Assignment 1 to include these new cheese and their prices when players
visit The Cheese Shop. The program should now display:
Welcome to The Cheese Shop! Cheddar – 10 gold
Marble – 50 gold
Swiss – 100 gold
How can I help ye? 1. Buy cheese
2. View inventory 3. Leave shop
In the code sca�old, you are given each type of cheese and its price in the variable CHEESE_MENU . You must not change or modify this variable.
1.2. Upgrade Buying Process
Update the function buy_cheese to repeatedly allow users to continue making purchases unless they enter the command back . The function receives one parameter, an int to represent the current
gold the player has.
The function returns a tuple with 2 elements. The �rst element is an int of the amount of gold spent. The second element is a tuple with the amount of each type of cheese bought in the same order as that given in the CHEESE_MENU in the code sca�old. As an example, if the user has 75 gold and buys 2 cheddar and 1 marble, the return value would be (70, (2, 1, 0)) .
def buy_cheese(gold: int) -> tuple:
Feature for players to buy cheese from shop Parameters:
gold: int, amount of gold that player has Returns:
gold_spent: int, amount of gold spent
cheese_bought: tuple, amount of each type of cheese bought ”’
If the player enters a cheese that is not sold by the shop, the program displays We don’t sell {player_request} ! where {player_request} is replaced with the player’s requested cheese in lowercase and the program prompts for another cheese and quantity input from the user. Otherwise, it will continue to check the following:
Missing quantity. : If users does not enter a quantity.
Invalid quantity. : If users enters an invalid number as the quantity e.g. one , wahid .
Must purchase positive amount of cheese. : If users enters 0 or a negative number as the quantity.
There should only ever be one error message per input if the input is invalid, and the error messages are in order of how they have been listed. So as example, an input such as info1110 would display
We don’t sell info1110! but not also display Missing quantity. . Another example, if the user enters both an invalid cheese and quantity e.g. brie one , the former takes priority i.e. it will display the error message We don’t sell brie! and continue asking for input.
There is now a change to the shop. When buying cheese, regardless of whether it is a valid or invalid command, players are prompted to buy more cheese. The only way to return back to main menu of The Cheese Shop is if they enter the back command.
All commands when buying cheese are case insensitive, so as examples, back and BACK are both valid commands that will move the players back to the shop menu. cheddar 10 and CHEDdaR 10 will buy 10 cheddar, assuming the player has su�cient gold. For this question, you may assume that all cheeses in the Cheese Shop is a single word.
This is an example input/outputs produced by the updated game:
Welcome to The Cheese Shop! Cheddar – 10 gold
Marble – 50 gold
Swiss – 100 gold
How can I help ye?
1. Buy cheese
2. View inventory
3. Leave shop
You have 125 gold to spend. State [cheese quantity]: #brie
We don’t sell brie!
You have 125 gold to spend.
State [cheese quantity]: #marble Missing quantity.
You have 125 gold to spend.
State [cheese quantity]: #marble 10 Insufficient gold.
You have 125 gold to spend.
State [cheese quantity]: #marble 1 Successfully purchase 1 marble.
You have 75 gold to spend.
State [cheese quantity]: #back
How can I help ye? 1. Buy cheese
2. View inventory 3. Leave shop
From the example above, the buy_cheese function is responsible for showing this output:
You have 125 gold to spend.
State [cheese quantity]: #brie
We don’t sell brie!
You have 125 gold to spend.
State [cheese quantity]: #marble Missing quantity.
You have 125 gold to spend.
State [cheese quantity]: #marble 10 Insufficient gold.
You have 125 gold to spend.
State [cheese quantity]: #swiss 1 Successfully purchase 1 swiss.
You have 25 gold to spend.
State [cheese quantity]: #cheddar 2 Successfully purchase 2 cheddar. You have 5 gold to spend.
State [cheese quantity]: #back
The function would return (120, (2, 0, 1)) as the player spent 120 gold, and bought 2 cheddar and 1 swiss.
You may be asking “Why are we using tuples and not lists for the return value?”
Tuples are immutable, meaning once it’s instantiated it cannot be changed. The idea behind it is that if a function returns you a tuple, it’s a de�nite answer and cannot be changed at any point in the program. If there were to be any accidental changes on this return value, whether it be from the user or even the function itself, it would raise an error. This guarantees the user is using the intended values returned from the function.
In simple words, it’s good design.
In the template, you’ve been provided two things. First, you have a cheese menu:
1.3. Update Inventory View
The inventory must be updated to show the quantity of each type of cheese. If a player doesn’t have any of that particular cheese, it must show 0 . The gold and quantity of each type of cheese can never be negative numbers.
Update the function display_inventory to receive three parameters of type int , list , and str , being the gold, cheese and trap respectively.
def display_inventory(gold: int, cheese: list, trap: str) -> None: ”’
Displays contents of inventory Parameters:
gold: int, amount of gold that player has
cheese: list, amount of each type of cheese that player has trap: str, name of trap that player that player has
At the start of the game, players will have 125 gold and no cheese. This is an example of the input/output produced by the program of the view of the inventory before and after buying 1 marble and 1 cheddar from the shop.
Welcome to The Cheese Shop! Cheddar – 10 gold
Marble – 50 gold
Swiss – 100 gold
How can I help ye? 1. Buy cheese
2. View inventory 3. Leave shop
Gold – 125
Cheddar – 0
Marble – 0
Trap – Cardboard and Hook Trap
CHEESE_MENU = ((“Cheddar”, 10), (“Marble”, 50), (“Swiss”, 100))
This is the menu for the shop. Each inner tuple contains both the name of the cheese and its price. This speci�c structure ensures the cheese menu does not change.
Secondly, you’ve been provided your own cheese:
cheese = [[“Cheddar”, 0], [“Marble”, 0], [“Swiss”, 0]]
These are the initial quantities of each cheese, as the player starts with 0 of each. This speci�c structure allows you to freely change the quantities for each cheese when you wish.
You do not have to use either of these in your solution, however it would likely make your program easier to read if you were to use containers.
How can I help ye?
1. Buy cheese
2. View inventory
3. Leave shop
You have 125 gold to spend.
State [cheese quantity]: #marble 1 Successfully purchase 1 marble.
You have 75 gold to spend.
State [cheese quantity]: #cheddar 1 Successfully purchase 1 cheddar. You have 65 gold to spend.
State [cheese quantity]: #back
How can I help ye?
1. Buy cheese
2. View inventory
3. Leave shop
Cheddar – 1
Marble – 1
Trap – Cardboard and Hook Trap
How can I help ye?
1. Buy cheese
2. View inventory
3. Leave shop
From the example above, the display_inventory function would be responsible for showing this
Gold – 125
Cheddar – 0
Marble – 0
Trap – Cardboard and Hook Trap
This function would then be called again later in the program to display the next output:
Cheddar – 1
Marble – 1
Trap – Cardboard and Hook Trap
2. New Game Features (2 marks)
Write Python codes to build the following features on top of the original Mousehunt game from Assignment 1.
2.1 Skip Feature
The developers received feedback that the training module is annoying to type skip every time and have requested for a quicker way to skip. You are asked to modify the current program to additionally enable players to skip training by entering ESC+Enter at any time. Even if they repeat the training, they can still enter ESC+Enter .
Modify train.py to check whether the user has entered ESC+Enter during each user prompt. This user prompt is not inclusive of the “skip” prompt given at the very start.
Additionally, all commands in this game feature are case insensitive and ignore trailing and leading whitespaces in the command. Ensure you update game.py such that these changes are re�ected.
These are example input/outputs in which players can skip training for di�erent scenarios during training. Note that players can’t enter ^[ when prompted to skip and it will not be tested. The �rst snippet below is when they can begin entering ^[ .
This question requires contents from Week 7.
Write your solutions into train.py and game.py . Read the question carefully to identify the correct location to place your solution!
You must only run your code in game.py if the expression if __name__ == ‘__main__’ is True .
You must only run your code in train.py if the expression if __name__ == ‘__main__’ is True . All your code that executes must be inside a main function.
Failure to do so will cause your program to not pass any of the test cases.
Hint: All symbols have an equivalent numeric value. Try an Internet search on the ASCII table to see what its value is, and see how it will help you!
When users enter ESC , it will appear as ^[ in the terminal.
Ideally, if you’ve imported the code from train.py into game.py , you wouldn’t need to change a single line in game.py for this game feature. It’s only required if you’re duplicating code.
Skip training when prompted to travel to the Meadow.
Larry: I’m Larry. I’ll be your hunting instructor. Larry: Let’s go to the Meadow to begin your training! Press Enter to travel to the Meadow…#^[
Skip training when prompted to view traps.
Larry: I’m Larry. I’ll be your hunting instructor. Larry: Let’s go to the Meadow to begin your training! Press Enter to travel to the Meadow…
Travelling to the Meadow…
Larry: This is your camp. Here you’ll set up your mouse trap. Larry: Let’s get your first trap…
Press Enter to view traps that Larry is holding…#^[
Skip training when prompted to select traps
Larry: I’m Larry. I’ll be your hunting instructor. Larry: Let’s go to the Meadow to begin your training! Press Enter to travel to the Meadow…
Travelling to the Meadow…
Larry: This is your camp. Here you’ll set up your mouse trap. Larry: Let’s get your first trap…
Press Enter to view traps that Larry is holding…
Larry is holding…
Left: High Strain Steel Trap
Right: Hot Tub Trap
Select a trap by typing “left” or “right”: #^[
Skip training when prompted to sound horn.
Larry: I’m Larry. I’ll be your hunting instructor. Larry: Let’s go to the Meadow to begin your training! Press Enter to travel to the Meadow…#
Travelling to the Meadow…
Larry: This is your camp. Here you’ll set up your mouse trap. Larry: Let’s get your first trap…
Press Enter to view traps that Larry is holding…#
Larry is holding…
Left: High Strain Steel Trap
Right: Hot Tub Trap
Select a trap by typing “left” or “right”: # Invalid command! No trap selected.
Larry: Odds are slim with no trap!
Sound the horn to call for the mouse… Sound the horn by typing “yes”: #^[
Skip training when prompted to continue training.
Larry: I’m Larry. I’ll be your hunting instructor. Larry: Let’s go to the Meadow to begin your training!
Press Enter to travel to the Meadow…#
Travelling to the Meadow…
Larry: This is your camp. Here you’ll set up your mouse trap. Larry: Let’s get your first trap…
Press Enter to view traps that Larry is holding…#
Larry is holding…
Left: High Strain Steel Trap
Right: Hot Tub Trap
Select a trap by typing “left” or “right”: #
Invalid command! No trap selected.
Larry: Odds are slim with no trap!
Sound the horn to call for the mouse…
Sound the horn by typing “yes”: #yes
Nothing happens.
To catch a mouse, you need both trap and cheese!
Press Enter to continue training and “no” to stop training: #^[
The train.py �le must have a main function that only executes if the __name__ value is
‘__main__’ .
def main(): ”’
Implement your code here. ”’
if __name__ == ‘__main__’: main()
2.2 How do I arm my trap with new cheese?
Add another option to the main menu called 4. Change Cheese . When players select this option, the game displays the current quantity of cheese that are in the player’s inventory and then prompts the user to provide the type of cheese that they intend to arm their trap with.
If the player enters the command back , the game returns to the main menu. The game must be able to recognize any of the cheese that the Cheese Shop stocks, i.e. cheddar , marble and swiss . If an invalid cheese is provided or players attempt to arm a cheese that don’t possess, the game displays an appropriate warning message (see table 1), displays the player’s inventory again and re-prompts for another cheese.