The Artima Developer Community
Sponsored Link

Java Answers Forum
Roulette

10 replies on 1 page. Most recent reply: Mar 9, 2002 7:27 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Roulette Posted: Mar 8, 2002 3:27 PM
Reply to this message Reply
Advertisement
Will someone please look at my roulette program and tell me what to write to "insert a call to the bet_options method from the make_bet method." I've tried everything to call this thing.

import java.io.*;
import java.util.*;


//*************************************************************
//Class Roulette contains the main driver for a roulette game
//*************************************************************
class Roulette {

//========================================================
// Contains the main processing loop for the roulette game
//========================================================
public static void main (String[] args) throws IOException{
Player player = new Player (100);
boolean done = false;

System.out.println ("Cash available: " + player.cash());
System.out.println();
while (!done) {
player.make_bet();
System.out.println ("Cash available: " + player.cash());
done = !player.spin_again();
System.out.println();
}
}//method main
} //class Roulette


//*****************************************************************
// Class Player represents one roulette player.
//*****************************************************************

cl ass Player {
private int bet, money;
private BufferedReader stdin;
private int bet_type;
//=============================================================
// The player constructor sets up the initial available cash.
//=============================================================
public Player (int initial_money) {
money = initial_money;
stdin = new BufferedReader (new InputStreamReader(System.in));

}//constructor Player

//================================================================
// Returns this player's current available cash.
//================================================================
public int cash() {
return money;
} //method cash

//==============================================================
// Prompts the user and reads betting information.
//==============================================================
public void make_bet() throws IOException{
System.out.print ("How much to bet: ");
bet = Integer.parseInt (stdin.readLine());
money = money - bet;

if (money == 0)
System.out.println ("Betting it all!");


} //method make_bet

//==============================================================
// Determines if the player wants to spin wheel again.
//==============================================================
public boolean spin_again() throws IOException{
String answer;
System.out.print ("Spin again [y/n]? ");
answer = stdin.readLine();
return (answer.equals ("y") || answer.equals ("Y"));
} //method spin_again

} //class Player

//****************************************************************
// Class Wheel represents a roulette wheel and its operations. Its
// data and methods are static because there is only one wheel.
//***************************************************************

class Wheel {

final static int RED = 1;
final static int BLACK = 2;
final static int NUMBER = 3;
private static int ball_position;
private static int color;
private final static int POSITIONS = 37;

//==============================================================
// Presents betting options and reads player choice.
//=============================================================
public static int bet_options (BufferedReader stdin)
throws IOException{
System.out.println ("1. Bet on red");
System.out.println ("2. Bet on black");
System.out.println ("3. Bet on a particular number");
System.out.print ("Your choice? ");
return Integer.parseInt (stdin.readLine());
}
} // class Wheel


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 5:01 PM
Reply to this message Reply
Hi Patti,

Just wanted to let you know that when you post code, if you surround it with the [java] and [/java] tags, it will look a lot nicer.

I'll also have a look at your code and answer the question, next time...

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 5:13 PM
Reply to this message Reply
OK. I didn't know about that.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 5:22 PM
Reply to this message Reply
Okay, here it is with "a call to the bet_options method from the make_bet method inserted."

Looks like you still have more to do, though, like the rolling of the ball and spinning of the wheel, along with the flashy 3-D sparkling graphics to boot.

A couple more comments: 1) See the code comment in the bet_options() method about input validation. 2) You might want to use the common Java coding convention of having method name parts delineated by capital letters instead of the old C-style underscores. For instance, names such as getOptions() instead of get_options(), etc.

import java.io.*;
import java.util.*;
 
//*************************************************************
 
//Class Roulette contains the main driver for a roulette game
 
//*************************************************************
 
public class Roulette
{
    
    //========================================================
    
    // Contains the main processing loop for the roulette game
    
    //========================================================
    public static void main(String[] args) throws IOException
    {
        Player player = new Player(100);
        boolean done = false;
        System.out.println("Cash available: " + player.cash());
        System.out.println();
        while(!done)
        {
            player.make_bet();
            System.out.println("Cash available: " + player.cash());
            done = !player.spin_again();
            System.out.println();
        }
    } //method main
} //class Roulette
 
//*****************************************************************
 
// Class Player represents one roulette player.
 
//*****************************************************************
 
class Player
{
    private int bet, money;
    private BufferedReader stdin;
    private int bet_type;
    
    //=============================================================
    
    // The player constructor sets up the initial available cash.
    
    //=============================================================
    public Player(int initial_money) 
    {
        money = initial_money;
        stdin = new BufferedReader(new InputStreamReader(System.in));
    } //constructor Player
    
    //================================================================
    
    // Returns this player's current available cash.
    
    //================================================================
    public int cash()
    {
        return money;
    } //method cash
    
    //==============================================================
    
    // Prompts the user and reads betting information.
    
    //==============================================================
    public void make_bet() throws IOException
    {
        System.out.print("How much to bet: ");
        bet = Integer.parseInt(stdin.readLine());
        money = money - bet;
        if(money == 0)
            System.out.println("Betting it all!");
 
        bet = Wheel.bet_options( new BufferedReader( new InputStreamReader(System.in) ) );
 
    } //method make_bet
    
    //==============================================================
    
    // Determines if the player wants to spin wheel again.
    
    //============================================================== 
    public boolean spin_again() throws IOException
    {
        String answer;
        System.out.print("Spin again [y/n]? ");
        answer = stdin.readLine();
        return (answer.equals("y") || answer.equals("Y"));
    } //method spin_again
} //class Player
 
//****************************************************************
 
// Class Wheel represents a roulette wheel and its operations. Its
 
// data and methods are static because there is only one wheel.
 
//***************************************************************
 
class Wheel
{
    final static int RED = 1;
    final static int BLACK = 2;
    final static int NUMBER = 3;
    private static int ball_position;
    private static int color;
    private final static int POSITIONS = 37;
    
    //==============================================================
    
    // Presents betting options and reads player choice.
    
    //=============================================================
    public static int bet_options(BufferedReader stdin) throws IOException
    {
        System.out.println("1. Bet on red");
        System.out.println("2. Bet on black");
        System.out.println("3. Bet on a particular number");
        System.out.print("Your choice? ");
 
        // Rather than simply returning here, you may want some input validation.
        // You should also catch the exception here as part of that.
        // If the input is out of range, or the wrong type, you would then
        // redisplay the options and ask for valid input.
        return Integer.parseInt(stdin.readLine());
    }
} // class Wheel 


(Strange thing -- in the preview, the color-coding of the java is not working, but the indentation is preserved. Hmm...)

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 8:02 PM
Reply to this message Reply
Well, I know you won't be shocked to hear I hit another snag. Please help with the spin method- I can't get the number and color of the spin to print.


java

import java.io.*;
import java.util.*;

//*************************************************************
/ /Class Roulette contains the main driver for a roulette game
//*************************************************************
class Roulette {

//========================================================
// Contains the main processing loop for the roulette game
//========================================================
public static void main (String[] args) throws IOException{
Player player = new Player (100);
boolean done = false;

System.out.println ("Cash available: " + player.cash());
System.out.println();
while (!done) {
player.makeBet();
System.out.println ("Cash available: " + player.cash());
done = !player.spin_again();
System.out.println();
}
}//method main
} //class Roulette


//*****************************************************************
// Class Player represents one roulette player.
//*****************************************************************

cl ass Player {
private int bet, money, number;
private BufferedReader stdin;
private int betType;
private int yourspin;


//=============================================================
// The player constructor sets up the initial available cash.
//=============================================================
public Player (int initialMoney) {
money = initialMoney;
stdin = new BufferedReader (new InputStreamReader(System.in));

}//constructor Player

//================================================================
// Returns this player's current available cash.
//================================================================
public int cash() {
return money;
} //method cash


//==============================================================
// Prompts the user and reads betting information.
//==============================================================
public void makeBet() throws IOException{
System.out.print ("How much to bet: ");
bet = Integer.parseInt (stdin.readLine());
money = money - bet;

if (money == 0)
System.out.println ("Betting it all!");

betType = Wheel.betOptions( new BufferedReader( new
InputStreamReader(System.in) ) );


if (betType == 3)
System.out.println ("Pick a number from 1 to 37.");
number = Integer.parseInt (stdin.readLine());

yourspin = Wheel.spin();


} //method make_bet

//==============================================================
// Determines if the player wants to spin wheel again.
//==============================================================
public boolean spin_again() throws IOException{
String answer;
System.out.print ("Spin again [y/n]? ");
answer = stdin.readLine();
return (answer.equals ("y") || answer.equals ("Y"));
} //method spin_again

} //class Player

//****************************************************************
// Class Wheel represents a roulette wheel and its operations. Its
// data and methods are static because there is only one wheel.
//***************************************************************

class Wheel {

final static int RED = 1;
final static int BLACK = 2;
final static int NUMBER = 3;
private static int ballPosition;
private static int color;
private final static int POSITIONS = 37;


public static int spin(){

int ballPosition = (int) (Math.random() * 36);
return ballPosition;}
{
if (ballPosition%2 == 0)
color = 1;
else
color = 2;

System.out.println ("Your spin is: " + ballPosition + color);

}

//==============================================================
// Presents betting options and reads player choice.
//=============================================================
public static int betOptions (BufferedReader stdin)
throws IOException{
System.out.println ("1. Bet on red");
System.out.println ("2. Bet on black");
System.out.println ("3. Bet on a particular number");
System.out.print ("Your choice? ");
return Integer.parseInt (stdin.readLine());
}
} // class Wheel

/Java

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 9:29 PM
Reply to this message Reply
Here's the whole thing. It has at least five mistakes:
I can't get it to print the results of the spin-number and color. I can't get the payoffs straight AT ALL-I'm lost-I tried to put in as much as I could. Please help. I've only been at this 6 weeks and I want to learn it.


java


import java.io.*;
import java.util.*;
import Wheel;



//*************************************************************
//Cla ss Roulette contains the main driver for a roulette game
//*************************************************************
class Roulette {

//========================================================
// Contains the main processing loop for the roulette game
//========================================================
public static void main (String[] args) throws IOException{
Player player = new Player (100);
boolean done = false;

System.out.println ("Cash available: " + player.cash());
System.out.println();
while (!done) {
player.makeBet();
System.out.println ("Cash available: " + player.cash());
done = !player.spinAgain();
System.out.println();
}
}//method main
} //class Roulette


//*****************************************************************
// Class Player represents one roulette player.
//*****************************************************************

cl ass Player {
private int bet,yourNumber;
private BufferedReader stdin;
private int betType;
private int yourSpin;
private static int winnings;
private static int money;

//=============================================================
// The player constructor sets up the initial available cash.
//=============================================================
public Player (int initialMoney) {
money = initialMoney;
stdin = new BufferedReader (new InputStreamReader(System.in));

}//constructor Player

//================================================================
// Returns this player's current available cash.
//================================================================
public int cash() {
return money;
} //method cash

public static int payment(){
return winnings;


} //method payment
//==============================================================
// Prompts the user and reads betting information.
//==============================================================
public void makeBet() throws IOException{
System.out.print ("How much to bet: ");
bet = Integer.parseInt (stdin.readLine());
money = money - bet;

if (money == 0)
System.out.println ("Betting it all!");

betType = Wheel.betOptions( new BufferedReader( new
InputStreamReader(System.in) ) );


if (betType == 3)
System.out.println ("Pick a number from 1 to 37.");
yourNumber = Integer.parseInt (stdin.readLine());

yourSpin = Wheel.spin();

money = Player.payment();

}//method makeBet


//==============================================================
// Determines if the player wants to spin wheel again.
//==============================================================
public boolean spinAgain() throws IOException{
String answer;
System.out.print ("Spin again [y/n]? ");
answer = stdin.readLine();
return (answer.equals ("y") || answer.equals ("Y"));
} //method spin_again

} //class Player

//****************************************************************
// Class Wheel represents a roulette wheel and its operations. Its
// data and methods are static because there is only one wheel.
//***************************************************************

class Wheel {

final static int RED = 1;
final static int BLACK = 2;
final static int NUMBER = 3;
private static int winnings;
private static int ballPosition;
private static int color;
private final static int POSITIONS = 37;


public static int spin(){

int ballPosition = (int) (Math.random() * 36);
return ballPosition;}
{
if (ballPosition%2 == 0)
color = 1;
else
color = 2;

System.out.println ("Your spin is: " + ballPosition + color);

}

private static int numberPayoff ( int bet, int betType, int money){
if (betType == 1 || betType == 2 && ballPosition == NUMBER)
winnings = bet * 36;
return winnings;
}

private static int redBlackPayoff (int bet, int betType, int money){
if ((betType == 1 && color == 1) || (betType ==2 && color ==2))
winnings = bet * 2;
return winnings;
}

//==============================================================
// Presents betting options and reads player choice.
//=============================================================
public static int betOptions (BufferedReader stdin)
throws IOException{
System.out.println ("1. Bet on red");
System.out.println ("2. Bet on black");
System.out.println ("3. Bet on a particular number");
System.out.print ("Your choice? ");
return Integer.parseInt (stdin.readLine());
}
} // class Wheel

/java

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 10:50 PM
Reply to this message Reply
Don't forget the square brackets around the java and /java tags.

As for you code, the problem is disguised by the indentation, somewhat, but it appears that the part that prints out the spin result is floating by itself and is not a part of the spin() method. Which means it is being treated as a static initializer, by the way. This is the code I'm referring to:
    public static int spin()
    {
        int ballPosition = (int) (Math.random() * 36);
        return ballPosition;
    }
    {
        if (ballPosition%2 == 0)
            color = 1; 
        else 
            color = 2;         
        System.out.println ("Your spin is: " + ballPosition + color);
    }

I added indentation, which should further clarify the problem. Additionally, even removing the extra curlies to move the block into the method would not be quite enough; you also need to move the return statement to the end, so the result will be something like this:
    public static int spin()
    {
        int ballPosition = (int) (Math.random() * 36);
        if (ballPosition%2 == 0)
            color = 1; 
        else 
            color = 2;         
        System.out.println ("Your spin is: " + ballPosition + color);
        return ballPosition;
    }

Sorry, but I have a little more bad news. There are 38, not 36 possible results on a roulette table. Two of them are neither black nor red, however, they are the special, usually green, values of 0 and 00. So, really the odds are stacked in favor of the house, as usual (of course). Additionally, it is not the case that all odds represent one color and evens the other. Here is the layout:
Red: 1 3 5 7 9 12 14 16 18 19 21 23 25 27 30 32 34 36
Black: 2 4 6 8 10 11 13 15 17 20 22 24 26 28 29 31 33 35
Green (or "other"): 0 00
One way to deal with this would be to make an array in the Wheel class like this:
    private final static int positions = { 
                                            GREEN,  //  0
                                            RED,    //  1
                                            BLACK,  //  2
                                            RED,    //  3
                                            BLACK,  //  4
                                            RED,    //  5
                                            BLACK,  //  6
                                            RED,    //  7
                                            BLACK,  //  8
                                            RED,    //  9
                                            BLACK,  // 10
                                            BLACK,  // 11
                                            RED,    // 12
                                            BLACK,  // 13
                                            RED,    // 14
                                            BLACK,  // 15
                                            RED,    // 16
                                            BLACK,  // 17
                                            RED,    // 18
                                            RED,    // 19
                                            BLACK,  // 20
                                            RED,    // 21
                                            BLACK,  // 22
                                            RED,    // 23
                                            BLACK,  // 24
                                            RED,    // 25
                                            BLACK,  // 26
                                            RED,    // 27
                                            BLACK,  // 28
                                            BLACK,  // 29
                                            RED,    // 30
                                            BLACK,  // 31
                                            RED,    // 32
                                            BLACK,  // 33
                                            BLACK,  // 34
                                            BLACK,  // 35
                                            RED,    // 36
                                            GREEN   // 00
                                         };
 

and have methods along these lines:
    String getColor()
    {
        switch( positions[ballPosition] )
        {
            case BLACK: return "black";
            case RED:   return "red";
            case GREEN: return "green";
        }
    }

This brings up another point. The spin() and other methods shouldn't be static. Instead, the Wheel should be an object, which has some state and methods for finding out about it (getColor()) and changing it (spin()). Also, as discussed in another recent post, your program may be more flexible if you don't print out results all over the place (in many different methods). In other words, main() should be able to glean all the information from the objects and print it out, otherwise all these different classes you are developing are tied directly to stdin and stdout. That may not seem to be a big deal now, but when you have to change this to be a graphical game using Swing, you will appreciate it.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Roulette Posted: Mar 8, 2002 10:54 PM
Reply to this message Reply
Oops, the positions array needs a [], of course:
private final static int positions[] = { ... };

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Roulette Posted: Mar 9, 2002 10:34 AM
Reply to this message Reply
Well, I've been working real hard on this. And I want to thank you for all your help and ask one last thing because it almost works perfectly. I don't have to add green because the instructions said just use even for red and odd for black (But you're right-I never would have remembered the green).Can't call the payoffs to the player.payment:


 
public static int payment(){
     	Wheel.numberPayoff;
     	Wheel.redBlackPayoff;
     	System.out.println ("Total payoff is: " + winnings);
     	money = money + winnings;
     	System.out.println ("Money now is: " + money);
     	return money;
                                	    	      	
     } //method payment   
 
In the Wheel class is:
 
private static int numberPayoff ( int bet, int betType, int NUMBER){ 
     	
     	if (betType == 3 && ballPosition == yourNumber)
          winnings = bet * 36;
        
     	return winnings;      
     }	
     
     private static int redBlackPayoff (int bet, int betType){
     	       
        if (betType == 1 && ballPosition%2 ==0)
           winnings = bet * 2;
     	if (betType ==2 && ballPosition%2 !=0)
     	   winnings = bet * 2; 
     	
         return winnings;
      }	

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Roulette Posted: Mar 9, 2002 2:10 PM
Reply to this message Reply
I got it !!! Thanks Matt.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Roulette Posted: Mar 9, 2002 7:27 PM
Reply to this message Reply
Hey Patti, a few more points to consider.

I recommend creating a static instance of java.util.Random in your class and then using it's nextInt(int range) method. This is better than using modulo on Math.random() and casting to int. Josh Bloch's book Effective Java (http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=pd_sxp_elt_l1/002-2035182-3032057), in explains in detail why this is, but essentially, it is simpler to use and you get better random distribution.

In your code above, you are using constants like 1, 2 and 3, but you have previously defined names like RED, BLACK and NUMBER for these values. You should always use the relevant named constants wherever possible. This will make your code much more readable and easy to understand. In fact, I would add some additional constants (static finals), to represent the number of numbers (36), the payoff multipliers (which in your case are the same as their corresponding number of possibilities, but in a real game the multiplier would be 36, while the number of possible picks is 38). This will make your program more flexible, so if a later assignment is to make the game more realistic, you will be ahead of the game.

Flat View: This topic has 10 replies on 1 page
Topic: Serial Port Manipulation with Visual J++, any chance? Previous Topic   Next Topic Topic: Methods

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use