CS 302 Summer 2012, Program 1 Programming Assignment #1 for CS302ers Summer 2012

Program 1 - A day in Las Vegas, Summer 2012

  DUE by 11:59 PM on Sunday, July 8th, 2012
  NOTE: NO LATE PROGRAMS WILL BE ACCEPTED!

P1 Announcements | Overview | Specifications | Testing | Submission | Back to Course Menu | Back to Course Home Page

P1 Announcements

Corrections, clarifications, and other announcements regarding this programming assignment will be found below.

  • 6/30/2012: Program 1 assigned.

Overview:

Assignment Goals:

  • Develop an algorithm using conditional and looping flow control (Chapter 3: Decisions and Chapter 4: Loops).
  • Develop a main program (in main method) from high-level specifications.
  • Gain experience using console I/O, for, while, do...while and other Java statements and clauses.
  • Practice using the Scanner class.
  • Practice using the Random class.
  • Learn how to test a basic program.
  • Learn to use proper style and comments.

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.

Specifications:

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.

Details:

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:

  1. View Status Report
    If this option is selected, display a detailed report of the current state of the program. This report should include the following information in the order:
    • Current Funds: the amount of money the player has currently
    • Initial Funds: the amount of money the player started with
    • Number Games Played: the total number of games the player has played (roulette, slots and craps)
    • Roulette Games Played: the number of times the player has played roulette
    • Slots Games Played: the number of times the player has played slots
    • Craps Games Played: the number of times the player has played craps
    After displaying this report, the program should continue by prompting the player to choose another option, that's why we should have a main loop.
  2. Play Roulette
    If this option is selected, we must first determine whether the user wants to bet on a color or a number. Prompt them to select from the following options by entering the corresponding integer:
    1. Color: if the user selects color, they must choose which color to bet on. Prompt them to select from the following options by entering the corresponding integer (note we are not allowing players to bet on green, the 3rd color for a real roulette game):
      1. Red
      2. Black
      Make sure to keep track of which color they selected.
    2. 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:

    1. If the player bet on a color and their selection does not match the color that is spun, or they bet on a number and their selection does not match the number that is spun, then they lose their bet. So, if they had $50 prior to playing, and bet $5, they have $45 afterwards.
    2. If the player bet on a color and their selection does match the color that is spun then they win an amount equal to that bet. So, if they had $50 prior to playing, and bet $5, they win $5 and have $55 afterwards.
    3. If the player bet on a number and their selection does match the number that is spun then they win an amount equal to 35 times their bet. So, if they had $50 prior to playing, and bet $5, they win $175 and have $225 afterwards.

    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.

  3. Play Slots

    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:

    • The slot machine consists of three (3) independent 'reels' - we'll call them reel one, two, and three. For each reel, we randomly select an integer from 1 to 4 (inclusive). This corresponds to how the reels 'spin' on a slot machine.
    • Once you have generated the values for each reel, print the results of the spins.
    • Determine the appropriate payout depending on the results, adjust the player's balance, and notify the player of how they did. The possible results are as follows:
      1. All three reels match. If all three wheels match, the player wins $25. So, if they had $50 prior to playing, they have $75 afterwards. Example spin: [3, 3, 3]
      2. Any two of the three reels match. In this case, the player doesn't win or lose but simply receives back their bet. So, if they had $50 prior to playing, they have $50 afterwards. Example spin: [1, 2, 1]
      3. There are no matches across the three reels. In this case, the player loses their bet. So, if they had $50 prior to playing, they have $45 afterwards. Example spin: [3, 2, 4]

    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.

  4. Play Craps

    If the user selects the option to play Craps, Game play should proceed as follows for Craps:

    1. Betting: Prompt the user to enter a bet. This should be similar to how you enter a bet for Roulette. The one exception is that, if the user selects to bet more than the amount of money they have, you should treat it as though they are betting all of their current money.
    2. Roll Dice: roll two six-sided dice. These dice should have the numbers 1-6 as their faces. The player's score is the sum of these 2 dice. Tell the user what the value for each die was, followed by their score. We will refer to this as the so-called first score.
    3. Evaluate Roll: The game proceeds differently depending on the player's first score. If first score is:
      • 7 or 11: The game is over, and the player wins the amount they had bet.
      • 2 or 3: The game is over, and the player loses the amount they had bet.
      • Any other score: Both dice are rolled again. If the new score is the same as the first score from this game, the player wins the amount they had bet, and the game is over. If the new score is seven, the game is over, and the player loses the amount they had bet. If the new score is neither of those values, repeat the roll. Keep doing this until the game ends.

      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.

  5. Quit
    If this option is selected, the program should proceed immediately to the game-end report (see the section "Display the Game-end report:").

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:

  1. The player's final amount of money (remaining balance)
  2. The player's initial money
  3. The amount of money the player has won or lost

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 :(".

Additional Notes to keep in mind:

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;

Testing:

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.

Submission:

Before handing in your program, check that you've done the following:

  • Did you verify that your program works correctly by following the information in the Testing section above? About 70% of your grade is based on your program working correctly. Note to get full credit you must make sure your program works for normal input/circumstances and is robust (i.e., works reasonably for abnormal/unusual/erroneous input/circumstances).
  • Did you turn in all necessary source files listed below? About 80% of your grade is based on the code you submit.
  • Did you use good programming style by following the CS 302 Style Guide? About 10% of your grade is based on good programming style.
  • Did you properly comment your program by following the CS 302 Commenting Guide? About 10% of your grade is based on proper commenting.
  • Is your program's source file named exactly Gamble.java and is your class named exactly Gamble? Deductions are made if you fail to properly name these.

Use dropbox for Programming Assignment 1 in Learn@UW to electronically submit and backup your work:

Submit only one single file:

  • "Gamble.java" containing your Java source code for your program.
  • Please submit this single java source file to the dropbox for programming assignment #1 in Learn@UW by due date/time.