CS21 Spring 2006
Homework 5
Due Wednesday, February 22 before 11:30pm


For this assignment you may work with one other student from our class. If you work with a partner, be sure to put both your names in the top-level comment of every .java file you submit, and only one of you should submit your joint solution using cs21handin.

Also, copy over my starting point files for the assignment:

# from your cs21/hw05 subdirectory:
% cp ~newhall/public/cs21/hw05/* .
% ls 
Makefile  Player.java  Roulette.java  

Program Description

Implement a program that plays Roulette with a single human player and two computer players. To do this you will implement two classes (Player.java for Player objects, and Roulette.java with the main part of the game).

Roulette is a betting game that is played with a roulette wheel and a ball. The roulette wheel consists of 38 sectors, each with a number and a color. The wheel spins with the ball in it, and the ball eventually comes to rest in one of the sectors. Players who placed a bet on a number or color that matched the one on which the ball stops, win their bet. Here is a picture of a Roulette wheel:

The game of Roulette is played by multiple players (you will play with one human player and 2 computer players). Before a spin, each player chooses the amount he/she will bet up to a $50 limit. Your human player's bet values should be read in from the user, and your computer player's bet values should be computed randomly. The Roulette wheel spins and the ball lands on some sector in the wheel. Each sector has number and color associated with it. A sector's color is either GREEN, RED, or BLACK. Players can bet either on a color (red or black, but NOT green), or players can bet on a specific number (a value between -1 and 36 inclusive). For our game, here is how numbers and colors are assigned:

For example, this is what part of our Roulette Wheel will look like:

Your program will start out by prompting the user to enter information to initialize the human player, and will create two additional computer players. Next, your program should begin playing rounds of Roulette until either the user chooses to walk away from the table (quit) or one of the three players has no money left to continue betting. At this point, your program should print out the final results of all games: for each Player, their name, the number of bets they won, and the amount of money they have left. In addition, print out the total number of games played.

See my sample output below to get an idea of what a run of your program should look like.


Implementation Details

The Player class represents a Roulette player and should have the player's name, age, the number of bets won, the amount of money left to bet, and anything else you need to add. Add constructor, accessor, and modifier methods to the Player class. In addition, you may want to add methods to change internal Player state when a player wins or loses a bet.

The Roulette class should have the main method that creates three Player objects, plays rounds of the games, and prints out the final results. Add helper methods to this class that implement parts of this large task (think Top-Down design here).

One player is the user of your program. Your program should read in information from the user that you will use to initialize their Player object. The other players are computer players, and their name and age can be hard-coded in (the user doesn't need to enter it). Your program should next enter a loop that continues until the user doesn't want to play another round of the game or until one of the players is out of money. For each round, your main game playing method should call a helper method name PlayGame, that plays one round of the game:

  1. Prompt user to enter his/her next bet amount (up to a $50 limit). Make sure that a user doesn't bet more money than she/he has.
  2. Prompt user to enter his/her choice for current game:
    1. a user can choose to bet on a color (either RED or BLACK)
    2. or a user can choose to bet on a number (a value between -1 and 36 inclusive)
  3. Randomly "choose" each computer player's next bet amount and choice
  4. Spin the wheel (it lands on one of 38 sectors with numbers and colors assigned:
    1. -1 and 0 are GREEN
    2. even values between 2-36 inclusive are RED
    3. odd values between 1-35 inclusive are BLACK
  5. Compute each players results:
  6. Print out the results of this round

The main loop should print out the results of this game, and than ask the user if he/she wants to play again. When the user answers "no", your program should print out the results of all games played.

Think about adding additional methods to your solution to implement good modular design. For example, you may want to add some private static helper methods in your Roulette class that are helper functions for your main method. A method that prints out a prompt and reads in an int value between some range might be helpful.

As a first step you may want to implement a single game of Roulette, with a single player. Once you get this to work, add 2 more Players, and add code to keep playing games of Roulette until a Player loses all his/her money or the user of the game doesn't want to play another round.


Sample Output

Here are two sample runs of my program, the first ends when one player runs out of money, the second, when the user choses to walk away from the table (note the error handling for bad input values):
% java Roulette

This program plays multiple rounds of 
Roulette against two computer opponents.
Before each spin, place your bet.
The  payout of winning bets are
	 1:1 for color bets
 	35:1 for number bets
Good Luck!!!!!

 Enter your name: Tia
 Enter your age: 42

How much money do you want each player to start with: 50

Okay, Tia, get ready to play THE ENIAC and UNIVAC computers...

================================
Let's play a round of Roulette
Place your bets...

Enter the dollar amount of your next bet up to our 50 dollar limit: 30
Enter 0 to choose RED, or 1 to choose BLACK, or 2 to choose a number: 2

Which number do you pick for your next bet: 99
  hey, 99 isn't between -1 and 36, try again...
Which number do you pick for your next bet: -4
  hey, -4 isn't between -1 and 36, try again...
Which number do you pick for your next bet: 33


Bets for this round:
------------------------------
Player: Tia chooses NUMBER 33, and bet $30 this round.
Player: THE ENIAC chooses RED, and bet $29 this round.
Player: UNIVAC chooses BLACK, and bet $36 this round.

The spin is: 
	BLACK 21

Results for this round:
------------------------------
Player: Tia lost. 
	 and now has $20 left
Player: THE ENIAC lost. 
	 and now has $21 left
Player: UNIVAC won!  
	 and now has $86 left

Do you want to play again (enter 1 for yes and 0 for no)? 1


Get ready to play next game...

================================
Let's play a round of Roulette
Place your bets...

Enter the dollar amount of your next bet up to our 50 dollar limit: 50
you can't bet $50, you only have $20 left.  Try again...
Enter the dollar amount of your next bet up to our 50 dollar limit: 20
Enter 0 to choose RED, or 1 to choose BLACK, or 2 to choose a number: 0


Bets for this round:
------------------------------
Player: Tia chooses RED, and bet $20 this round.
Player: THE ENIAC chooses NUMBER 23, and bet $20 this round.
Player: UNIVAC chooses NUMBER 26, and bet $33 this round.

The spin is: 
  	RED 22

Results for this round:
------------------------------
Player: Tia won!  
	 and now has $40 left
Player: THE ENIAC lost. 
	 and now has $1 left
Player: UNIVAC lost. 
	 and now has $53 left

Do you want to play again (enter 1 for yes and 0 for no)? 1


Get ready to play next game...

================================
Let's play a round of Roulette
Place your bets...

Enter the dollar amount of your next bet up to our 50 dollar limit: 20
Enter 0 to choose RED, or 1 to choose BLACK, or 2 to choose a number: 1


Bets for this round:
------------------------------
Player: Tia chooses BLACK, and bet $20 this round.
Player: THE ENIAC chooses RED, and bet $1 this round.
Player: UNIVAC chooses NUMBER 11, and bet $28 this round.

The spin is: 
  	BLACK 33

Results for this round:
------------------------------
Player: Tia won!  
	 and now has $60 left
Player: THE ENIAC lost. 
	 and now has $0 left
Player: UNIVAC lost. 
	 and now has $25 left

oops, one player is out of money...no more games can be played

Final Results:
------------------------------------------------
Name 		 Age	 Num Wins	 Amt Won
------------------------------------------------
       Tia	 42	  2       	$  60
 THE ENIAC	 59	  0       	$   0
    UNIVAC	 54	  1       	$  25
------------------------------------------------
Total number of games: 	  3


% java Roulette

This program plays multiple rounds of 
Roulette against two computer opponents.
Before each spin, place your bet.
The  payout of winning bets are
	 1:1 for color bets
 	35:1 for number bets
Good Luck!!!!!

 Enter your name: Tia
 Enter your age: 33

How much money do you want each player to start with: 30

Okay, Tia, get ready to play THE ENIAC and UNIVAC computers...

================================
Let's play a round of Roulette
Place your bets...

Enter the dollar amount of your next bet up to our 50 dollar limit: 20
Enter 0 to choose RED, or 1 to choose BLACK, or 2 to choose a number: 0


Bets for this round:
------------------------------
Player: Tia chooses RED, and bet $20 this round.
Player: THE ENIAC chooses NUMBER 9, and bet $26 this round.
Player: UNIVAC chooses NUMBER 13, and bet $27 this round.

The spin is: 
     BLACK 23

Results for this round:
------------------------------
Player: Tia lost. 
	 and now has $10 left
Player: THE ENIAC lost. 
	 and now has $4 left
Player: UNIVAC lost. 
	 and now has $3 left

Do you want to play again (enter 1 for yes and 0 for no)? 0


So Long.

Final Results:
------------------------------------------------
Name 		 Age	 Num Wins	 Amt Won
------------------------------------------------
       Tia	 33	  0       	$  10
 THE ENIAC	 59	  0       	$   4
    UNIVAC	 54	  0       	$   3
------------------------------------------------
Total number of games: 	  1