Data Structures - Lab 02
Tuesday, September 1st
Yesterday we started looking at how classes are defined in Python. We didn't quite make it far enough to justify having you create your own classes completely from scratch. Instead, you will work with some already written code to describe several classes and then modify this code to add to the class's functionality.
If you have not already done so, please take the time to read section 8.2 in your textbook.
After you understand the idea behind the code:
>>> die1 = Die() >>> print die1.getValue() >>> die1.roll() >>> print die1.getValue() >>> die1.roll() >>> print die1.getValue() >>> print die1
>>> playOneGame() >>> playOneGame() >>> playManyGames()
While I understand why your author put the code for playOneGame() and playManyGames() in the same file/module as the code for the player class, I disagree with that design choice.
Separate these two different ideas into two different files by:
>>> playOneGame() >>> playOneGame() >>> playManyGames()
You should get generalized results similar to what you saw in Part B.
[SIG1] When you get this all working properly demonstrate this code for me and obtain my signature.
Right now your code will only allow a Die to have six sides. If you have played a lot of board games you know that dice can come with any number of sides (well, ok, the smallest die you can actually create has four sides).
Modify the code from part A so that
To test this, load the class and create a die with 8 sides:
>>> die1 = Die(8)
Next, keep running the following lines of code until you get a value of 8 to confirm that you have things set up properly. While this may take a little while due to random luck it shouldn't take longer than 12-16 rolls
>>> die1.roll()
>>> die1.getValue()
Now that you have changed the way that Die works, modify the Player class so that it uses this slightly different form of Die. This is a simple change, but you will have to think about what changes you just made.
Finally, modify your Player so that he is playing craps with a die of six sides and a die of eight sides, but leave the rest of the game/rules the same.
Run playManyGames() again to see what effect this has on the players chances of winning versus losing.
[SIG2] When you get this all working properly demonstrate this code for me and obtain my signature.
Do not forget to “log off” of the machine you are working on before you leave.