package casino; import java.util.Scanner; // Programmer: Rance Cleaveland // Date: 10/27/2006 // // This class implements a standalone executable for a simple casino. // There are two games in the casino: dice, and roulette. public class Casino { /** * @param args */ // An abbreviation of the long-winded System.out.print private static void print (String s){ System.out.print (s); } // An abbreviation of the long-winded System.out.println private static void println (String s){ System.out.println (s); } // A polymorphic game-playing routine. The int returned is the payoff // (positive) or loss (negative). public static int play (Scanner sc, Game g){ String[] betTypes = g.getValidBets(); // Allowed types of bets String validBets = betTypes[0]; // Concatenated string of allowed bets String betType; // Type of bet placed by user // Build validBets string for (int i=1; i < betTypes.length; i++) validBets += " " + betTypes[i]; // Determine the kind of bet to be placed. Do not allow an invalid // bet to be placed. println ("What kind of bet would you like to place? "); for (;;) { print ("Please enter one of " + validBets + ": "); betType = sc.next(); if (g.isValidBet(betType)) break; else println("Invalid bet. "); } // Get the bet amount. Make sure it is positive. println ("How many dollars would you like to bet? "); int amt; for (;;) { amt = sc.nextInt (); if (amt > 0) break; else print ("Please enter a positive amount: "); } // Now set the bet, bet amount and play the game g.setBet(betType); g.setBetAmount(amt); return (g.play()); } public static void main(String[] args) { // Create games to be played Scanner sc = new Scanner (System.in); Dice dice = new Dice (); Roulette roulette = new Roulette (); int payoff = 0; String names = dice.getName() + " or " + roulette.getName(); // Welcome println("Welcome to the casino!\n"); for (;;) { // Prompt for game println("What game would you like to play, " + names + "? "); String game = sc.next(); while (!game.equals(dice.getName()) && !game.equals(roulette.getName())){ print ("Please enter " + names + ": "); game = sc.next(); } // Play appropriate game if (game.equals(dice.getName())) payoff = play(sc, dice); else payoff = play(sc, roulette); // Report the payoff if (payoff == 0) println ("There was a push; you neither won nor lost."); else if (payoff < 0) println ("You lost $" + -payoff + "."); else println ("You won $" + payoff + "."); // Continue? print("Play another game? [yn] "); String response = sc.next(); while (!response.equals("y") && !response.equals("n")){ println ("Please enter y or n."); response = sc.next(); } if (response.equals("n")) break; } println ("Good bye. Thanks for playing."); } }