APS105 Lab 2
- The course material necessary for completing this lab is found in
your lecture notes and in the Carter text through the end of chapter 4.
- Use only the programming techniques presented in the first four chapters of the text, in lecture, and in this handout.
- Begin each program with a comment giving your name, student number, and a brief description of what the program does.
- In sample outputs, input from the user is given in a bold font.
- You will be marked not only on correctness, but also on style (e.g. indentation, variable names, commenting, code clarity).
- You must use the
submitaps105s
command to electronically submit your solution. The lab is due on Saturday, February 13 at 11:59 PM. (Extension: Wednesday, February 17 at 11:59 PM.)
Objective
The objective of this lab is to give you practice writing C programs that use conditionals and loops.
Bogart
For this lab, you will implement the dice game Bogart.
Game Description
Note: The only difference between our version and the version described on the above Bogart page is that we will restrict the game to two players. (We also do not consider the Gambling Variant.) Below is my description of the game.
The game makes use of a pot of chips, initially with zero chips. It also makes use of regular dice with six sides. We begin by randomly choosing player 1 or player 2 to start the game. That initial player continues until their turn is over, at which point it is the other player's turn; when that player's turn is over, we return to the initial player again, and so on. The following happens on the current player's turn:
- Step 1: roll one die, and put one chip into the pot.
- Step 2: if a "1" is rolled, the current player's turn is over. The other player begins at step 1, rolling one die.
- Step 3: the current player decides whether to end their turn (option A), or roll again (option B).
- A:
- The current player decides to end their turn. They collect all of the chips in the pot, and their turn is over. Continue at step 7.
- B:
- The current player decides to roll again. They roll one more die than they rolled last time, and one chip is added to the pot for each die that is rolled. For example, if they rolled three dice last time, they would roll four dice now and four chips would be added to the pot.
- If at least one "1" was rolled on these dice, continue at step 4.
- Otherwise, no "1" was rolled. Continue at step 5.
- Step 4: the current player's turn is over. The other player begins at step 1, rolling one die.
- Step 5: If the current player just rolled five dice (none of which was a "1"), that player wins. The game is over. (It does not matter how many chips they have collected.)
- Step 6: otherwise, continue at step 3.
- Step 7: If the current player has collected a total of at least 30 chips, they win, and the game is over. Otherwise, the other player begins at step 1, rolling one die.
C Implementation
You will write a C implementation of this game in file bogart.c
. Begin by printing a welcome message and randomly choosing which player (1 or 2) will have the first turn. Then, for each player's turn:
- Print a message indicating how many chips each player has
- Print a message indicating how many chips are currently in the pot
- Print a message indicating which player's turn it is
- Print a message indicating that enter should be pressed to roll, and wait for enter to be pressed
- Roll one die, and print a message indicating what was rolled
- Add 1 chip to the pot
- As long as the player's turn is not over:
- Print a message indicating the number of chips currently in the pot
- Ask the player whether they want to roll again. They must type
y
or n
; repeatedly ask them until they enter one of these valid responses
- If they answer
y
, roll the required number of dice, indicating each roll with a message, and adding chips to the pot
- If they answer
n
, collect the pot and switch to the other player's turn
- When the game is over, print a message indicating which player has won
Sample Transcripts
Here is a sample transcript of a complete game. You should use this not only for another explication of the game, but also as the source for the strings you should use in your printf
statements. You should use exactly the same prompts and output messages as are used in the transcript. Do not add any other prompts, and do not ask for any other input.
Here is another sample transcript. In this one, the game is won by successfully rolling five dice without getting any 1's.
Implementation Tips
Several aspects of this program require some "tricks" not discussed in class.
- Include the file
time.h
. Then, right after you declare your variables at the top of main
, include the line:
srand(time(0));
Without this, your "random" numbers returned from rand
will be the same every time you run your program.
- When you prompt the user with, "Press enter to roll.", use the following code to wait until the enter key is pressed:
scanf("%*[^\n]"); getchar();
This uses a new feature of scanf
(called scansets) to read everything except for the newline character. The getchar
reads that newline.
- When you prompt the user with, "Roll again (y/n)? ", you'll want to read one character with
scanf
. After you do that, though, a newline is going to remain in the input buffer, which will mess up future scanf
s. To remedy this, add a getchar();
after the scanf
.
- To help you get this input/output under control, here is tricks.c, showing how to (A) wait until the user presses enter, and (B) ask for one character from the user.
Submitting your Lab
When you have completed the lab, use the command submitaps105s 2 bogart.c
to submit your file. Make sure you name your file exactly as stated (including lower/upper case letters). You may check the status of your submission using the command submitaps105s -l 2
, where -l
is a hyphen followed by the letter 'ell'.