程序代写代做 game go graph Excel COMP 202

COMP 202
posted: due:
Primary Learning Objectives
Summer 2020
Saturday, June 13, 2020 Monday, June 22, at 23:59
Assignment 4
By the end of this assignment, students should be able to…
• Use Matplotlib to plot numerical data in different formats.
• Implement an object-oriented program and write code to appropriately use classes, methods, and objects.
• Scrape data from web pages and store it to disk in the form of Python objects. Submission instructions
• This file is contained within a .zip file. Also in the .zip file are a series of Python code files. You will write the solutions to this assignment in these Python code files. (Make sure to unzip the zip file, as you cannot write solutions in files which are inside a zip file.)
• When ready to submit, upload each Python code file to codePost under the appropriate heading. (Make sure to upload each code file individually, and not the entire folder at once.)
• You can submit multiple times on codePost, so don’t worry if you realize that your current submission contains an error.
• Submit early, submit often! (Computer crashes do occur, and codePost may be overloaded during rush hours.)
Coding instructions
Please read the following instructions carefully to avoid losing marks.
• You must include your name and McGill ID number at the top of each .py file that you submit. By doing so, you are certifying that the code file is entirely your own, and represents the result of your sole effort.
• You are expected to comment your code, on average 1 comment for every 5-6 lines.
• You are expected to use descriptive variable names whenever possible. Do not use variable names like x, y or z (unless you are dealing with a mathematical function). Instead, use names like user input, sum of numbers, or average value.
1

• Some questions will ask you to print text to the screen in the exact same form as given in the examples. Make sure that the output of your program exactly matches the output of the examples in these cases or marks will be deducted.
• Some questions will ask you to define a function. The function you define must have the same name, parameters and return value as specified. Further, questions may have examples to show the output that your program is required to produce. Make sure that the output of your program exactly matches the output of the examples. (Input values are highlighted in gray, as they should be input by the user.)
• Those with prior programming knowledge may be able to solve a question with advanced Python concepts. Although it may be more efficient to do so, it defeats the purpose of the assignment as we are testing on specific language constructs. Therefore, please only use concepts (language constructs, types, functions, methods, etc.) seen in class. Solutions that use Python concepts not seen in class will be penalized. Further, the use of third-party modules is not permitted.
Policies
• Late assignments will be accepted up to 2 days (48 hours) after the due date and will be penalized by 10 points per day. Note that submitting one minute late is the same as submitting 23 hours late. We will deduct 10 points for any student who submits or resubmits after the due date irrespective of the reason, be it wrong file submitted, wrong file format submitted or any other reason. This policy will hold regardless of whether or not the student can provide proof that the assignment was indeed “done” on time.
• If your program does not work at all, e.g., gives an error and does not produce any output, zero points will be given for that question. If your program executes without errors but produces incorrect output, partial marks may be awarded based on the correctness of the code.
• If anything is unclear, it is up to you to seek clarification by either directly asking a TA during office hours or making a post on Piazza.
• Work submitted in this assignment must represent your own efforts. Assignments must be done individually; you must not work in groups. Do not ask friends or tutors to do this assignment for you. You must not copy any other person’s work in any manner (electronically or otherwise), nor give a copy of your work to any other person. Code similarity detection software will be run on all assignments. Students under suspicion of plagiarism will be reported to their faculty’s Disciplinary Officer.
2

Figure 1: A portion of the data in midterm.csv. Question 1 [35 points]
In the year 202X, in a major university on the North American continent, students in an introduc- tory programming class, COMPUT 2202, underwent a midterm examination. This midterm exam consisted of several different topics, drawn from a large pool of topics, where each topic had several questions.
The data for this imaginary midterm exam can be found (in part) in the file midterm.csv, a file of comma-separated values. By opening this file in a text editor (or more preferably, spreadsheet software such as Numbers or Excel), you will see the data in tabular format. See Figure 1 for an example of how it might look.
Each column in the file represents the midterm exam answers of an individual student (except the first column, which gives the row titles). In particular, the first row gives the student number, the second and third rows give the number of topics that student did not complete (due to lack of time, e.g.,), and the fourth column gives the ID of the individual who graded their exam.
The rest of the rows are divided into topics and questions for those topics. Each topic begins with a row called ”Topic X”, where X is the topic number (from 1 to 13). An X in that row for any student’s column means that that student was given that topic during the exam. The rows that follow the topic row are the questions for that topic. Each topic has a certain number of questions (between 1 and 6). An X in a question row for any student’s column means that that student answered that question correctly.
In this question, we will write various functions that calculate statistics of the data (average, etc.), as well as plot these statistics to the screen using Matplotlib, in the file midterm.py. Examples of how the plots should look can be seen in Figure 2.
3

Figure 2: Examples of how the plots should look.
Before we get started, note that several functions to process the data, as listed below, are already defined for you. It is up to you to decide where and when to use them in your own code.
read csv(filename): Takes a filename of a .csv file as parameter and returns the list of lists for the data in that file.
count x in row(row): Takes a list as parameter and returns the number of x and X in that list.
get row for question(data, topic, question): Takes the midterm data as list of lists and topic and question number, and returns the row (list) corresponding to that question.
get num questions in topic(data, topic): Takes the midterm data as list of lists and topic number, and returns the number of questions in that topic.
get num students(data): Takes the midterm data as list of lists, and returns the total number of students who sat for the midterm.
get num students for topic(data, topic): Takes the midterm data as list of lists and topic number, and returns the total number of students who were given that topic during the exam.
get topics for student(data, student col index): Takes the midterm data as list of lists and a column index for a particular student, and returns the list of topics that that student was given during the exam.
Basic functions (30 points)
plot num students per topic(data)
The first plot we will make relates to the number of students who were given each topic during the exam. For example, say 100 students were given topic 1, while 95 students were given topic 5.
We will display this data in a pie chart. The function to make a pie chart is as follows:
plt.pie(data, labels=categories)
4

where data is a list of percentages, each percentage in the range [0.0, 1.0], corresponding to the per- centage of students that were given a topic relative to the total number of students, and categories is a list of labels (e.g., ‘Topic 1’, ‘Topic 2’, etc.), where each element at position i of categories refers to the percentage at position i of data.
Make sure to give your pie chart a title, and then show it to the screen.
The next calculation we will make is the average per topic. For example, if all students who took topic 1 did very well on all the questions in topic 1, then topic 1 will have a high average. We will calculate the exact value by writing three functions as follows.
get average of question(data, topic, question)
Given a particular topic and question number, calculate and return the average for that question, which is simply the number of correct answers for that question (for all student columns) divided by the total number of students who were given that topic.
get average of topic(data, topic)
Given a particular topic, calculate the average for each question in that topic, then calculate and return the average of all those question averages.
plot topic averages(data)
Display the average for each topic in a bar chart, where each bar corresponds to the average of a different topic, and show it to the screen. Make sure to give titles for your plot, x-axis, and y-axis.
Also, make sure that the topic number (1, 2, …, 13) shows underneath each bar (under the x-axis). You can do so by calling the plt.xticks function, passing a list of integers (topic numbers from 1 to 13) as argument.
We will next calculate the student averages for the entire exam.
get student average(data, student col index)
Given a particular column index (referring to an individual student), calculate their average for the exam.
Note that you will have to go through each topic, check if the student was given that topic, and if so, count the number of questions in that topic, and count how many of those questions they got correct. Do this for each topic, keeping count as you go, and when done, you can take the number of questions they got correct and divide it by the total number of questions they were given.
If they were given 0 questions, instead return the None value.
get student averages(data)
Return a list containing the average for each student (by calling your function above for every student, but excluding None values).
5

plot student averages(data)
Display the averages for all students in a histogram, and show it to the screen. Make sure to give your histogram a title. Make the x-axis range from 0.0 to 1.0 (you can again use the plt.xticks function, providing a list of values of increments of 0.1).
A histogram can be produced in Matplotlib by calling plt.hist(data, bins=x), where data is the list of percentages between 0 and 1 (e.g., student averages), and x is the number of ‘bins’, or separate bars, to display in the histogram. Use 20 bins for your histogram.
Finally, we will write a function to determine the so-called ‘hard’ questions on the exam.
get hard questions(data)
Go through every question of every topic, calculate the average for that question, and check if the average is less than 0.4. Return a list of strings of the format ‘TxQy’, where x and y correspond to the topic/question number whose average was less than 0.4. E.g., if topic 2 question 5 and topic 8 question 2 were hard, then the returned list should be [‘T2Q5’, ‘T8Q2’].
Additional functionality (5 points)
For the remaining 5 points of this question, you are encouraged to make a plot to display some other interesting characteristic of the data. The requirements to obtain the 5 points are as follows:
• The plot must be of a different type than the ones above (i.e., not a pie chart, bar chart or histogram).
• The plot must show some interesting characteristic of the data for which it was necessary to perform some computation and iteration (not a simple statistic like total number of students, or total number of questions per topic). If you are unsure if your idea qualifies, please ask on Piazza, as it will not be possible to make your case after submission if you are unhappy with the grade for these 5 points.
One example of an interesting statistic is the average of students per grader. There were five graders in total and each student was assigned to one grader. It may be interesting to see some kind of plot that shows the difference in student averages from grader to grader.
6

Question 2 [40 points]
I want to be the very best, Like no one ever was.
To catch them is my real test, To train them is my cause!
In this question we will write a Pokemon Battle Simulator.
Pokemon is a popular game in which ‘Pokemon Trainers’ battle each other using the ‘Pokemon’ they have captured. Each Pokemon has a name, a type relating to their abilities (water-based, electric-based, fire-based, etc.), and a list of attack moves (e.g., ‘tail swipe’, ‘mega punch’, etc.), amongst other attributes. A battle between Pokemon Trainers is turn-based, where each Pokemon uses one attack move on the opposing Pokemon, the move being chosen by the Pokemon Trainer. Instead of using an attack move, a Pokemon Trainer can also decide to use an item on their Pokemon (e.g., a health potion, which increases their hit points, or HP), or switch to another one of their Pokemon for the rest of the battle. If the hit points (HP) of a Pokemon reaches 0, they faint, and the Pokemon Trainer must send out another one of their Pokemon. The battle ends when a Pokemon Trainer has no more Pokemon left (i.e., they have all fainted).
Other important things to know include:
• After a Pokemon wins a battle, they gain experience points. When enough experience points have been gained, the Pokemon reaches a new level of strength.
• Certain types of Pokemon are strong against some types, and weak against others. For example, Pokemon of electric type are strong against ground type Pokemon, but weak against flying type Pokemon. Thus, in battle, their moves will be more and less effective, respectively.
The code for the battle simulation itself is already given to you in the file battle.py. Your job is to define the classes that this battle simulation depends on, as listed below.
Note that two other files besides battle.py are also provided to you:
• element types.py: Defines an Enum called ElementType for the different possible element
types (to be used for the pokemon type and move type attributes).
• main.py: The code that you will use to test your completed class definitions. The file creates two PokemonTrainer objects, adds various Pokemon and Item objects to them, then calls the battle function.
Core classes (30 points)
Each of the following classes should be written in its own file, except classes that inherit from other classes (which should be written in the same file as their parent class). In all, there will be four files for you to write and submit: pokemon trainer.py, pokemon.py, item.py and move.py.
7

PokemonTrainer class
The PokemonTrainer class defines a Pokemon trainer. A Pokemon trainer has several Pokemon, and also several items. A Pokemon trainer also has a name. Finally, a Pokemon trainer has a currently active Pokemon, indicating which Pokemon is currently active in the battle.
Initializer method
Parameter: a string name
Create the four attributes as described above: name (set to the parameter value), pokemon (set to
an empty list), items (set to an empty list), and current active pokemon (set to None). iadd (self, other) (overrides the += operator)
Parameter: an object obj
Use type-based dispatch to deal with the parameter. If it is of type Pokemon, call the add pokemon method on self and then return the self object. If it is of type Item, call the add item method on self and then return the self object. Otherwise, raise a TypeError with an appropriate error message.
add pokemon(self, pkmn) Parameter: a Pokemon object
Add the parameter into the list of Pokemon that the Pokemon trainer currently has. If there is no currently active Pokemon, then set it to the parameter.
add item(self, item)
Parameter: an Item object
Add the parameter into the list of items that the Pokemon trainer currently has.
switch active pokemon(self, index)
Parameter: an index into the list of Pokemon
Set the currently active Pokemon to be the Pokemon at the given index of the list of Pokemon.
has usable pokemon(self)
Return True if the Pokemon trainer still has usable Pokemon (i.e., a Pokemon that has not yet fainted); otherwise return False.
Move class
Initializer method
Parameters: a string name, an ElementType move type, and an integer damage. Set three attributes of the same names as the parameters for the self object.
8

str (self)
Return a string containing the values of all the attributes of the self object in a readable format.
E.g.,: Water Cyclone (type: WATER, damage: 5)
Item class
Initializer method
Parameters: a string name.
Set the attributes of the same name as the parameter for the self object. use(self, pokemon)
Raise the NotImplementedError.
HealthPotion sub-class (inheriting from Item)
Initializer method
Parameters: a string name and an integer amount of hp to restore.
Call the parent class’ initializer method on the self object with the name parameter, then assign the other parameter to the attribute of the same name.
use(self, pokemon) Parameter: a Pokemon object
Call the gain HP method on the Pokemon object, passing the amount of hp to restore attribute as argument.
Pokemon class
Initializer method
Parameters: a string name, an ElementType pokemon type and a list of Move objects moves.
Set two attributes of the same names as the parameters for the self object. Then, set values for the following attributes:
• level: 1
• exp: 0
• max hp: a random number between 0 and 100 • current hp: equal to the max HP
9

• attack power: a random number between 1 and 5 • defense power: a random number between 1 and 5 • fainted: False
str (self)
Return a string containing the values of some or all of the attributes of the self object in a readable
format. E.g.,: Staryu (WATER type); HP: 30/30; attack: 3; defense: 4 (If the Pokemon has fainted, add the word Fainted to the end.)
defend(self, strength)
Parameter: a positive integer strength
Calculate the damage to be inflicted on the self Pokemon object. The damage will be a random number in the range [1, strength – self.defense power]. Decrease the Pokemon’s current HP by the damage, and set fainted to True if their HP has reached 0. Finally, print out how much damage the Pokemon took (e.g., Staryu took 5 damage.), and if the damage is more than half of the Pokemon’s maximum HP, print Critical hit!.
attack(self, opponent, move)
Parameter: a Pokemon object opponent and a Move object move.
Call the defend method on the opponent, and give as argument the current Pokemon’s attack power multiplied by a random number in the range [1, move.damage].
gain HP(self, amount)
Parameter: a positive integer amount
Add the given amount to the Pokemon’s current HP (but make sure it does not rise above their maximum HP). Then, print a message saying how much HP was gained (e.g., Staryu gained 5 HP!).
gain exp(self, opponent level)
Parameter: a positive integer opponent level
Calculate the number of experience points the Pokemon has gained by taking a random number in the range [1, opponent level], add it to the Pokemon’s current experience points, and print a message saying how much experience was gained (e.g., Staryu gained 5 EXP!). If their experience points reach 10, set their experience back to 0 and increase their level by 1, and print a corresponding message (e.g., Staryu gained a level. They are now level 2.).
10

ElectricPokemon sub-class (inheriting from Pokemon)
Initializer method
Parameters: a string name and a list of Move objects moves.
Call the parent class’ initializer method on the self object with the given parameters, and ElementType.Electric for the move type parameter.
attack(self, opponent, move)
Parameter: a Pokemon object opponent and a Move object move. Check the pokemon type of the opponent Pokemon.
• If it is ElementType.GROUND, then print the message It’s super effective! and call the defend method on the opponent, giving as argument two times the current Pokemon’s attack power multiplied by a random number in the range [1, move.damage].
• If it is ElementType.ELECTRIC or ElementType.FLYING, then print the message It’s not very effective… and call the defend method on the opponent, giving as argument half the current Pokemon’s attack power multiplied by a random number in the range [1, move.damage].
• Otherwise, just call the parent class’ attack method, giving the parameters as arguments.
Additional functionality [10 points]
For the remaining 10 points of this question, you may choose two of the following enhancements to make to the program. Each enhancement, if properly implemented, is worth 5 points. Please make to describe in a comment at the top of your file(s) which enhancement(s) you have fulfilled. To obtain the full 5 marks for an enhancement, the behavior must be as described below and the comment must include various example tests (lines of code) that can be run to verify that the enhancement works properly.
• Add three other sub-classes of Item and/or Pokemon, and write code in main.py to add such new Pokemon/items to the player’s PokemonTrainer object (if adding new Pokemon, you can also edit the ElementType class by adding new element types).
• Enhance the Item class to use function objects (which will be seen later in the week). In particular, the class initializer should take a parameter for a function object and set it as attribute, then call that function when the use method is invoked.
• CreateanewAreaclassthathasalistofPokemonasattribute,thencreateanewvisit(area) function that takes an Area object as argument, lets the player move around the area or visit another area, and each time the player moves around the area, let them have a chance to catch one of the particular Pokemon contained in the area. The Pokemon should then be added into the player’s list of Pokemon.
11

• Edit the battle function to let the player run from a fight, based on a random chance that involves the opponent Pokemon’s level versus the player Pokemon’s level.
Other enhancements are possible; please post on Piazza if you have another idea of a similar level of complexity to the above.
12

Question 3 [25 points]
This question is intended to be an open-ended exploration of website data using techniques shown in class.
We will write our code in the file web.py. Note that there will be no codePost tests for this question; you must make sure it meets the following requirements before you submit it.
In this question, we ask you find a website that contains some kind of data in tabular or list format. For example, tabular data can be found on many pages on the Statistics Canada website; data in list form could be search results from Google, Amazon, or some other search engine.
You are encouraged to find a website with data that relates in some way to your major.
Once you find a page with the tabular or list data, you will need to find the XPath query or CSS selector string (or a combination of both) to scrape the data off of the web page.
You will then use the XPath and/or CSS selector string(s) in your Python code to retrieve the data and store it into a dictionary or list of lists. Once you have retrieved all the data, use Pickle to store it into a file.
Then, analyze the data by plotting it using Matplotlib, with at least two graphs showing different parts of the data. Save these graphs to disk.
Note: Before starting to write your code, make sure you have the correct XPath/CSS selector string. Also, when testing your code, comment out the code dealing with the retrieval of the data so that you can test all other parts of your code first (use empty lists or fake data instead of retrieving the actual data). Only when you are sure that it all works, then un-comment that code block and test the entire code file. We strongly recommend you follow this testing strategy since repeated downloading of data from a website can cause you to be automatically blocked from further visiting that web page, at which point you would have to change the website you chose for this question to a different one.
13