COSC 135 B Lab 2

Tom Linton, Fall 2000, Central College
Let's try a fresh start with methods, maybe some simpler examples! In case you're interested, here is source code for the three depreciation type methods. Let's try something that is a bit more fun!

A simplified version of a roulette game might involve a player that has a bankroll of money and makes bets on which number the roulette wheel lands on. For this activity, we will concentrate on setting up the basics of a Player class and the bet making process. Assume the roulette wheel:

A player can bet on red (meaning they win if the spin of the wheel produces an even slot number) or black (where they win if the ball lands in an odd slot number) and the payoff in both these cases is twice the amount they bet (when they win; if they lose, the payoff is zero). A player can also bet on a single number (from 1 to 36). To win this type of number bet, the spin of the wheel must produce their selected number (if you bet on slot 7, the ball must land in slot 7). The payoff in this case is 36 times the amount of the bet. In order to process these three types of bets, we will utilize numeric codes for the three types of bets. We can implement constants in our Player class that make this coding more readable. Suppose that our Player class has an integer variable called betType, to store the value 1, 2 or 3 for a bet on black, red or a number respectively. We can declare constants, which are variables with the modifier final, to designate that their values are fixed or finalized and cannot change. Consider the following block of code:
final int BLACK = 1;
final int RED = 2;
final int NUMBER_BET = 3;
int betType;

// later in this class file

if (betType == BLACK){
   handle a bet on black;
}
else if (betType == RED) {
   handle a bet on red;
}
else if (betType == NUMBER_BET) {
   handle a bet on a specific number;
}
 

Nobody reading this code will have trouble figuring out your coding scheme (that is, a black bet is coded by a 1, a red bet by 2, etc.). Enough on constants and coding string values with numbers, let's design the basic shell of our roulette player class. Our players should be able to place bets on red or black spins, as well as place bets on any single outcome (like the ball landing in slot 7). Since we'll be betting, we'll need a bankroll, or the current amount of money our player has available. We'll need a way for our player to spin the roulette wheel (for now, we'll just add a "spin" field, where the user can type in the result of the spin). In order to process our bets, we'll need a payoff method, to figure out how much we won, and update our bankroll appropriately.
  1. Copy the roulette folder from G:\Lintont\Cosc135b\Pickup\ onto your H:\ drive folder for this class.
  2. Start CodeWarrior and select File -> Open, and open the roulette.mcp project file (the copy you placed in your H:\ drive). If you don't have Codewarrior available, here is the source code for Player.java.
  3. Read through the code. It should compile and run, but most of the methods do nothing as of yet. I have given all of the declaration lines for the class methods. I have also given working (but not robust) code for one of the place a bet methods (the one for betting on a single number). Each method for placing a bet should:
  4. Write the other two bet placing methods and test them out. Print out a copy of the source code file and turn it in.
  5. Now, make all three bet placing methods more robust. At the start of each, check to make sure that the player is NOT betting more money than they have in their bankroll. If they attempt to bet more than they have, reset the betAmount to be equal to their current bankroll amount and display (using a messageBox) a message stating that they are betting it all!
  6. Notice that the pre-wriiten code illustrates an overloaded payoff method. One version (the one with 3 integer inputs) is designed to calculate the payoff for a bet of type NUMBER_BET (on a specific number). The three parameters to this method are:
    1. the amount of the player's bet (wager);
    2. the number that the player is betting on (selectedNumber); and
    3. the slot that the ball landed in after the roulette wheel was spun (wheelValue).
    All that the payoff method needs to do is to determine if the bet was a winning bet, and if it was, increase the player's bankroll by the correct amount (and display the new value of the bankroll). Write the 3 parameter payoff method.
  7. The 2 parameter payoff  method is called whenever the bet was on red or black. The inputs to this version of the payoff method are the amount of the bet  and number that the wheel produced when it was spun. Write this two parameter version of the payoff method. Use the remainder operator, % to determine if the spin was red or black, then check the value of betType to see if the player won or lost their bet. If they won, add the appropriate amoutn to the variable bankroll and redisplay the new amount.
  8. Be sure to check your code by typing in a few winning spins and a few losing spins of each betType. Add comments to your code and then turn in the entire folder to your subfolder under G:\Lintont\cosc135b\Handin\.