Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Collections;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.text.DecimalFormat;
- import java.text.NumberFormat; // use with Currency
- import java.util.Locale; // US, UK etc
- public class DealOrNoDeal {
- /**
- * Program: DealOrNoDeal.java
- * Purpose: Game: choose 22 boxes containing various amounts of money
- * Creator: Chris Clarke
- * Created: 18.06.2014
- * Note: If you like this code, find lots more in the books
- * "50 Java Program Source Codes" and "50 More Java Source Codes"
- * available in paperback and e-book - download now from Amazon.
- */
- private static final int NUM_BOXES = 22;
- private static HashMap<Integer, Double> boxes;
- private static HashMap<Integer, Double> boxesInOrder;
- private static Scanner in;
- private static DecimalFormat df;
- private static NumberFormat currency;
- public static void main(String[] args) {
- DealOrNoDeal dnd = new DealOrNoDeal();
- boxes = new HashMap<Integer, Double>();
- boxesInOrder = new HashMap<Integer, Double>();
- in = new Scanner(System.in);
- currency = NumberFormat.getCurrencyInstance( Locale.UK);
- df = new DecimalFormat("GBP £#,###.00");
- boolean[] chosen = new boolean[NUM_BOXES+1];
- boolean[] available = new boolean[NUM_BOXES+1];
- int box;
- int n=1;
- int myBox;
- dnd.initBoxes();
- boxesInOrder = boxes;
- boxes = dnd.randomiseBoxes();
- for (int i=0; i<NUM_BOXES; i++) {
- available[i+1] = true;
- } // end for
- System.out.println("Choose your box (1-"+NUM_BOXES+")");
- do {
- myBox = in.nextInt();
- if (myBox<1 || myBox>NUM_BOXES) {
- System.out.println("Must be in range [1.."+NUM_BOXES+"]");
- } // end if
- } while (myBox<1 || myBox>NUM_BOXES);
- chosen[myBox] = true;
- System.out.println("Let\'s play Deal or No Deal!");
- dnd.displayAmounts( available);
- for (int i=0; i<NUM_BOXES; i++) {
- if (i==NUM_BOXES-1) {
- System.out.println("You walk away with "+df.format( boxes.get(myBox)));
- return;
- } // end if
- System.out.println("Please choose a box:");
- // display available boxes
- dnd.displayBoxes(chosen);
- box = dnd.getBox(chosen);
- chosen[box] = true;
- for (int j=1; j<=NUM_BOXES; j++) {
- if (boxesInOrder.get(j) == boxes.get(box)) {
- available[j] = false;
- } // end if
- } // end for
- //System.out.println(box+" = "+df.format( boxes.get(box)));
- System.out.println( currency.format( boxes.get(box)));
- dnd.displayAmounts( available);
- if (n%3 == 0 && i<NUM_BOXES-2) {
- if (dnd.bankerOffer(chosen)) {
- System.out.println("Thanks for playing \"Deal or No Deal\".");
- return;
- } else {
- System.out.println("Let\'s play another round!");
- } // end if
- } // end if
- n++;
- } // end for
- } // end main()
- public int getBox(boolean[] chosen) {
- int box;
- do {
- // get number from user
- box = in.nextInt();
- if (box<1 || box>NUM_BOXES) {
- System.out.println("Box out of range [1.."+NUM_BOXES+"]");
- } else if (chosen[box]) {
- System.out.println("Box "+box+" already chosen!");
- } // end if
- } while (box<1 || box>NUM_BOXES || chosen[box]);
- return box;
- } // end getBox
- public boolean bankerOffer(boolean[] chosen) {
- double total = 0.0f;
- double offer = 0.0f;
- int n = 0;
- String deal = "";
- for (int i=0; i<NUM_BOXES; i++) {
- if (!chosen[i+1]) {
- total += boxes.get(i+1);
- n++;
- } // end if
- } // end for
- offer = 0.25 * (total / n);
- System.out.print("Banker offers "+df.format(offer) + ". Deal or no deal? (d/n) ");
- deal = in.next().toLowerCase();
- if (deal.startsWith("d")) {
- System.out.println("Player dealt at "+df.format(offer));
- return true;
- } else {
- System.out.println("No deal!");
- return false;
- } // end if
- } // end bankerOffer()
- public void displayBoxes(boolean[] chosen) {
- for (int i=0; i<NUM_BOXES; i++) {
- if (!chosen[i+1]) {
- System.out.print((i+1)+" ");
- } // end if
- } // end for
- System.out.print("?? ");
- } // end displayBoxes()
- public void displayAmounts(boolean[] avail) {
- for (int i=0; i<NUM_BOXES; i++) {
- if (avail[i+1]) {
- System.out.println( df.format( boxesInOrder.get( i+1)));
- } // end if
- } // end for
- } // end displayAmounts()
- public void initBoxes() {
- int c = 1;
- boxes.put(c++, 0.01); // a penny
- boxes.put(c++, 0.10);
- boxes.put(c++, 0.50);
- boxes.put(c++, 1.00);
- boxes.put(c++, 5.00);
- boxes.put(c++, 10.00);
- boxes.put(c++, 50.00);
- boxes.put(c++, 100.00);
- boxes.put(c++, 250.00);
- boxes.put(c++, 500.00);
- boxes.put(c++, 750.00);
- boxes.put(c++, 1000.00);
- boxes.put(c++, 2000.00);
- boxes.put(c++, 3000.00);
- boxes.put(c++, 5000.00);
- boxes.put(c++, 10000.00);
- boxes.put(c++, 20000.00);
- boxes.put(c++, 35000.00);
- boxes.put(c++, 50000.00);
- boxes.put(c++, 75000.00);
- boxes.put(c++, 100000.00);
- boxes.put(c++, 250000.00); // £250,000
- } // end initBoxes()
- public HashMap<Integer, Double> randomiseBoxes() {
- List<Integer> li = new ArrayList<Integer>();
- HashMap<Integer, Double> boxCopy = new HashMap<Integer, Double>();
- // add numbers 1 to 22 to list
- for (int i=0; i<NUM_BOXES; i++) {
- li.add(i+1);
- } // end for
- // shuffle list
- Collections.shuffle(li);
- for (int i=0; i<NUM_BOXES; i++) {
- boxCopy.put((i+1), boxes.get( li.get(i)));
- } // end for
- return boxCopy;
- } // end randomiseBoxes()
- } // end class DealOrNoDeal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement