Question: A small-ish example: Player In this task, you'll be creating a class with a lot of guidance. Note: If you need examples to follow, the

A small-ish example: Player

In this task, you'll be creating a class with a lot of guidance.

Note: If you need examples to follow, the Class Design Recipe (Links to an external site.) may be of use to you.

The class you'll be creating will be called Player. Think about games like PacMan or Tetris, where players earn a score every time they play. For this specific problem, we'll use the following specifications:

A player has a name and a history of the last 100 scores they've achieved in the game. We need to be able to add new scores they get so we can determine their top score, and find their average score on their most recent n games (where n is some positive whole number).

For simplicity, we'll ignore the specifics of what game the Player is playing, and we'll only be concerned with the Player themself and their scores.

To start: define the class Player and give it an appropriate docstring. You won't be able to document its attributes yet, but you will soon.

You'll first want to decide what attributes the Player class will have, and what methods. To do this, you'll want to dissect the specifications piece by piece. With your group, try to answer to following questions:

  1. What attributes should a Player have? Think about what we need a Player to keep track of. Hint: the word have is intentionally bolded.
  2. What methods should a Player have? What should a Player be able to do? These should be actions that a Player can do. Hint: look for actions in the specification.

At this point, you should have a small list of attributes and methods. You can choose to implement them now, or you can write some sample usage first. Writing sample usage will help you iron out specific details you might need, such as the parameters you'll want for a method.

For this next part, use any attribute names and method names that seem reasonable. In the docstring of your Player class, write sample usage that does the following:

  1. Create a new Player with your name.
  2. Add 5 different scores to the Player's history of scores. (This might take multiple lines of code: that's fine! This is a design decision.)
  3. Get the average score of the Player's 3 most recent games.
  4. Get the Player's top score.

You should not need to implement the class at all in order to write the sample usage, nor to find out what the expected results of (3) and (4) are, though you're free to do so!

You should now have a clear idea of what your methods should look like. If you haven't already done so: implement your class!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!