Dealing with Objects
- Get more practice making and using classes and objects.
For this lab, you will be modifying an object-oriented (OO)
program that simulates playing a simple card game. Start with the
- Create a PyCharm project. Make sure you use your name somewhere in the
project name (like CassLab7).
- Download some starter code: source files.
- Don't try to run the project yet; it won't work because there are
missing parts, which you will provide.
Step 0: Have a Look Around
Have a look at the provided code, which includes a main module
and four modules that define classes that main uses.
As you look at the classes, focus only on the public methods and
the constructors. There are lots of private helper methods, but
in general, when you read someone's code to try to understand what it
does, or in an attempt to modify it, you should start with the public
methods. Get in that habit here.
The provided code provides the following four classes:
- GameSimulator [src]
- Plays a game between two players repeatedly.
- Game [src]
- Plays a single game at a time. This is where the rules of the game are
encoded. See rules.txt for an English
description of the rules.
- Player [src]
- Models a player and the player's hand.
- Deck [src]
- Models a deck of cards.
Step 1: Answer some questions
Add a text file called answers.txt to your project and in that
file, answer the following questions:
- What are the instance variables of the Player class? Indicate the
names and types. Hint: after the constructor is finished, all the
instance variables have values.
- What are the instance variables of the Deck class? Indicate the
names and types.
- What are the instance variables of the Game class? Indicate the
names and types.
- Is it possible to construct a Player without giving a name to the
player? Why not?
- Is it possible to change the name of a Player once the player is
created? Why not?
Step 2: Write the Card class
Notice that the Deck class assumes a Card class.
Write a simple card class (in a new module called card):
- Write the constructor. You can tell by how the Deck is
constructed what parameters the Card constructor should
- Write the getters. You can tell by how Game uses
the Card class what getter methods you need. In
particular, look at Game.__matches_table().
Step 3: Write some methods
In GameSimulator, the methods __increment_count()
and get_num_wins() are supposed to increment and get,
respectively, the number of wins by a given player in the game. You'll
write these methods:
- Add an instance variable to GameSimulator that can keep
track of the number of wins for each player. Hint: you can use a list
or a dictionary.
- Write get_num_wins() so that it accesses the new instance
variable added above.
- Write __increment_count() so that it updates the new instance
How to turn in this lab
Before turning in any program in this class, remember this mantra:
Just because it works doesn't mean it's good.
Part of your grade will also come from things like how understandable
and readable your code is. You will also be graded on the neatness,
presentation, and style of your program code.
For all labs, turn in only an electronic version. Compress the
project and email the tarball or zip file to me at firstname.lastname@example.org.
Ask for help if you're having problems!