The Artima Developer Community
Sponsored Link

Java Answers Forum
One last Roulette snag

1 reply on 1 page. Most recent reply: Mar 11, 2002 1:36 PM by Patti

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 1 reply on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

One last Roulette snag Posted: Mar 11, 2002 10:37 AM
Reply to this message Reply
Advertisement
I can't get this program to process the numberPayoff portion which pays off 36 * bet. It seems yourNumber is not being compared to ballPosition correctly.
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.spinAgain();
		  	 System.out.println();
		  }	 
		}//method main
} //class Roulette
 
 
//*****************************************************************
// Class Player represents one roulette player.
//*****************************************************************
 
class Player {
	 public static int bet,ballPosition,money;
	 private static BufferedReader stdin;
	 private static int betType;
	 public int yourSpin; 
	 private int totalpay;
	 public static int totalwinnings, winnings, yourNumber;
   
		 
	 //=============================================================
	 // 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.");
     	   yourNumber = Integer.parseInt (stdin.readLine());} 
     	  
     	      	      	 
     	   yourSpin = Wheel.spin();
     	   totalpay = Player.payment();
     	  
     	   
     }//method makeBet
    
      public static int payment(){
     	  	   
        totalwinnings = ((Wheel.numberPayoff(bet,betType,ballPosition,yourNumber,winnings)) + (Wheel.redPayoff(bet,betType,ballPosition,winnings))+ (Wheel.blackPayoff(bet,betType,ballPosition,winnings)));
     
     	System.out.println ("Total payoff is " + totalwinnings);
            
     	System.out.println ();
        money = money + totalwinnings;
     	return money;
                                	    	      	
     } //method payment      
         
    //==============================================================
    // 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 {
  	
  	private final static int RED = 1;
  	private final static int BLACK = 2;
  	private final static int POSITIONS = 37;
    private static int ballPosition;
  	private static int color;  
  
 
  	public static int spin()
  	{
  		
  	 int ballPosition = (int) (Math.random() * (POSITIONS - 1));
  	 
  	   if (ballPosition%2 == 0) {
  	   	 color = RED;
  	   	 System.out.println ("Color is RED");
  	     System.out.println ("Spin is: " + ballPosition);}    	   
  	   else  {
  	   	 color = BLACK;
  	   	 System.out.println ("Color is BLACK"); 
  	     System.out.println ("Spin is: " + ballPosition);}
  	     
       return ballPosition;  	  
      }  
            
    public static int numberPayoff ( int bet, int betType, int ballPosition, int yourNumber, int winnings){ 
        
      if (betType == 3 && ballPosition == yourNumber)
          winnings = bet * 36 + bet;        
           
     	return winnings;      
     }	
     
     public static int redPayoff (int bet, int betType, int ballPosition,int winnings){
         
         if (betType == 1 && color == 1)
           winnings = bet * 2 + bet;
                     
       return winnings; 
      } 
      
      public static int blackPayoff (int bet, int betType, int ballPosition,int winnings){
         
         if (betType == 2 && color == 2)
           winnings = bet * 2 + bet;
                     
       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	


Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: One last Roulette snag Posted: Mar 11, 2002 1:36 PM
Reply to this message Reply
Nevermind. I got it. Talk to you later.

Flat View: This topic has 1 reply on 1 page
Topic: swing Previous Topic   Next Topic Topic: can anyone help to find the error in the code

Sponsored Links



Google
  Web Artima.com   

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