Comp 110 Drop, Stop, or Roll - P1/P2

In this problem set you will build a fun dice game called Drop, Stop, or Roll.

Points: 400

Parts 1 & 2 Early Deadline: 2/3 at 11:59pm

Parts 1 & 2 Deadline: 2/5 at 11:59pm

Due to the water shortage, we have broken Part 3 into a separate submission due 2/7 at 11:59pm. If you are working on Part 3, please submit here.

!!! PLEASE CHECK THE F.A.Q. SECTION BELOW FOR ANSWERS TO COMMONLY ASKED QUESTIONS

Overview

The game begins by choosing the number of points you are playing to. The game then continues in rounds.

In each round, both players will take a turn. 

In a turn, a player begins by rolling a die. If the die's value is 1, the player's turn is dropped and the player ends with 0 points for the round. If the die's value is between 2 and 6, the player has that many points for the round. The player can then choose to stop their turn and keep their points or roll again. On subsequent rolls, rolling a 1 will cause the player's turn to end with 0 points. Rolling between a 2 and 6 will add that many points to the player's points for the turn. Using the same logic and following steps as above, the player chooses again to stop or roll.

At the end of each round, if either player has exceeded the # of points the game is being played to and there is not a tie, the game is over and that player wins.

IMPORTANT: This problem set is broken into 3 programs. The first two are smaller pieces of the full game. The auto-grader will be ready and posted Monday. We strongly recommend completing each and getting full auto-grader credit for that part before continuing on to the next part. Plan on this problem set to taking a significant amount of time to reach 100% completion on the order of 8 to 14 hours.

Part 0) Roll Demo - We provide a sample program to illustrate how rolling a Die object and reading its value works. You should fully understand this program before continuing.

Part 1) One Turn - This program is a single player's single turn.

Part 2) One Player - This program is a single player version of the full game.

Part 3)Two Player - This program is the complete, two player game.

Frequently Asked Questions

Q: How do I exit a while loop?

A: The while loop condition must evaluate to false. Take a look at RollDemo.java - the boolean “rolling” is assigned to false, and therefore will break out of the while loop the next time the test is evaluated. Also see the example in the following question.

Q: How do I maintain a sum in a loop?

Consider the following example which sums all numbers between 1 and 100:

int i = 1;
int sum = 0;
boolean looping = true;
while (looping) {
  sum = sum + i;
  if (i >= 100) {
    looping = false;
  }
  i = i + 1;
}
console.print(sum);

Q: What is the difference between = and == ?

= is the assignment operator. Some variable is taking the value of another expression.

int x = 7; // The variable x is assigned a value of 7
x = 6; // The variable x is now assigned a value of 6

== is the equality operator. It is used in building expressions and evaluates to a boolean. When we use the equality operator we are asking a question: "True or false: is the left side equal to the right side?"

int x = 7;
x == 7 // true
x == 6 // false

Note: You cannot use == with String values. Please see the question below to test whether two String values are equal to one another.

Q: How do I test if two String values are equal?

With strings, you cannot use the equality operator ==  to test for equality; this operator is only used with primitive values, like int or double. To test equality of two Strings, use the equalsIgnoreCase method:

String a = "a string";
boolean testOne = a.equalsIgnoreCase("another string");
// testOne would be assigned a value of false
boolean testTwo = a.equalsIgnoreCase("a string");
// testTwo would be assigned a value of true

Import the Support Code for PS2

To get started, follow these steps to install the Support Code for PS02.

  1. Start Eclipse
  2. Select and copy the following link to your clipboard:  https://github.com/comp110/17S-PS2-DropStopOrRoll.git
  3. Click the File menu
  4. Select "Import"
  5. Double click "Git" to expand the folder
  6. Double click "Projects from Git"
  7. Double click "Clone URI".
  8. The "URI" field should already have the URI you copied in Step 2 in it. If it does not, paste the URI from step 2 in it. Click next.
  9. Ensure there's a check beside of "master". Click next.
  10. In the "Directory" field, click "Browse". Navigate to your documents folder and select the COMP110 folder. Click next.
  11. Ensure "Import existing Eclipse projects" is selected. Click next.
  12. Ensure the correct Problem Set folder is checked. Click finish.

Roll Demo

Begin by opening RollDemo.java. Read through the main method closely and try to predict exactly what will happen as the program runs.

Next try running the program. Did it behave as you expected?

The main lessons to learn from this small demo are:

1) Notice how the boolean variable named rolling is used as the test condition of the while loop and modified at the end of the while loop. This is a useful pattern you may want to mimic in other parts of this problem set.

2) Notice how the Die object, named die, is rolled using the roll method, and how its value is read using the getValue method. The method documentation for each is as such:

Die's Methods

void roll() - "Rolls" the die, causing its value to be randomized between 1 and 6. This method does not return any value (hence 'void').

int getValue() - Returns the value of the die after its last roll.

Part 1 - One Turn

NOTICE: Please see the F.A.Q. above for answers to commonly asked questions relating to Part 1.

You can download a working version of Part 1 here to try playing. Once downloaded, double click to run and try playing through. (This file is safe to run, despite browser warnings.)

Below are some Example Runs of this program. Notice this program covers a single player's turn within the larger game.

Note that when you are prompted to Roll or Stop, unlike in Roll Demo, here you must prompt the user for a String. You are encouraged to use the following method to compare whether the user typed "roll" or not. The grader will always test with the lowercase word "roll".

boolean equalsIgnoreCase(String other)

Roll a 1 Immediately

Welcome to One Turn Drop, Stop, and Roll
(Alert) Press OK to begin.
You rolled a 1
You DROPPED your points!
Your turn score is 0
Game Over

Roll a 2 and stop

Welcome to One Turn Drop, Stop, and Roll
(Alert) Press OK to begin.
You rolled a 2
Your turn score is 2
(Prompt) Roll or Stop? stop
Your turn score is 2
Game Over

Roll a 2 and 3 and stop

Welcome to One Turn Drop, Stop, and Roll
(Alert) Press OK to begin.
You rolled a 2
Your turn score is 2
(Prompt) Roll or Stop? roll
You rolled a 3
Your turn score is 5
(Prompt) Roll or Stop? stop
Your turn score is 5
Game Over

Roll a 3 and a 1

Welcome to One Turn Drop, Stop, and Roll
(Alert) Press OK to begin.
You rolled a 3
Your turn score is 3
(Prompt) Roll or Stop? roll
You rolled a 1
You DROPPED your points!
Your turn score is 0
Game Over

Roll a 6, 5, 4 and stop

Welcome to One Turn Drop, Stop, and Roll
(Alert) Press OK to begin.
You rolled a 6
Your turn score is 6
(Prompt) Roll or Stop? roll
You rolled a 5
Your turn score is 11
(Prompt) Roll or Stop? roll
You rolled a 4
Your turn score is 15
(Prompt) Roll or Stop? stop
Your turn score is 15
Game Over

--

Part 2 - One Player

NOTICE: Please see the F.A.Q. above for answers to commonly asked questions relating to Part 2.

You can download a working version of Part 2 and try playing. Once downloaded, double click to run and try playing through. (This file is safe to run, despite browser warnings.)

Below are some Example Runs of this program. Notice this program allows a single player to play multiple rounds.

In order to achieve this, you will need to use a while loop inside of a while loop similar to how we have used if-then statements inside of if-then statements. The outer loop will represent rounds of the game while the inner loop will represent a single turn, just like in Part 1. Slow down and try to carefully think through the steps involved in each on paper before writing any code. We encourage you to try drawing a flow chart for this stage similar to how you have seen in lecture slides.

Playing to 2, rolls a winning number first roll, stops.

Welcome to One Player Drop, Stop, or Roll
(Prompt) What are you playing to? 2
Round 1
(Alert) Press OK to begin your turn.
You rolled a 2
Your turn score is 2
(Prompt) Roll or Stop? stop
Total score is 2
You won in 1 rounds with 2 points!

Playing to 3, rolls winning number in first two rolls, stops.

Welcome to One Player Drop, Stop, or Roll
(Prompt) What are you playing to? 3
Round 1
(Alert) Press OK to begin your turn.
You rolled a 2
Your turn score is 2
(Prompt) Roll or Stop? roll
You rolled a 3
Your turn score is 5
(Prompt) Roll or Stop? stop
Total score is 5
You won in 1 rounds with 5 points!

Playing to 3, rolls winning number in first roll of two rounds, stops.

Welcome to One Player Drop, Stop, or Roll
(Prompt) What are you playing to? 3
Round 1
(Alert) Press OK to begin your turn.
You rolled a 2
Your turn score is 2
(Prompt) Roll or Stop? stop
Total score is 2
Round 2
(Alert) Press OK to begin your turn.
You rolled a 3
Your turn score is 3
(Prompt) Roll or Stop? stop
Total score is 5
You won in 2 rounds with 5 points!

Playing to 5, rolls, drops first turn, wins in first roll of second round, stops.

Welcome to One Player Drop, Stop, or Roll
(Prompt) What are you playing to? 5
Round 1
(Alert) Press OK to begin your turn.
You rolled a 4
Your turn score is 4
(Prompt) Roll or Stop? roll
You rolled a 1
You DROPPED this turn's points!
Total score is 0
Round 2
(Alert) Press OK to begin your turn.
You rolled a 6
Your turn score is 6
(Prompt) Roll or Stop? stop
Total score is 6
You won in 2 rounds with 6 points!

--

Part 3 - Two Player Drop, Stop, or Roll

You can download a working version of Part 3 and try playing. Once downloaded, double click to run and try playing through. (This file is safe to run, despite browser warnings.)

Below are some Example Runs of this program. Notice this program allows a two players to play a complete game over multiple rounds. Also notice that ties are not allowed to end the game.

For this part, you will likely have three while loops. One that loops once per round and two loops inside of the outer loop, one following the other, representing each player's turn. Again, it is recommended you try to visualize this by drawing it out or writing out the rules needed to explain the game before beginning to write code.

Two players playing to 5. Player 1 wins in first round.

Welcome to Drop, Stop, or Roll
(Prompt) What are you playing to? 5
Round 1
The game is tied 0 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 5
Player 1: Your turn score is 5
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 5
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 1
Player 2: You DROPPED this turn's points!
Player 2: Total score is 0
Player 1 wins 5 to 0

Two players playing to 5. Player 2 wins in first round.

Welcome to Drop, Stop, or Roll
(Prompt) What are you playing to? 3
Round 1
The game is tied 0 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 2
Player 1: Your turn score is 2
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 2
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 6
Player 2: Your turn score is 6
(Prompt) Player 2: Roll or Stop? stop
Player 2: Total score is 6
Player 2 wins 6 to 2

Two players playing to 5. First round ends in a 6-6 tie. Player 1 wins second round.

Welcome to Drop, Stop, or Roll
(Prompt) What are you playing to? 5
Round 1
The game is tied 0 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 6
Player 1: Your turn score is 6
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 6
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 6
Player 2: Your turn score is 6
(Prompt) Player 2: Roll or Stop? stop
Player 2: Total score is 6
Round 2
The game is tied 6 to 6
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 2
Player 1: Your turn score is 2
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 8
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 1
Player 2: You DROPPED this turn's points!
Player 2: Total score is 6
Player 1 wins 8 to 6

Two players playing to 10. Player 2 wins in second round.

Welcome to Drop, Stop, or Roll
(Prompt) What are you playing to? 10
Round 1
The game is tied 0 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 6
Player 1: Your turn score is 6
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 6
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 5
Player 2: Your turn score is 5
(Prompt) Player 2: Roll or Stop? stop
Player 2: Total score is 5
Round 2
Player 1 leads 6 to 5
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 1
Player 1: You DROPPED this turn's points!
Player 1: Total score is 6
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 5
Player 2: Your turn score is 5
(Prompt) Player 2: Roll or Stop? stop
Player 2: Total score is 10
Player 2 wins 10 to 6

Two players playing to 7. Player 1 wins in a miraculous come back.

Welcome to Drop, Stop, or Roll
(Prompt) What are you playing to? 7
Round 1
The game is tied 0 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 1
Player 1: You DROPPED this turn's points!
Player 1: Total score is 0
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 6
Player 2: Your turn score is 6
(Prompt) Player 2: Roll or Stop? stop
Player 2: Total score is 6
Round 2
Player 2 leads 6 to 0
(Alert) Player 1: Press OK to begin your turn.
Player 1: You rolled a 2
Player 1: Your turn score is 2
(Prompt) Player 1: Roll or Stop? roll
Player 1: You rolled a 2
Player 1: Your turn score is 4
(Prompt) Player 1: Roll or Stop? roll
Player 1: You rolled a 2
Player 1: Your turn score is 6
(Prompt) Player 1: Roll or Stop? roll
Player 1: You rolled a 2
Player 1: Your turn score is 8
(Prompt) Player 1: Roll or Stop? stop
Player 1: Total score is 8
(Alert) Player 2: Press OK to begin your turn.
Player 2: You rolled a 1
Player 2: You DROPPED this turn's points!
Player 2: Total score is 6
Player 1 wins 8 to 6

--

Submissions

Grading is now open! The grading report should will give you the output log of failed tests and tell you what we *expected* the next line of output to be in your program versus what your program *actually* responded with.