Introduction. This is our first assignment using the object-oriented
approach to programming, with Classes and Objects as the centerpieces of the
program.
DebuggingExercises: Programs DebugOne.py, DebugTwo.py and DebugThree.py all have simple class definitions that make one of the common programming mistakes for this material.
DebugOne.py defines a Person class. The instance variables for this class are age and name; both are supplied as arguments to the constructor. There is a method setAge(), which changes the value of the instance variable age. All of this seems quite straightforward, but if you run it you will get an error message that says "setAge() takes one argument but two are given" The method setAge() is defined with one argument and it is called in the line
x.setAge(24).
How does this have two arguments?
DebugTwo.py defines a Student class, with instance variables for name and major. If you run this you get an error message saying that "Student instance has no attribute 'major'." What is going on here; doesn't this set up the major in the constructor?
DebugThree.py defines a Circle class, with instance variables for the center and the radius of a circle. It has a Print() method for printing out its information. It also has a method grow() that increases the circle's radius and calls the Print() method to print the new information. That might not be the way you would set this up, but it ought to be possible for grow() to call Print(). When I do that I get an error message: "Global name 'Print' is not defined." What is going on here? It is obvious that Print is indeed defined as the next method.
Programming Exercise. This week we will write a simple game simulator. Two players take turns rolling a pair of dice until one of them gets a total greater than a preset limit. The rules for a turn are simple:
Program diceGame.py has this partically completed. There are three classes to implement for this program: Dice (which simulates a pair of dice), Player (which remembers the name and point total of one of the players and also similates one turn for that player), and Game (which simulates a whole game between two players). The Game class is completely (and correctly) implemented; don't change it. The other two classes are partially written. Your job is to complete these two classes so the program works.
The program involves one new element -- we need some randomness to simulate dice. There is a Python module called random. If you import it, by putting the line
from random import *
at the top of the program, you have access to a number of functions for working with random numbers. We will use one called randint(). This takes two arguments, say A and B, and returns a random integer between A and B. To simulate a standard 6-sided die, you just call
randint(1, 6)
Method roll() for the Dice class gets two random numbers and saves them in the instance variable values -- this is a list of two elements. Note that this method doesn't return or print anything; it just saves two random numbers. To roll the dice you first call the roll() method, and then look at the values[0] and values[1] numbers to see the results of the roll.
There are two incomplete methods for class Dice. Method doubles() looks at values[0] and values[1]. If they are the same it returns True; if not it returns False. Method total() is supposed to return the sum of values[0] and values[1]; that is the sum of the numbers on the dice.
Class Player only has two methods: a constructor and method turn(). The constructor should be easy -- it just saves the player's name in an instance variable and start the player's total at 0. It also creates a dummy instance variable called "next" to indicate which player's turn is next. This is just a placeholder in the constructor; variable next gets its values after several objects of this class are constructor.
The other method of class Player is turn(), which represents one player's turn with the dice. Remember that in this game a player keeps rolling the dice until he or she does not get a double. So the turn() method consists of a loop. In this loop you roll the dice, and add their total onto the player's total. If you have a roll which is not a double, break out of the loop. The method doesn't need to return anything, but it should print out the results of the roll each time so that the user can see what is happening.
Here is the output of one such game. This has two players "bob" and "fred", and a maximum of 20 points.
Enter a name for the first player: bob
Enter a name for the second player: fred
Enter the goal for the game: 20Round 1
bob rolls a 2 for a total of 2
Doubles!!!
bob rolls a 9 for a total of 11
fred rolls a 7 for a total of 7
Round 2
bob rolls a 7 for a total of 18
fred rolls a 9 for a total of 16
Round 3
bob rolls a 3 for a total of 21
bob wins!!