ITP 115编程辅导、辅导Python编程语言、Python程序辅导
辅导Database|辅导P
ITP 115 – Programming in Python
2021-06-25 Page 1 of 13
Final Project – Pocket Monster Simulation
Goals
Complete a project that demonstrates problem solving skills and logical thinking.
Demonstrate knowledge of Python programming concepts learned throughout the
semester. You are not allowed to use Python concepts that were not taught this
semester in this course.
Overview
You will be writing a program that simulates catching, storing, and releasing Pocket
Monsters. You will implement different types of Pocket Monsters and balls.
It is recommended that you implement the code in the order listed below. After
completing each part, it is also recommended that you test the functionality of your
existing code before proceeding to the next part.
To best understand the structure and flow of the simulation, please read all the
directions through before beginning any code.
Requirements
In PyCharm, create a new project named project_last_first where last is your
last/family name and first is your preferred first name.
This project will be a folder containing the required class files you write, the given
helper file, the given txt file, and the given CSV file.
Use proper coding styles and comments.
Name newly created Python files as directed below.
Project should perform error-checking (on all inputs).
Use the Python concepts that we have covered during this semester.
You may NOT import the CSV or other modules.
ITP 115 – Programming in Python
2021-06-25 Page 2 of 13
Files Provided
Download the following Python files and put them in your newly created project.
GrassyField.py
We have provided a helper file for you called GrassyField.py which includes helper
methods to allow Pocket Monsters to randomly appear.
You will not need to interact directly with this file, however it is important that you add
it to your directory with the other project files.
Run.py
We have also provided a main function and one other function for you in a file called
Run.py which starts the simulation. This is the only file that you will need to run and it
should also be placed in the same directory as your project files.
You will need to update the trainer name (currently an empty string) to your own
trainer name.
pm.csv
A CSV (comma-separated values) file with PocketMonsters
Each line in the file represents a menu item:
o Name,Type,Strength,Rarity
CSV file example:
Bulbasaur,Grass,318,2
Ivysaur,Grass,405,3
Venusaur,Grass,525,5
Charmander,Fire,309,2
Charmeleon,Fire,405,3
There is not a header row in the CSV file.
ITP 115 – Programming in Python
2021-06-25 Page 3 of 13
Part 1 – Creating the Pocket Monsters
To represent a Pocket Monster, you will be creating two separate classes: PocketMonster
and PMIndex.
PocketMonster Class
Start by defining the PocketMonster class in a file titled PocketMonster.py.
This class will represent a single Pocket Monster.
The class will have the following instance attributes:
o self.name: a string containing the name of the item
o self.type: a string containing the category of the item
o self.strength: a float containing the price of the item
o self.rarity: a string containing a description of the item
Define the following methods:
o __init__
Parameters (4): name (string), type (string), strength (int), and rarity (int)
of the PocketMonster
Return value: None
Assign the inputs to the 4 instance attributes listed above.
o Define get methods for all the instance attributes. There is no need to define set
methods for the instance attributes.
o __str__
Parameters (0): None
Return value: a string
Construct a message containing all 4 attributes, formatted in a readable
manner such as:
Name (Type)
Strength: ___
Rarity: ___
For example:
Kangaskhan (Normal)
Strength: 490
Rarity: 4
ITP 115 – Programming in Python
2021-06-25 Page 4 of 13
PMIndex Class
Next, define the PMIndex class in a file titled PMIndex.py. This class will make use of
PocketMonster objects, so be sure to include the proper import statement.
This class represents the entire collection of pocket monsters categorized by their
different types.
The class will have a following class (or static) variable:
o TYPES: a list containing 15 strings, representing the 15 possible types of Pocket
Monsters: “Bug”, “Dragon”, “Electric”, “Fighting”, “Fire”, “Fairy”, “Ghost”,
“Grass”, “Ground”, “Ice”, “Normal”, “Poison”, “Psychic”, “Rock”, “Water”
The class will have the following instance attributes:
o self.bug: a list containing PocketMonster objects that are of the Bug type
o self.dragon: a list containing PocketMonster objects that are of the
Dragon type
o self.electric: a list containing PocketMonster objects that are of the
Electric type
o self.fighting: a list containing PocketMonster objects that are of the
Fighting type
o self.fire: a list containing PocketMonster objects that are of the Fire type
o self.fairy: a list containing PocketMonster objects that are of the Fairy
type
o self.ghost: a list containing PocketMonster objects that are of the Ghost
type
o self.grass: a list containing PocketMonster objects that are of the Grass
type
o self.ground: a list containing PocketMonster objects that are of the
Ground type
o self.ice: a list containing PocketMonster objects that are of the Ice type
o self.normal: a list containing PocketMonster objects that are of the
Normal type
o self.poison: a list containing PocketMonster objects that are of the Poison
type
o self.psychic: a list containing PocketMonster objects that are of the
Psychic type
o self.rock: a list containing PocketMonster objects that are of the Rock type
o self.water: a list containing PocketMonster objects that are of the Water
type
o Extra credit
ITP 115 – Programming in Python
2021-06-25 Page 5 of 13
Instead of 15 lists, you can use a single dictionary self.monsters: a
dictionary containing all the Pocket Monsters. The keys are strings
representing the types in the PMIndex class, and each value is a list of
PocketMonster objects depending on the key.
Use the TYPES class variable for the keys whenever possible.
Define the following methods:
o __init__
Parameter (1): the name of the CSV file (string) that contains the
information for the Pocket Monsters
Return value: None
Initialize each instance attribute to an empty list.
Open and read the CSV file.
Create a PocketMonster object from each line in the file. Use the type
to add the new object to one of the instance attributes. Note that each
line in the file contains the 4 pieces of information needed to create a
Pocket Monster object.
Close the file object.
o getMonster
Parameters (2): a type (string) and an index (integer) which is the
position of the Pocket Monster
Return value: a PocketMonster object from the appropriate instance
attribute using the index parameter
For error checking, make sure that the type parameter is one of the
TYPES.
For error checking, make sure that the index parameter is in the range of
the appropriate list.
Return the correct PocketMonster using its category and index
position.
If the type and/or index were not valid, then do not return anything.
o getMonsterType
Parameter (1): a type (string)
Return value: a list of Pocket Monsters of the appropriate type
For error checking, make sure that the type parameter is one of the
TYPES. If it is not, then don’t return anything.
o printMonsterType
Parameter (1): a type (string)
Return value: None
Print the list of Pocket Monsters given by the type input
ITP 115 – Programming in Python
2021-06-25 Page 6 of 13
For error checking, make sure that the type parameter is one of the
TYPES. If it is not, then don’t print anything.
Print a header with the type of Pocket Monster, followed by a numbered
list of all the menu items of that category. Start the numbering at 0.
Example:
TYPE: Rock
Geodude (Rock)
Strength: 300
Rarity: 1
Graveler (Rock)
Strength: 390
Rarity: 3
Golem (Rock)
Strength: 495
Rarity: 4
Onix (Rock)
Strength: 385
Rarity: 3
o getNumMonsterType
Parameter (1): a type (string)
Return value: an integer which is the number of PocketMonsters in the
appropriate list based on the type parameter
For error checking, make sure that the type parameter is one of the
TYPES. If it is not, then return 0.
o You do not need to define any get and set methods for the instance attributes of
this class.
o You are welcome to create the __str__ method that returns a string
containing the full Pocket Monster lists (all 15 of them). This can be used after
you create a PMIndex object by calling print() on the PMIndex object. By looking
at the output in the console window, you can make sure the menu was created
correctly in the __init__ method. This is not required.
At this point, you should be able to test the methods in the PMIndex class.
ITP 115 – Programming in Python
2021-06-25 Page 7 of 13
Part 2 – Creating Trainers
Next, define the Trainer class in a file titled Trainer.py. This class represents a
trainer who will use Catch Capsules to capture Pocket Monsters
The class will have the following class (or static) variables:
o MAXPM: this is the maximum Pocket Monsters a trainer is allowed to have at any
time. The value will be set at 6
o MAXCC: this is the maximum number of Catch Capsules a trainer is allowed to
have at any time. The value will be set at 10
The class will use the following instance attributes:
o self.name: a string containing the trainer’s name
o self.caught: a list of the PocketMonster objects caught by the trainer
o self.capsules: an integer corresponding to the number of capsules the
trainer has currently
Define the following methods:
o __init__
Parameter (1): a string containing the trainer’s name
Return value: None
Set the trainer’s name attribute to the input value. Set the trainer’s
caught attribute to an empty list (the trainer has not caught any Pocket
Monsters yet). Set the capsules attribute to 0
o Define get methods for all the instance attributes. There is no need to define set
methods for the instance attributes.
o getNumCaught
Parameters (0): None
Return value: integer representing the number of Pocket Monsters
caught by the trainer
o buyCapsules
Parameter (1): an integer representing how many capsules to buy
Return value: None
Checks the input amount and makes sure it is valid. Input amount should
be greater than 0 and the current capsule count plus the input amount
should not exceed the MAXCC value
o addMonster
Parameters (1): PocketMonster to add to list
Return value: None
Appends the Pocket Monster to the list of Pocket Monsters caught by
the trainer
o releaseMonster
ITP 115 – Programming in Python
2021-06-25 Page 8 of 13
Parameters (1): an integer representing an index
Return value: None
This function should remove the proper Pocket Monster (based on the
index given) from the list of caught Pocket Monsters and print a
“Goodbye” message
o encounter
Parameters (1): a PocketMonster to encounter
Return value: an integer which indicates how the encounter ended
This function should print the Pocket Monster that the user encountered
and then ask the user to perform one of two actions until the encounter
is over. The actions are:
0: Throw a Catch Capsule
1: Run
If the user chooses to throw a capsule, then decrease the trainer’s
capsule count by 1 and do the following:
Create a random integer value from 0-3900 (inclusive)
If the random integer is greater than or equal to the monster’s
strength * rarity then the user caught the monster. Return 1
If it’s not, the monster escaped the capsule
o If the random integer is less than the monster’s strength,
then the monster ran away. Return -1
o If the random integer is greater than or equal to the
monster’s strength, then ask for a new action
If the user runs out of capsules, the encounter is over. Return -2
If the user chooses to run, the encounter is over. Return 0
This function is fairly complex so to help diagram the flow of control, see
the following flowchart:
ITP 115 – Programming in Python
2021-06-25 Page 9 of 13
S
Get/validate
action
Action?
E
Run
Generate
random int
Caught?
Did it flee?
Capsules >
0?
Throw
Yes
No
Yes
No
No
Yes
ITP 115 – Programming in Python
2021-06-25 Page 10 of 13
o __str__
Parameters (0): None
Return value: a string
Construct a message containing the trainer’s name, number of capsules,
and the list of monsters they currently have caught.
Examples:
Trainer Stats:
Ray
Capsules: 1
Monsters:
Dratini (Dragon)
Strength: 300
Rarity: 2
Gastly (Ghost)
Strength: 310
Rarity: 1
ITP 115 – Programming in Python
2021-06-25 Page 11 of 13
Part 3 – Making it Work
Now you will make it all work. We have already defined the main function and one
other function for you, you simply need to write the following functions in Run.py to
make it all work.
Define the following functions:
o getMenuSelection
Parameter (0):
Return value: an integer representing the user’s menu choice
Print a list of options for the user to choose from. The options should be:
0: Explore the Grassy Fields
1: Release monsters
2: Enter the shop to purchase capsules
3: Print the trainer stats
4: Quit
Get the user’s input and validate that it’s one of the choices.
Return the user’s choice as an integer
o releaseMonsters
Parameter (1): a Trainer object
Return value: None
Print the list of monsters that the trainer has.
Ask the user to select which monster to release by selecting their index
Validate the user’s selection
Release the monster by calling the appropriate Trainer method
o enterShop
Parameters (1): a Trainer object
Return value: None
Ask the user how many capsules they would like to purchase. Make sure
the number they purchase is greater than 0. Also make sure the number
they purchase will not make the trainer’s capsule count exceed MAXCC
Call the appropriate Trainer method to set the new capsule count
ITP 115 – Programming in Python
2021-06-25 Page 12 of 13
Extra Credit (Up to 10% total)
Update the PMIndex class to use a single dictionary instead of 15 lists.
o Create an instance attribute self.monsters: a dictionary which contains all
the Pocket Monsters. The keys are strings representing the types of the menu
item, and each value is a list of PocketMonster objects depending on the
key.
o Update the methods in the PMIndex class appropriately.
o Use the TYPES class variable for the keys whenever possible.
ITP 115 – Programming in Python
2021-06-25 Page 13 of 13
Sample Output
Sample is shown a separate file.
Deliverables
Project that do not run will subject to an automatic 50% penalty.
Late submissions will not be accepted.
Submission Instructions
Compress your Python project folder (project_last_first) which contains your Python
source code. Your folder should contain the following files:
– PocketMonster.py, PMIndex.py, Trainer.py
– GrassyField.py, Run.py
– pm.csv
You should have a zip file entitled project_last_first.zip where last is replaced with
your last/family name and first is replaced with your first name.
Under Final Project on Blackboard, upload the zip file.
Grading
Item Points
PocketMonster Class 10
PMIndex Class 25
Trainer Class 25
Run.py Functions 20
Project executes successfully and has error checking 10
Proper coding style and detailed comments 10
Total* 100
* Points will be deducted for poor code style, or improper submission.