Guest User

Untitled

a guest
Jan 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package osu.cse1223;
  2.  
  3. /*
  4.  * Project07.java
  5.  *
  6.  *   A program that plays the dice game high/low
  7.  *   Used to practice breaking code up into methods.
  8.  *
  9.  * @author Jeff Casavant
  10.  *
  11.  */
  12.  
  13. import java.util.Scanner;
  14.  
  15.  
  16. public class HighLow {
  17.  
  18.  
  19.     public static void main(String[] args) {
  20.         Scanner in = new Scanner(System.in);
  21.         System.out.println("Welcome to the High-Low table.");
  22.         int money = 100;
  23.         int bet = 1;
  24.         char hls = 'h';
  25.         int roll = 7;
  26.         int win = 0;
  27.         while(bet>0&&money>0){
  28.             bet = getBet(in, money);
  29.             hls = getHighLow(in);
  30.             roll = getRoll();
  31.             roll += getRoll();
  32.             win = determineWinnings(hls,bet,roll);
  33.             System.out.println("The roll: " + roll + ".  Your bet: " + hls +". Your winnings: " + win + ".");
  34.             money += win;
  35.         }
  36.         System.out.println("Your final worth: " + money + ".  Profit: " + (money - 100) + ".");
  37.         System.out.println("Thanks for playing!");
  38.     }
  39.    
  40.    
  41.     // Given a Scanner and a current maximum amount of money, prompt the user for
  42.     // an integer representing the number of dollars that they want to bet.  This
  43.     // number must be between 0 and to maximum number of dollars.  If the user enters
  44.     // a number that is out of bounds, display an error message and ask again.
  45.     // Return the bet to the calling program.
  46.     private static int getBet(Scanner in, int currentPool) {
  47.         System.out.println("Your total money stands at " + currentPool + ".  Bet?");
  48.         int bet = in.nextInt();
  49.         in.nextLine();
  50.         while(! (bet<=currentPool)){
  51.             System.out.println("You haven't got that much to bet.  Bet?");
  52.             bet = in.nextInt();
  53.             in.nextLine();
  54.         }
  55.         return bet;
  56.     }
  57.    
  58.     // Given a Scanner, prompt the user for a single character indicating whether they
  59.     // would like to bet High ('H'), Low ('L') or Sevens ('S').  Your code should accept
  60.     // either capital or lowercase answers, but should display an error if the user attempts
  61.     // to enter anything but one of these 3 values and prompt for a valid answer.
  62.     // Return the character to the calling program.
  63.     private static char getHighLow(Scanner in) {
  64.         System.out.println("High, low, or sevens? (h,l,s)");
  65.         char hls = in.nextLine().charAt(0);
  66.         hls = Character.toLowerCase(hls);
  67.         while(!(hls == 'h' || hls == 'l' || hls == 's')){
  68.             System.out.println("Sorry, don't understand.  High, low, or sevens? (h,l,s)");
  69.             hls = in.nextLine().charAt(0);
  70.         }
  71.         return hls;
  72.         }
  73.    
  74.     // Produce a random roll of a single six-sided die and return that value to the calling
  75.     // program
  76.     private static int getRoll() {
  77.         return (int)(Math.random()*5)+1;
  78.     }
  79.    
  80.     // Given the choice of high, low or sevens, the player's bet and the total result of
  81.     // the roll of the dice, determine how much the player has won.  If the player loses
  82.     // the bet then winnings should be negative.  If the player wins, the winnings should
  83.     // be equal to the bet if the choice is High or Low and 4 times the bet if the choice
  84.     // was Sevens.  Return the winnings to the calling program.
  85.     private static int determineWinnings(char highLow, int bet, int roll) {
  86.         if((roll>8 && highLow == 'h')||(roll<6 && highLow == 'l')||(roll==7 && highLow == 's')){
  87.             if(roll == 7){
  88.                 return bet * 4;
  89.             } else {
  90.                 return bet;
  91.             }
  92.         } else {
  93.             return -1 * bet;
  94.         }
  95.     }
  96.  
  97. }
Add Comment
Please, Sign In to add comment