Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Very short description: Write a Python program that allows the user to play the

ID: 3805228 • Letter: V

Question

Very short description: Write a Python program that allows the user to play the two dice games Fifty and Pig. More detailed description: Your computer program should allow 2 or more players to play the dice games Fifty and Pig. The rules for each game are below. The program should continue executing until the user chooses to quit playing. After each game, the user should be allowed to choose which game to play next (or choose to quit). You must implement 2 versions of the program. • One version should use imperative constructs only, and must take advantage of procedural abstraction. In other words, your program must be broken down into an appropriate number of functions, instead of just being in one or two large functions. • The second version should use object-oriented constructs. Therefore, your program must be organized using classes. You need to demonstrate inheritance with your program, as well as other object-oriented techniques. The rules for Fifty are as follows: Goal: The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles. Play: A turn consists of a player rolling a pair of dice (with the goal of rolling doubles), and scoring the roll as described below. Play continues with each player taking one roll per turn. The first player to score 50 or more points is declared the winner. Scoring: All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points. Double 3s wipe out the player’s entire score, and the player must start again at 0. Non-double rolls are 0 points. Note: As you are developing and testing your program, you might want to make the winning score something smaller than 50, such as 10. It can take a while to reach 50 points. Just be sure to change it back to 50 before turning in the program. Alternatively, you could allow the user to set the winning score. The rules for Pig are as follows: Goal: The goal of Pig is to be the first player to reach 100 points. You get points by rolling a single die multiple times and adding the value on each roll of the die to your current score. Play: The first player rolls the die as many times as they want. The value of each throw is added onto the score until the player decides to end his turn and passes the die to the next player. Play continues until one player reaches 100. Scoring: The value of each throw is added to the current player’s score. If the player rolls a 1, the player’s score goes back to 0, and their turn ends. At one extreme, any player who gets a 1 on the first roll is immediately out. At the other extreme, the first player could theoretically reach the winning score on the first turn, as long as they don’t roll a 1. If the player succeeds, the game ends there. Note: As you are developing and testing your program, you might want to make the winning score something smaller than 100. Just be sure to change it back to 100 before turning in the program. Alternatively, you may allow the user to set the winning score. ! Program Requirements: Your program must do the following • Print an introductory message at the beginning of the program to tell the user what the program does. • Allow the user to play games of “Fifty” with another player. • Allow the user to play games of “Pig” with another player. • When a player wins, the program should print a congratulatory message that includes which player won and the final score. • Be sure any user interface elements are clear for the user. • Your output should be clearly labeled and neatly formatted. Be sure to print helpful information, such as allowing the user to view the game rules before a game, and printing updated scores after each roll. • Use good variable names and appropriate comments throughout your program. Make use of whitespace (blank lines, indenting, etc.) in order to make your program readable. • You must have a comment block at the beginning of your program that lists your name, the date, the class number and section and a description of what your program does. You must submit a printout of your program in addition to an electronic copy. • The printout must be turned in during class. • The electronic copy should be submitted via Canvas. • Please review the rules regarding late programs as outlined in the syllabus • Only turn in your .py files. Your files should be named dicegames.py (the imperative version) and dicegames_oo.py (the object-oriented version).

Explanation / Answer

Implement the is_prime function described in Chapter 7, Exercise 10. Copy the following into a file named prime.py and fill in the body of is_prime:

import sys
def test(did_pass):
    """ Print the result of a test. """
    linenum = sys._getframe(1).f_lineno   # Get the caller's line number.
    if did_pass:
        msg = "Test at line {0} ok.".format(linenum)
    else:
        msg = ("Test at line {0} FAILED.".format(linenum))
    print(msg)
    
def is_prime(n):
    """
    For any integer n > 1, return True if n is a prime number and False otherwise.
    """
    pass // Replace this with your code

test(is_prime(2))
test(is_prime(3))
test(not is_prime(4))
test(is_prime(11))
test(not is_prime(35))
test(is_prime(19911121))

Create a function named roll_die. It should receive one parameter, an integer that specifies the number of sides on the die. It should generate a random integer that is at least 1 and no more than the number of sides. (For example, a six-sided die can only produce the integers 1 through 6, inclusive.) Then it should return the randomly generated number.

Hint: Look at the online python documentation for the random module to help you decide which random number function to call in your roll_die function.

After you create your roll_die function, test it to make sure it is working correctly. In particular, if you roll the die many times, do all of the 6 numbers eventually show up? How would you test it?

Taking a turn

Create a function named take_turn to let a single player to take a single turn. The function should receive one parameter, an integer that identifies the player number. It should return the number of turn points acquired by the player.

Below is a pseudo-code description of the algorithm you should use to implement the take_turn function.

As you create your take_turn function, you should test it to be sure it behaves correctly. Track the turn points manually to be sure that the function adds up points correctly.

Below is an example output from the take_turn function for player 1.

==================================================
Player 1 press enter to begin your turn.

You rolled a 4
Your turn points are now 4 .
Continue rolling (0=no,1=yes)? 1

You rolled a 3
Your turn points are now 7 .
Continue rolling (0=no,1=yes)? 1

You rolled a 1
Turn over
==================================================

This is an example for player 2.

==================================================
Player 2 press enter to begin your turn.

You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1

You rolled a 3
Your turn points are now 6 .
Continue rolling (0=no,1=yes)? 1

You rolled a 5
Your turn points are now 11 .
Continue rolling (0=no,1=yes)? 0

Turn over
==================================================

At the bottom of pig_turn.py, add the following test code:

for i in range(10):
    print(roll_die(4))

print()

take_turn(1)
take_turn(2)

Run this program to make sure it behaves as expected. Fix any problems. Since you are not done with the game yet, you do not need to do an Above & Beyond component for this part of the assignment.

When it is working, submit the file pig_turn.py using the turnin form.