Advertisement
brilliant_moves

DealOrNoDeal.java

Jun 20th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.44 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. import java.text.DecimalFormat;
  7. import java.text.NumberFormat;  // use with Currency
  8. import java.util.Locale;    // US, UK etc
  9.  
  10. public class DealOrNoDeal {
  11.  
  12.     /**
  13.     *   Program:    DealOrNoDeal.java
  14.     *   Purpose:    Game: choose 22 boxes containing various amounts of money  
  15.     *   Creator:    Chris Clarke
  16.     *   Created:    18.06.2014
  17.     *      Note:    If you like this code, find lots more in the books
  18.     *           "50 Java Program Source Codes" and "50 More Java Source Codes"
  19.     *           available in paperback and e-book - download now from Amazon.
  20.     */
  21.  
  22.     private static final int NUM_BOXES = 22;
  23.  
  24.     private static HashMap<Integer, Double> boxes;
  25.     private static HashMap<Integer, Double> boxesInOrder;
  26.  
  27.     private static Scanner in;
  28.     private static DecimalFormat df;
  29.     private static NumberFormat currency;
  30.  
  31.     public static void main(String[] args) {
  32.         DealOrNoDeal dnd = new DealOrNoDeal();
  33.         boxes = new HashMap<Integer, Double>();
  34.         boxesInOrder = new HashMap<Integer, Double>();
  35.         in = new Scanner(System.in);
  36.         currency = NumberFormat.getCurrencyInstance( Locale.UK);
  37.         df = new DecimalFormat("GBP £#,###.00");
  38.         boolean[] chosen = new boolean[NUM_BOXES+1];
  39.         boolean[] available = new boolean[NUM_BOXES+1];
  40.         int box;
  41.         int n=1;
  42.         int myBox;
  43.  
  44.         dnd.initBoxes();
  45.         boxesInOrder = boxes;
  46.         boxes = dnd.randomiseBoxes();
  47.  
  48.         for (int i=0; i<NUM_BOXES; i++) {
  49.             available[i+1] = true;
  50.         } // end for
  51.  
  52.         System.out.println("Choose your box (1-"+NUM_BOXES+")");
  53.         do {
  54.             myBox = in.nextInt();
  55.             if (myBox<1 || myBox>NUM_BOXES) {
  56.                 System.out.println("Must be in range [1.."+NUM_BOXES+"]");
  57.             } // end if
  58.         } while (myBox<1 || myBox>NUM_BOXES);
  59.         chosen[myBox] = true;
  60.  
  61.         System.out.println("Let\'s play Deal or No Deal!");
  62.         dnd.displayAmounts( available);
  63.  
  64.         for (int i=0; i<NUM_BOXES; i++) {
  65.             if (i==NUM_BOXES-1) {
  66.                 System.out.println("You walk away with "+df.format( boxes.get(myBox)));
  67.                 return;
  68.             } // end if
  69.  
  70.             System.out.println("Please choose a box:");
  71.             // display available boxes
  72.             dnd.displayBoxes(chosen);
  73.             box = dnd.getBox(chosen);
  74.  
  75.             chosen[box] = true;
  76.             for (int j=1; j<=NUM_BOXES; j++) {
  77.                 if (boxesInOrder.get(j) == boxes.get(box)) {
  78.                     available[j] = false;
  79.                 } // end if
  80.             } // end for
  81.  
  82.             //System.out.println(box+" = "+df.format( boxes.get(box)));
  83.             System.out.println( currency.format( boxes.get(box)));
  84.             dnd.displayAmounts( available);
  85.  
  86.             if (n%3 == 0 && i<NUM_BOXES-2) {
  87.                 if (dnd.bankerOffer(chosen)) {
  88.                     System.out.println("Thanks for playing \"Deal or No Deal\".");
  89.                     return;
  90.                 } else {
  91.                     System.out.println("Let\'s play another round!");
  92.                 } // end if
  93.             } // end if
  94.             n++;
  95.         } // end for
  96.     } // end main()
  97.  
  98.     public int getBox(boolean[] chosen) {
  99.         int box;
  100.         do {
  101.             // get number from user
  102.             box = in.nextInt();
  103.             if (box<1 || box>NUM_BOXES) {
  104.                 System.out.println("Box out of range [1.."+NUM_BOXES+"]");
  105.             } else if (chosen[box]) {
  106.                 System.out.println("Box "+box+" already chosen!");
  107.             } // end if
  108.         } while (box<1 || box>NUM_BOXES || chosen[box]);
  109.         return box;
  110.     } // end getBox
  111.  
  112.     public boolean bankerOffer(boolean[] chosen) {
  113.         double total = 0.0f;
  114.         double offer = 0.0f;
  115.         int n = 0;
  116.         String deal = "";
  117.  
  118.         for (int i=0; i<NUM_BOXES; i++) {
  119.             if (!chosen[i+1]) {
  120.                 total += boxes.get(i+1);
  121.                 n++;
  122.             } // end if
  123.         } // end for
  124.  
  125.         offer = 0.25 * (total / n);
  126.  
  127.         System.out.print("Banker offers "+df.format(offer) + ". Deal or no deal? (d/n) ");
  128.         deal = in.next().toLowerCase();
  129.  
  130.         if (deal.startsWith("d")) {
  131.             System.out.println("Player dealt at "+df.format(offer));
  132.             return true;
  133.         } else {
  134.             System.out.println("No deal!");
  135.             return false;
  136.         } // end if
  137.     } // end bankerOffer()
  138.  
  139.     public void displayBoxes(boolean[] chosen) {
  140.         for (int i=0; i<NUM_BOXES; i++) {
  141.             if (!chosen[i+1]) {
  142.                 System.out.print((i+1)+" ");
  143.             } // end if
  144.         } // end for
  145.         System.out.print("?? ");
  146.     } // end displayBoxes()
  147.  
  148.     public void displayAmounts(boolean[] avail) {
  149.         for (int i=0; i<NUM_BOXES; i++) {
  150.             if (avail[i+1]) {
  151.                 System.out.println( df.format( boxesInOrder.get( i+1)));
  152.             } // end if
  153.         } // end for
  154.     } // end displayAmounts()
  155.  
  156.     public void initBoxes() {
  157.         int c = 1;
  158.         boxes.put(c++, 0.01); // a penny
  159.         boxes.put(c++, 0.10);
  160.         boxes.put(c++, 0.50);
  161.         boxes.put(c++, 1.00);
  162.         boxes.put(c++, 5.00);
  163.         boxes.put(c++, 10.00);
  164.         boxes.put(c++, 50.00);
  165.         boxes.put(c++, 100.00);
  166.         boxes.put(c++, 250.00);
  167.         boxes.put(c++, 500.00);
  168.         boxes.put(c++, 750.00);
  169.  
  170.         boxes.put(c++, 1000.00);
  171.         boxes.put(c++, 2000.00);
  172.         boxes.put(c++, 3000.00);
  173.         boxes.put(c++, 5000.00);
  174.         boxes.put(c++, 10000.00);
  175.         boxes.put(c++, 20000.00);
  176.         boxes.put(c++, 35000.00);
  177.         boxes.put(c++, 50000.00);
  178.         boxes.put(c++, 75000.00);
  179.         boxes.put(c++, 100000.00);
  180.         boxes.put(c++, 250000.00); // £250,000
  181.     } // end initBoxes()
  182.  
  183.     public HashMap<Integer, Double> randomiseBoxes() {
  184.         List<Integer> li = new ArrayList<Integer>();
  185.         HashMap<Integer, Double> boxCopy = new HashMap<Integer, Double>();
  186.  
  187.         // add numbers 1 to 22 to list
  188.         for (int i=0; i<NUM_BOXES; i++) {
  189.             li.add(i+1);
  190.         } // end for
  191.  
  192.         // shuffle list
  193.         Collections.shuffle(li);
  194.  
  195.         for (int i=0; i<NUM_BOXES; i++) {
  196.             boxCopy.put((i+1), boxes.get( li.get(i)));
  197.         } // end for
  198.  
  199.         return boxCopy;
  200.  
  201.     } // end randomiseBoxes()
  202.  
  203. } // end class DealOrNoDeal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement