CS21 Lab 7: Big Pig

Design is due Saturday, October 29, before midnight

Full Implementation is due Saturday, November 5, before midnight

Goals

  • practice using Top-Down Design

  • write a complex program, with multiple functions, from scratch

  • practice using the random library

  • practice validating user input

Notes

Please read through the entire lab before starting!

This is a two-part lab, split over two weeks. For the first week you will work with a partner and focus on using top-down design to create the overall structure for the program. Once your proposed structure has been reviewed and approved by your professor (either your class professor or your lab professor), you will each individually use bottom-up implementation to complete the full program.

You have two weeks to complete the full program. The first week is for the design, and the second for the full program. Your initial top-down design is due this Saturday (Oct 29) and the full implementation the following week (Nov 5). We highly recommended that you submit your top-down design as soon as possible so that we have time to give you feedback before you begin your full implementation. If you submit your design on the last day at midnight, it might take us a few days to get to it and send you comments.

The list of partnerships is on here.

Please see the following guidelines for lab partnerships for information about how to best collaborate with a partner on lab assignments.

Big Pig

These rules are adapted from the rules found on Wikipedia’s Pig_(dice_game) page.

Big Pig is a dice game where players take turns rolling a pair of dice as many times as they wish. The values on the dice determine the number of points awarded (see below). These points are added to a running total of points earned that turn. After each roll of the dice, the player may choose to end their turn ("hold"). If they hold, their points earned that turn are added to their game score. But if they roll too many times they run the risk of becoming a "Big Pig": their turn immediately ends and they get no points at all for their turn. Players become a "Big Pig" if they roll a 1 on one die but not the other.

The rules are as follows. The player may choose to roll two dice or hold:

  1. If the player rolls a 1 on one die and anything except a 1 on the other die, they score nothing and it becomes the next player’s turn. When this happens it’s called "Big Pig".

  2. If the player rolls two different values on their dice, the sum of the dice is added to their current round total and the player’s turn continues. For example, if one die shows a 6 and the other die shows a 2, the (6 + 2) = 8 is added to their current round total.

  3. If the player rolls a 1 on both dice, 25 points is added to their current round total and the player’s turn continues.

  4. If the player rolls the same value on both dice (and that value is greater than 1), twice the sum of the dice is added to their current round total and the player’s turn continues. For example, if both dice show the number 5, then 2 * (5 + 5) = 20 is added to their current round total.

  5. If a player chooses to "hold", their current round total is added to their game score, and it becomes the next player’s turn.

If at the end of both player’s turns, at least one player has 100 points or more, the game ends, and the player with the highest score wins.

Demonstration game

Let’s take a look at a demonstration game to get a better sense of how the game works. The human is going to play against the computer. The computer has special rules that determine how it plays, but we will see that later. For now, just follow along.

The game score starts off as HUMAN 0, COMPUTER 0. The human always goes first.

The human’s turn begins. So far, they’ve earned 0 points this turn. They roll the dice and get a 5 on one die and a 6 on the other die: [5, 6]. According to rule 2 above, the sum of the dice (5 + 6) = 11 is added to their current round score. Now their current round score is 11. The human decides they want to roll again and rolls [3, 4]. Once again, rule 2 above says that this roll is worth (3 + 4) = 7, so their current round score is (11 + 7) = 18. The human decides to hold. According to rule 5 above, their current round total (18) is added to their game score (0), so now the human has (0 + 18) = 18 points.

The current score is HUMAN 18, COMPUTER 0.

The computer’s turn begins. So far, they’ve earned 0 points this turn. They roll the dice and get a 4 on one die and a 4 on the other die: [4, 4]. According to rule 4 above, twice the sum of the dice 2*(4 + 4) = 16 is added to their current round score. Now their current round score is 16. The computer will roll again and rolls [1, 1]. According to rule 3 above, 25 is added to their current round score, so their current round score is (16 + 25) = 41. The computer will hold. The game score is updated so now the computer has (0 + 41) total points.

The current score is HUMAN 18, COMPUTER 41.

The human has some catching up to do! So far, they’ve earned 0 points this turn. They roll and get [6, 6], and their current round score is now 2*(6+6) = 24. They roll again and get [4, 5], so now their round score is increased by (4 + 5) = 9, so their current round score is (24 + 9) = 33. The human could hold now, but they’re feeling lucky and roll again. This time, they roll and get [4, 1]. Oh no! BIG PIG! According to rule 1 above, they won’t earn any points this turn and their turn immediately ends.

The current score is HUMAN 18, COMPUTER 41.

The computer’s turn begins. The computer rolls [3, 2] so their current round score is now 5. The computer will roll again and gets [6, 2] so their current round score is now (5 + 8) = 13. The computer will roll again and gets [1, 3]. BIG PIG! The computer won’t earn any points this turn and their turn immediately ends.

The current score is HUMAN 18, COMPUTER 41.

The game continues with each player getting a turn until, after the computer goes, either the human or the computer has 100 points. At that point, whichever player has more points wins.

Game Requirements

You have a lot of freedom to design the game how you want to look, but here are our requirements:

  • The user always goes first and the computer always gets a final turn to try to win.

  • The only allowed user input is roll or hold, as well as r or h for short.

  • A player must get to 100 to win. If both the user and the computer get to 100, the higher score wins.

  • The computer follows strict rules on when it will roll or hold.

    • If the player has fewer than 100 points:

      • The computer will choose roll if their current round score is less than 20 points.

      • Otherwise the computer will hold

    • If the player has 100 or more points:

      • The computer will choose roll if the points earned so far will not let it win,

      • Otherwise the computer will hold

Example program runs

Here are some more examples of our program running so you can see more examples of how the game works.

Top-Down Design requirements

You and your partner should complete your top-down design (TDD), submit it, and obtain feedback on it before beginning the full implementation. Special procedures for this lab:

  • create design-pig.py first.

  • after you have a working design (see below), you and your partner should both run handin21 to turn it in! After running handin21, send an email to tdd@cs.swarthmore.edu, letting us know your design is done. We will take a look at each design and send you comments (usually within a day or two). If the design is incomplete or insufficient, you may be asked to submit a second design.

  • After you have the design and have heard back from us, copy the file to pig.py (ex. cp design-pig.py pig.py, or use "Save As" in Visual Studio Code) and implement the full program. Leave design-pig.py as it is. Work by yourself for the implementation portion of this lab.

  • Please ensure your design meets the following requirements before submitting:

  • main() should be completely written, and should perform high-level steps without focusing on details.

  • main() should call the functions you create in your design, in the appropriate order, and with arguments that match parameters. Return values should be saved in main(), even if you don’t do anything with them yet.

  • All functions should be stubbed out with parameters, a block comment, and a return statement. They don’t need to do anything yet, except possibly call other functions.

  • If your function is supposed to return something, you should return a dummy value of the appropriate type (e.g. return 0 for an int, [1,2,3] for a list, …​).

  • Your design should contain several functions. Each function should be function worthy (i.e., not a trivial task). Each function should also demonstrate encapsulation (one clearly defined purpose)

  • The design should run without syntax errors. (even if it doesn’t do much)

Here is a simple example of Top-Down Design.

Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-07.txt file in your cs21/labs/07 directory and answer the questions in that file.

Once you’re done with that, you should run handin21 again.

Submitting lab assignments

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.

Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

First quit any applications you are running, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!