Introduction to Computer Science II
Homework 1
Due by 11:50am on Tuesday, January 11
Reading
Read chapter 7 and section 8.1, the week 1 PPT, and the first 9
slides of the week 2 PPT.
Problems
Solve the following by implementing the corresponding functions in homework1.py. When
done, submit that file through D2L.
1. [Jan 3 Lab] Craps is a dice
game played at many casinos. A player rolls a pair of normal
six-sided dice. If the initial roll sums up to 2, 3, or 12, the
player loses. If the roll is 7 or 11, the player wins. Any other
initial roll causes the player to "roll for point" which means that
the player keeps rolling the dice until either rolling a 7 or
re-rolling the value of the initial roll. If the player re-rolls the
initial value before rolling a 7, it's a win for the player. Rolling
a 7 first is a loss. Write a program that will simulate multiple
games of craps and return the fraction of games that the player won.
You should organize your program as follows:
- Write a function forPoint(roll) that takes as input
an initial roll (i.e. an integer between 2 and 12) and
repeatedly generates rolls of a pair of dice until either the
value of the roll is 7 (in which case 0 is returned) or the
value of the roll is the initial roll (in which case 1 is
returned).
- Write a function craps() that takes no argument and
simulates one game of craps. The program will first roll a pair
of dice. If the value (sum of two dice) of the roll
obtained is 7 or 11, then 1 is returned by craps(). If
the value obtained is 2, 3 or 12, then 0 is returned by craps().
For all other roll values, forPoint(value) is called
and the value returned by it is returned by craps().
In other words, craps() returns 1 if the player won, 0
otherwise.
- Write a function testCraps(n) that takeas a positive
integer n as input, simulates n games of
craps and returns the fraction of games the player won.
Usage:
>>> forPoint(2)
0
>>> forPoint(2)
0
>>> forPoint(2)
1
>>> craps()
1
>>> craps()
0
>>> testCraps(10000)
0.4806
>>> testCraps(10000)
0.4978
>>> testCraps(10000)
0.4901
2. You will write the function baccarat()
that simulates a game of baccarat, another classic card game played
in casinos. The game is similar to blackjack in that the player
plays agains the house.
Cards in baccarat have values as follows: all cards numbered 2
through 9 are counted at face value, cards 10, J, Q, and K have
value 10 and Aces count as 1. The total value of a hand is obtained
by adding up the values of the cards in the hand modulo 10. In other
words, if the cards in the hand total a two-digit number, only the
last digit counts. Example: a hand with two cards having rank 5 and
8 has value 3 since 5 + 8 = 13. The goal of the game is to have a
hand whose total value
is closest to 9. Ties are possible. Here is how the game is played:
- At the beggining, two cards are dealt from the deck to the
player and two cards are dealt to the house. The third card
drawing rules for the player and the house are entirely rule
based and dictate the whole game.
- If either the player or the house has a hand with total value
8 or 9 (called a "natural") no one draws a third card and the
game is decided based on the current hand values (example 1
below). The below rules are applied only if neither the player
or the house has a natural.
- If the player has a hand with total value 6 or 7, the player
stands (i.e., does not draw a third card); in this case the
house draws a third card only if its current hand has total
value that is one of 0,1,2,3,4, or 5. The game can now be
decided (example 2 below which results in a tie). The below
rules are applied only if the player has a hand whose total
value is 0,1,2,3,4, or 5.
- The player draws a third card. The house will draw a third
card if one of the following is true:
- The house has a hand with total value 0,1, or 2 (example 4
below).
- The house has a hand with total value 3 and the third card
just drawn by the player has a value that is not 8 (i.e. its
value is one of 1,2,3,4,5,6,7,9,10). (Example 3 below shows
the case when the house does not draw a third card because
the player's third card has a value of 8.)
- The house has a hand with total value 4 and the third card
just drawn by the player has a value that is one of
2,3,4,5,6,7 (example 5 below).
- The house has a hand with total value 5 and the third card
just drawn by the player has a value that is one of 4,5,6,7.
- The house has a hand with total value 6 and the third card
just drawn by the player has a value that is one of 6,7.
The game can now be decided.
Note that I am providing you with helper functions newDeck,
shuffle, dealCard, value, total,
and compareHands. Use them.
Usage:
>>> baccarat()
House: 9 ♣ J ♠
Player: A ♡ 2 ♡
House wins
>>> baccarat()
House: 8 ♣ 8 ♠
Player: 3 ♡ 3 ♢
Tie
>>> baccarat()
House: K ♠ 3 ♣
Player: 6 ♣ 8 ♣
Player draws 8 ♡
House wins
>>> baccarat()
House: 10 ♠ J ♢
Player: 10 ♢ 4 ♠
Player draws J ♣
House draws 6 ♢
House wins
>>> baccarat()
House: 9 ♠ 5 ♡
Player: 7 ♢ 4 ♢
Player draws 4 ♣
House draws J ♡
Player wins
3. [Jan 10 Lab] You will augment
the Point
class discussed in class by adding and implementing the following
methods:
- method distance(other) that takes another object of
type point and returns the distance between other and the point
invoking the method.
- methods up(), down(), left(),
and right() that move the point by one unit in the
specified direction. Your implementation must use the existing
method move rather than changing the instance
variables x and y directly.
- method equals(other) that takes another object of
type point and returns True if other and the point invoking the
method are equal (i.e., they have the same coordinates) and
False otherwise.
4. [Jan 10 Lab] Implement class Triangle
that models a triangle that is specified using three points in the
plane. The class uses the Point class above and it should
support the following methods:
- method setVertices(p,q,r) that is used to specify
the three vertices of the tirangle. The method takes as input
three objects of type Point.
- method area() that computes and returns the area of
the triangle.
- method perimeter() that computes and returns the
perimeter of the triangle. Your implementation should use the Point
class method distance that you implemented in Problem
3.a.
Usage:
>>> p = Point()
>>> p.setx(0)
>>> p.sety(0)
>>> q = Point()
>>> q.setx(1)
>>> q.sety(1)
>>> r = Point()
>>> r.setx(2)
>>> r.sety(0)
>>> t = Triangle()
>>> t.setVertices(p,q,r)
>>> t.area()
1.0
>>> t.perimeter()
4.82842712474619