Corrections, clarifications, and other announcements regarding this programming assignment will be found below.
Assignment Goals:
Scanner
class.Random
class.Description: Whether or not you have been to a casino in Las Vegas, you're probably familiar with the sorts of games of skill and chance that can be found there. Poker, blackjack, and many other games provide ample opportunity to test your luck, and lose your money. You might win or lose depending on your luck; the only given is that, one way or another, the casino always wins (otherwise it wouldn't exist).
You'll be creating a program that simulates a simplified version of some popular games. More specifically, you'll be implementing a roulette game, a slots game and a craps game (see Roulette and Slot machine and Craps if you are unfamiliar with these games).
The player's goal of course is to win as much money as possible (in worst case, we should say - lose a little as possible). When the game begins, they have $100 available with which to gamble. The program repeatedly does the following until the game is over. The player is told their current balance and then is given five (5) options: request a status report describing how they are doing, play roulette, play slots, play craps or quit.
The status report provides information about the player's current performance.
If they choose to play roulette, they'll have the option of choosing whether to bet on a color (red or black) or a number (integer from 0 to 36). The player can also choose the amount of their bet. The wheel is spun, and the player either wins or loses money proportional to the bet.
If they select slots, their bet will always be $5 - the amount the 'machine' takes. Three 'reels' will spin - if all land on the same value, the player wins $25. If two of three are the same, the player doesn't win anything but they do get their $5 back, and if there are no matches, they lose their $5.
If they select craps, the player should choose the amount of bet. The player wins or loses bet depending on what comes out of rolling two six-sided dice. Refer to 'Specifications' for more details.
The game ends when the player decides to quit, or the player runs out of money. When the game ends, the player is shown a report of their final balance, initial balance, and the amount they have won or lost.
It is strongly recommended that you incrementally develop your code since this is a very effective way to program. By this we mean, code a small part of the specifications, carefully test that it works as desired, and then add another small part. Developing code incrementally minimizes the places where you have to search for bugs to the part you recently added since you've already tested the previous parts. This saves tons of time since it's unlikely you'll need to look through your entire program for the problem.
Please first click on Gamble.java for the skeletal code provided as a starting point for your program. You must implement the tasks in the order shown in the code skeleton. Download it and put it in your project directory in Eclipse. Please add your code directly into that file.
Your program will get input from the player using a Scanner object. In the skeleton code provided, I've already added the code to make the Scanner object. This must be the only Scanner object that you should use in your program. Making additional Scanner objects will cause you to lose points for your program's code.
Your program will also be able to generate random numbers to add unpredictability to the program to make it a bit more interesting. We've also added code that asks for a seed number to be entered when the program begins, and uses that seed to make a (pseudo) random number generator. Again this must be the only random number generator that you use in your program. Making additional Random objects will cause you to lose points for your program's code.
Here are the details of how your program should work for each game:
I). Display the Instructions:
When the program just begins, display instructions so that the player understands the program
and how to play each of the 3 games. Make your instructions clear, concise and complete like in many commercial software programs.
Also, display the player's beginning balance, which is $100 in this case.
II). Repeat the main loop:
until the player runs out of money or decides to quit.
Each repetition, display the player's current amount of money, and then ask the player to choose from the following options by entering the corresponding integer:
Number: if the user selects number option, they must choose which integer to bet on. The roulette wheel consists of the integers from 0 to 36 (inclusive). Any integer from within this range is valid, while any integer from outside this range is not.
Prompt them to select a number, and read the integer they enter.
Once the program has determined what the player wants to bet on, it must get the amount of the player's bet. Prompt the user to enter an appropriate amount. Only values from 1 to the player's current balance are acceptable. Note: the player cannot bet $0 or less, they cannot bet more than they have, and keep in mind all bets must be in whole dollar increments.
To this point, the program has now collected all of the necessary information to play the game. Proceed to 'spin' the roulette wheel. The result of spinning the wheel is an integer from 0 to 36 (inclusive). If the integer is odd, this corresponds to a red position on the wheel. If the integer is even, this corresponds to a black position on the wheel - with one exception! If the integer is 0, this corresponds to a green position on the wheel. (Note - this is not exactly how real roulette works, but we'll take this approach in this program). Print out the results of the roulette spin - make sure to indicate the color and the number that was selected.
Now that we have determined the results of the spin, we need to figure out how to adjust the player's money. This is determined as follows:
In each of the above cases, you should print out the player's net monetary change from the game. If the change is positive (they gained money), include a '+' sign in front of it. If it was negative, include a '-' sign in front of it. So, if they won $5 dollars, you might print out "Net change this game: +5", while if they lost $5, you might print out "Net change this game: -5"
Once the current round of roulette is complete, prompt the player to choose another option if they have money left. If not, proceed to the game-end report.
If this option is selected, first verify that the user has enough money to play this game. Slots requires a bet of $5. If the player does not have enough money to play, print an appropriate message, and prompt the player to choose another option. If they do, proceed as follows:
Once this round of slots is complete, prompt the player to choose another option if they have money left. If not, the program must proceed to the game-end report.
If the user selects the option to play Craps, Game play should proceed as follows for Craps:
Tell the player the results of each individual dice roll for each roll after the first roll, but do not tell them the summed score. When the game ends, let them know how they did - whether they won or lost, followed by how much they won or lost.
Display the Game-end report:
If the player has run out of money, or chosen to quit the program, then display a report and then end the program.
This report should include the following information in this order:
Your program must display whether the player won or lost money (or broke even). For example, if they won $10, display "You won: $10". If they lost $20, display "You lost: $20". If they ended with the same amount they started with, print a message indicating there was no change like "you wasted your time of being in casino today :(".
1. A note on invalid input. If at any point during the program run the player enters an invalid choice, the program should print an appropriate message, and repeat the prompt. So, for instance, if the player is playing roulette, and betting on a color, they must choose between red (1) and black(2). If they enter 3, this is not a valid choice - the program should say so, and repeat the menu prompt for selecting a color. Continue doing this until a valid value is entered.
2. A note on random numbers. There are various places throughout this program that require some sort of random calculation. Wherever the program uses a value within a range, you should use the nextInt() method with the quantity of values in the desired range, and shift the result as appropriate. For example, to find a random number between 2 and 11 (inclusive), you would perform the calculation int myNum = rand.nextInt(10) + 2;
It is important to verify that your program works under normal circumstances as well as when the user makes a mistake. First, develop your program incrementally (described under the specifications above) so that it works as specified for valid input. Then enhance your program so that it behaves reasonably even when the user makes a mistake. Think through all possible errors a user might make when using your program and write defensive code to handle them. If a mistake is made, a clear error message should be displayed and the user should be allowed to try again until they get it right (instead of crashing your program!!).
Make sure to carefully compare your program with this sample run that I(Yinggang) got using my laptop to verify that it is working correctly. Though your text doesn't have to be identical, your numbers and sequence of input should match mine exactly. I recommend you to use the similar text to mine for sort of consistency, but it's not required.
For full credit, you must handle both incorrect integer input (e.g., negative numbers, out of range, etc.) as well as non-integer input (e.g., inputting 'a' when prompted for an integer, SEE 3.6 in the textbook). I highly recommend you first create your program to work under normal circumstances (without code that checks for valid input). Once you're confident that version is working correctly, then add validation of incorrect integer input (handle values that are out of range). After that is working, add code to handle non-integer input.
Before handing in your program, check that you've done the following:
Use dropbox for Programming Assignment 1 in Learn@UW to electronically submit and backup your work:
Submit only one single file: