CSCI 2311, Spring 2013 Programming Assignment 5 The program is due Sunday, March 3 by midnight. Overview of Assignment Begin this assignment by first creating a new Java Project called Assignment 5.There is only one part to this assignment. From now on, we will start including comments in our code. For this assignment, the only comment you must have is a header comment at the top of your class that has the following format: /* * Author: <Your Name Here> * Assignment: <Assignment Number> * Due Date: <Date Assignment is Due> * * Purpose: * * The purpose of this assignment is to... */ You should also include comments in your code where ever you feel appropriate to help clarify variables and give meaning to sections of code. Be certain to follow the naming conventions we discussed in class when naming your identifiers. Also, don t assume your assignment works correctly just because it matches my example output. Try your program out with several different values to be sure it works. IMPORTANT: Starting with this assignment and all future assignments, point deductions will occur when failing to properly name variables, poor code formatting, forgetting to comment, failing to follow conventions, etc. This is the last assignment I will remind students of this, you are expected to remember it for future assignments.
How to Play Craps To play craps (a simple version of it anyway), you need two dice. The total value of the dice is used to determine whether a roll is a win or a loss. The game starts when one person, the shooter, places a bet and then begins with an initial roll of the dice. If the two dice total 7 or 11, it is a winning roll. If the two dice total, 2, 3 or 12, it is a losing roll. If the two dice total 4, 5, 6, 8, 9, or 10, the shooter continues to roll until: 1. If the two dice equal the total of the original roll, it is a winning roll 2. If the two dice total 7, it is a losing roll 3. If the two dice total anything else, rolls continue until part 1 or 2 occur Bets are paid out at a ratio of 1:1; for example, a winning bet of $5.00 results in a gain of $5.00 and a losing bet of $5.00 results in a loss of $5.00. Play continues until the shooter decides to stop betting or runs out of money. As an example, consider the following scenario (assume the user starts with $50.00): The shooter bets $5.00 and on the first roll, gets a 2 and a 5 for a total of 7. This is a winning roll and now the shooter has $55.00. The shooter bets $10.00 and on the first roll, gets a 6 and a 6 for a total of 12. This is a losing roll and now the shooter has $45.00. The shooter bets $20.00 and on the first roll, gets a 2 and a 3 for a total of 5. The shooter rolls again and gets a 1 and a 1 for a total of 2. The shooter rolls again and gets a 6 and a 6 for a total of 12. The shooter rolls again and gets a 5 and a 6 for a total of 11. The shooter rolls again and gets a 1 and a 4 for a total of 5. This is a winning roll and now the shooter has $65.00. The shooter bets $65.00 and on the first roll, gets a 5 and a 4 for a total of 9. The shooter rolls again and gets a 1 and a 2 for a total of 3. The shooter rolls again and gets a 4 and a 4 for a total of 8. The shooter rolls again and gets a 2 and a 5 for a total of 7. This is a losing roll and now the shooter has $0.00. The game is over since the shooter is out of money.
Random Number Generator You will need to use a random number generator to complete this assignment. To use random numbers, we have to import that Random class, similar to how we import the Scanner class. We can handle this using the import java.util.random. Once we import the Random class, we have to create an instance of the class. (e.g, Random random = new Random();). Once created, we can use the variable name (in this case, random) to access the methods of the Random class. The only method you will need from this class for the assignment is the nextint(integer param) method. This method will return a value from 0 up to, but excluding, the value of the parameter. For a dice, we need a value from 1 to 6, so to do this we can use the following code: random.nextint(6) + 1. Below is an example of how to use the Random class: import java.util.random; public class ExampleClass { public static void main(string[] args) { int diceroll1, diceroll2; Random random = new Random(); diceroll1 = random.nextint(6) + 1; diceroll2 = random.nextint(6) + 1; } } You only need to have the statement Random random = new Random(); one time in your code. Every time you invoke random.nextint(6), it will return a random value from 0 to 5. Remember, every time you roll the dice, you will need to invoke this method for both die, similar to the example above. The Program Create a new class in this project called Craps. You will write a program that plays the craps dice game. Your program must do the following: 1) Create a constant and store in it the initial starting balance of 50. 2) Start the program by showing a welcome message and the rules of craps. You should only display this to the user ONE time (not every time we loop). 3) Begin a loop that should continue until either the user enters 0 as a bet, or the current balance is 0. If the user enters 0, the program should display the appropriate game ending messages such as in my output and then immediately terminate. 4) At the start of a new set of rolls, display the user s current balance and then prompt the user to enter a bet amount.
5) You may assume that all user bets will be an integer value (assume the user will not enter values like 1.5) the must be from 1 up to the current balance (at the start, users should only be allowed to enter a bet from 1 to 50, or 0 if they want to exit the game). a. If the user enters a bet less than 0 or greater than balance, the program should inform the user of an invalid bet and then again prompt the user for a another bet, as in my example, until a valid bet is entered. 6) After a valid bet, display the value of each dice roll and the total of the two dice together. For dice rolls, we need the random number generator described above. a. If it s a winning roll, inform the user and then add the value of the bet to the current balance b. If it s a losing roll, inform the user and then subtract the value of the bet from the current balance c. If it s a roll that requires more rolling, continue rolling and displaying the values of the rolls (as in my output) until either a winning roll or a losing roll occurs, based on the rules of Craps. 7) Continue to play the game until one of the conditions in part 3 above occurs. At the end of the game, display the appropriate game ending messages. If the current balance is greater than or equal to the starting balance, consider it a win and display the amount above $50.00 that was won; for example, You won $19.00. If the current balance is less than the starting balance, display something the amount lost, for example, You lost $25.00. 8) Don t forget that the balance at all times must be formatted as currency. The printf() method makes this easier. Refer back to assignment 4 if you don t remember how to do this. Tips: To complete this program, consider using a do-while loop for the outer loop, a nested do-while loop to verify the bet amount, and another nested while loop for the continuing dice rolls. When examining whether a first roll is a win, a loss, or a continuing roll, a switch statement works nicely. Example Output 1 (User input is the bolded blue text) Welcome to Craps! Rules 1. Start by placing a bet 2. The dice are then rolled - Rolling a 7 or 11 is a win - Rolling a 2, 3 or 12 is a loss - Otherwise, rolling continues until: -- Rolling the original total of the dice again (win) -- Rolling a 7 (loss)
Please enter a bet (or 0 to exit): -5 Invalid bet! Must be a value from 1 to 50. Please enter a bet (or 0 to exit): 51 Invalid bet! Must be a value from 1 to 50. Please enter a bet (or 0 to exit): 5 You rolled 5 and 3 totaling 8. More rolls needed: You rolled 5 and 2 totaling 7. Your current balance is $45.00 Please enter a bet (or 0 to exit): 46 Invalid bet! Must be a value from 1 to 45. Your current balance is $45.00 Please enter a bet (or 0 to exit): 0 GAME OVER Your final balance is $45.00 You lost $5.00 Thanks for playing! Example Output 2 (User input is the bolded blue text) Welcome to Craps! Rules 1. Start by placing a bet 2. The dice are then rolled - Rolling a 7 or 11 is a win - Rolling a 2, 3 or 12 is a loss - Otherwise, rolling continues until: -- Rolling the original total of the dice again (win) -- Rolling a 7 (loss) Please enter a bet (or 0 to exit): 5 You rolled 1 and 3 totaling 4. More rolls needed: You rolled 6 and 4 totaling 10. You rolled 3 and 5 totaling 8. You rolled 6 and 6 totaling 12. You rolled 1 and 2 totaling 3. You rolled 2 and 3 totaling 5. You rolled 4 and 4 totaling 8. You rolled 5 and 4 totaling 9. You rolled 3 and 6 totaling 9. You rolled 6 and 6 totaling 12. You rolled 1 and 2 totaling 3. You rolled 3 and 2 totaling 5.
You rolled 6 and 4 totaling 10. You rolled 1 and 6 totaling 7. Your current balance is $45.00 Please enter a bet (or 0 to exit): 1 You rolled 2 and 1 totaling 3. Your current balance is $44.00 Please enter a bet (or 0 to exit): 23 You rolled 3 and 4 totaling 7. You win! Your current balance is $67.00 Please enter a bet (or 0 to exit): 0 GAME OVER Your final balance is $67.00 You won $17.00 Thanks for playing! Example Output 3 (User input is the bolded blue text) Welcome to Craps! Rules 1. Start by placing a bet 2. The dice are then rolled - Rolling a 7 or 11 is a win - Rolling a 2, 3 or 12 is a loss - Otherwise, rolling continues until: -- Rolling the original total of the dice again (win) -- Rolling a 7 (loss) Please enter a bet (or 0 to exit): 50 You rolled 2 and 4 totaling 6. More rolls needed: You rolled 3 and 1 totaling 4. You rolled 2 and 1 totaling 3. You rolled 1 and 3 totaling 4. You rolled 3 and 2 totaling 5. You rolled 6 and 6 totaling 12. You rolled 3 and 1 totaling 4. You rolled 2 and 3 totaling 5. You rolled 5 and 6 totaling 11. You rolled 3 and 4 totaling 7. GAME OVER Your final balance is $0.00 You lost $50.00 Thanks for playing!
Example Output 4 (User input is the bolded blue text) Welcome to Craps! Rules 1. Start by placing a bet 2. The dice are then rolled - Rolling a 7 or 11 is a win - Rolling a 2, 3 or 12 is a loss - Otherwise, rolling continues until: -- Rolling the original total of the dice again (win) -- Rolling a 7 (loss) Please enter a bet (or 0 to exit): 13 You rolled 5 and 6 totaling 11. You win! Your current balance is $63.00 Please enter a bet (or 0 to exit): 25 You rolled 3 and 4 totaling 7. You win! Your current balance is $88.00 Please enter a bet (or 0 to exit): 60 You rolled 5 and 5 totaling 10. More rolls needed: You rolled 3 and 4 totaling 7. Your current balance is $28.00 Please enter a bet (or 0 to exit): 0 GAME OVER Your final balance is $28.00 You lost $22.00 Thanks for playing!
Assignment Submission Submit the following file to me on blackboard: Craps.java All assignments will be submitted through blackboard. You are only allowed ONE submission so do not submit the assignment until you are completely done with it. However, you can upload the assignment and save a draft without submitting it. If you do this, don t forget to submit it before the due date. Saving a draft and submitting it are not the same thing. To submit the assignment, login to http://blackboard.ecu.edu. Navigate to our course and then to the Assignments folder. Click the link that says Lab Assignment 5. On this new page, scroll down to where it says Attach File and attach the java files for your program. You will need to browse to the location where you saved the file on your computer. If you aren t sure where it is located, refer to assignment 1 on how to find out.