View Single Post
  #2 (permalink)  
Old 12-18-2007, 02:57 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.io.*; import java.util.Random; public class CasinoTest { /* Player p = new Player(); Chips c = new Chips(); Games g = new Games(); Statistics s = new Statistics(); */ private static BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in ) ); public static void main ( String [] args ) throws IOException { // For minimal changes you can pass some references around. Player p = new Player(); Chips c = new Chips(p); Games g = new Games(p, c); Statistics s = new Statistics(p); byte opt = 0; System.out.println( "Welcome to the Casino!" ); do{ if( p.numchips == 0 && p.cash < 11) { break; } System.out.println( "You currently have " + p.cash + " to spend" ); System.out.println( "What would you like to do?" ); System.out.println( "\n\t1. Buy Chips\n\t2. Sell Chips\n\t3. Status Report" ); System.out.println( "\t4. Play Craps\n\t5. Play Arup's Dice\n\t6. Cash in and Leave" ); opt = Byte.parseByte( stdin.readLine() ); switch( opt ) { case 1: c.Buy(); break; case 2: c.Sell(); break; case 3: s.Status(); break; case 4: g.Craps(); break; case 5: g.ArupsDice(); break; } }while ( opt != 6 ); p.cash += p.numchips * 10; p.numchips = 0; System.out.println( "Upon leaving, you have $" + p.cash ); } } class Player { int cash = 1000, numchips = 0; } class Chips { Player p; Chips(Player p) { this.p = p; } private static BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in ) ); short count = 0; int r1 = 0, r2 = 0; String roll = null; Random generator = new Random( 8675309 ); void Buy() throws IOException { System.out.println( "Chips cost $11.00 apiece. You have " + p.cash + " dollars." ); System.out.println( "How many would you like to purchase?" ); count = Short.parseShort( stdin.readLine() ); if( p.cash >= count * 11 ) { p.cash -= count * 11; p.numchips += count; return; } if( p.cash <= count * 11 ) { System.out.println( "I'm sorry, but you cannot afford that many." ); return; } if( count == 0 ) { System.out.println( "You have decided not to purchase any chips." ); return; } } void Sell() throws IOException { System.out.println( "Chips may be sold or $10.00 apiece. You have " + p.numchips + " chips." ); System.out.println( "How many would you like to sell?" ); count = Short.parseShort( stdin.readLine() ); if( count == 0 ) { System.out.println( "You have decided not to sell any chips." ); return; } if( count <= p.numchips ) { p.numchips -= count; p.cash += count * 10; return; } if( count > p.numchips ) { System.out.println( "I'm sorry, but you don't have that many." ); return; } return; } byte Wager() throws IOException { System.out.println( "How many chips would you like to wager?" ); count = Short.parseShort( stdin.readLine() ); if( count == 0 ) { System.out.println( "Sorry, but you cannot play for free." ); return 0; } if( count <= p.numchips ) { // You might consider increasing the precision of // the return type (above byte) to avoid overflow. return (byte)count; } if( count > p.numchips ) { System.out.println( "I'm sorry, but you don't have that many." ); return 0; } return 0; } byte Dice() throws IOException { r1 = generator.nextInt( 6 ) + 1; System.out.println( "Press enter to roll." ); roll = stdin.readLine(); //Time between calls to random number generator r2 = generator.nextInt( 6 ) + 1; return ( byte ) ( r1 + r2 ); } } class Games { Player p; Chips c; Games(Player p, Chips c) { this.p = p; this.c = c; } private static BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in ) ); byte temp = 0, point = 0, wager = 0; boolean wins = false; void Craps() throws IOException { wager = c.Wager(); if( wager == 0 ) { return; } temp = c.Dice(); System.out.println( "You rolled " + temp ); if( temp == 7 || temp == 11 ) { System.out.println( "You win!" ); p.numchips += wager; return; } if( temp == 2 || temp == 3 || temp == 12 ) { System.out.println( "I'm sorry, but you lost." ); p.numchips -= wager; return; } else { point = temp; System.out.println( "Your point is " + point + ". Roll again."); do{ temp = c.Dice(); System.out.println( "You rolled " + temp + "." ); if( temp == point ) { System.out.println( "You win!" ); p.numchips += wager; return; } if( temp == 7 ) { System.out.println( "I'm sorry, but you lost." ); p.numchips -= wager; return; } else { System.out.println( "Please roll again." ); } }while( temp != 7 && temp != point ); } return; } void ArupsDice() throws IOException { wager = c.Wager(); if( wager == 0 ) { return; } temp = c.Dice(); System.out.println( "You rolled " + temp + "." ); if( temp == 12 || temp == 11 ) { System.out.println( "You win!" ); p.numchips += wager; return; } if( temp == 2 ) { System.out.println( "I'm sorry, but you lost." ); p.numchips -= wager; return; } else { point = temp; System.out.println( "Your point is " + point + ". Roll again." ); temp = c.Dice(); System.out.println( "You rolled " + temp + "." ); if( temp > point ) { System.out.println( "You win!" ); p.numchips += wager; return; } if( temp <= point ) { System.out.println( "I'm sorry, but you lost." ); p.numchips -= wager; return; } return; } } } class Statistics { Player p; Statistics(Player p) { this.p = p; } void Status() { System.out.println( "Status Report:\n-----------------" ); System.out.println( " Chips: " + p.numchips ); System.out.println( " Cash: " + p.cash ); System.out.println( "-----------------" ); return; } }
Reply With Quote