Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package osu.cse1223;
- /*
- * Project07.java
- *
- * A program that plays the dice game high/low
- * Used to practice breaking code up into methods.
- *
- * @author Jeff Casavant
- *
- */
- import java.util.Scanner;
- public class HighLow {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("Welcome to the High-Low table.");
- int money = 100;
- int bet = 1;
- char hls = 'h';
- int roll = 7;
- int win = 0;
- while(bet>0&&money>0){
- bet = getBet(in, money);
- hls = getHighLow(in);
- roll = getRoll();
- roll += getRoll();
- win = determineWinnings(hls,bet,roll);
- System.out.println("The roll: " + roll + ". Your bet: " + hls +". Your winnings: " + win + ".");
- money += win;
- }
- System.out.println("Your final worth: " + money + ". Profit: " + (money - 100) + ".");
- System.out.println("Thanks for playing!");
- }
- // Given a Scanner and a current maximum amount of money, prompt the user for
- // an integer representing the number of dollars that they want to bet. This
- // number must be between 0 and to maximum number of dollars. If the user enters
- // a number that is out of bounds, display an error message and ask again.
- // Return the bet to the calling program.
- private static int getBet(Scanner in, int currentPool) {
- System.out.println("Your total money stands at " + currentPool + ". Bet?");
- int bet = in.nextInt();
- in.nextLine();
- while(! (bet<=currentPool)){
- System.out.println("You haven't got that much to bet. Bet?");
- bet = in.nextInt();
- in.nextLine();
- }
- return bet;
- }
- // Given a Scanner, prompt the user for a single character indicating whether they
- // would like to bet High ('H'), Low ('L') or Sevens ('S'). Your code should accept
- // either capital or lowercase answers, but should display an error if the user attempts
- // to enter anything but one of these 3 values and prompt for a valid answer.
- // Return the character to the calling program.
- private static char getHighLow(Scanner in) {
- System.out.println("High, low, or sevens? (h,l,s)");
- char hls = in.nextLine().charAt(0);
- hls = Character.toLowerCase(hls);
- while(!(hls == 'h' || hls == 'l' || hls == 's')){
- System.out.println("Sorry, don't understand. High, low, or sevens? (h,l,s)");
- hls = in.nextLine().charAt(0);
- }
- return hls;
- }
- // Produce a random roll of a single six-sided die and return that value to the calling
- // program
- private static int getRoll() {
- return (int)(Math.random()*5)+1;
- }
- // Given the choice of high, low or sevens, the player's bet and the total result of
- // the roll of the dice, determine how much the player has won. If the player loses
- // the bet then winnings should be negative. If the player wins, the winnings should
- // be equal to the bet if the choice is High or Low and 4 times the bet if the choice
- // was Sevens. Return the winnings to the calling program.
- private static int determineWinnings(char highLow, int bet, int roll) {
- if((roll>8 && highLow == 'h')||(roll<6 && highLow == 'l')||(roll==7 && highLow == 's')){
- if(roll == 7){
- return bet * 4;
- } else {
- return bet;
- }
- } else {
- return -1 * bet;
- }
- }
- }
Add Comment
Please, Sign In to add comment