MnMWizard icon

Deal or no deal final day

MnMWizard | PRO | 12/14/17 10:17:58 PM UTC | 0 ⭐ | 411 👁️ | Never ⏰ | []
text |

5.05 KB

|

None

|

0 👍

/

0 👎

//Mason Marnell
// Cases class here, main class down below
  public class Cases {
	private int[] cases;
 	public Cases(){
		cases = new int[27];
		cases[0] = 0;
		cases[1] = 1;
		cases[2] = 2;
		cases[3] = 5;
		cases[4] = 10;
		cases[5] = 25;
		cases[6] = 50;
		cases[7] = 75;
		cases[8] = 100;
		cases[9] = 200;
		cases[10] = 300;
		cases[11] = 400;
		cases[12] = 500;
		cases[13] = 750;
		cases[14] = 1000;
		cases[15] = 5000;
		cases[16] = 10000;
		cases[17] = 25000;
		cases[18] = 50000;
		cases[19] = 75000;
		cases[20] = 100000;
		cases[21] = 200000;
		cases[22] = 300000;
		cases[23] = 400000;
		cases[24] = 500000;
		cases[25] = 750000;
		cases[26] = 1000000;
  	}
   	public int numcasesleft(){
		int count = 0;
		for (int i = 1; i < cases.length; i++) {
			if(cases[i] != 0){
				count++;
			}
 		}
		return count;
	}
 	public String[] casesleft(){
		int left = numcasesleft();
		String [] toRet = new String[left];
		int location = 0;
		for (int o = 1; o < cases.length; o++) {
			if(cases[o] != 0){
				toRet[location] = o + "";
				location++;
			}
		}
		return toRet;
	}
 	public String valuesleft(){
		String toRet = "";
		for (int p = 1; p < cases.length; p++) {
			if(cases[p] != 0){
				toRet = toRet + cases[p] + " ";
			}
		}
		return toRet;
	}
 	public String toString(){
		String temp = "";
		for (int i = 0; i < cases.length; i++) {
			temp += cases[i] + " ";
		}
		return temp;
	}
 	public void swap(int A, int B){
		int temp = cases[A];
		cases[A] = cases[B];
		cases[B] = temp;
	}
	public int location(int number){
		String [] location = casesleft();
		String val = location[number];
		int intval = Integer.parseInt(val.trim());
		return intval;
	}
 	public int ValueOfCase(int location){
		return cases[location];
	}
 	public void findvalue(int value){
		for (int t = 0; t < cases.length; t++) {
			if(cases[t] == value){
				cases[t] = 0;
				break;
			}
 		}
	}
 	public void shuffle(){
		for (int i = 1; i < cases.length; i++) {
			int newlocation = (int)(Math.random() * (cases.length - 1)) + 1;
			swap(i,newlocation);
		}
	}
 	public void setLocation(int location){
		cases[location] = 0;
	}
 	public int SumOfValue(){
		int sum = 0;
 		for (int i = 0; i < cases.length; i++) {
			sum = sum + cases[i];
		}
 		return sum;
	}
 	public double average(){
		int sum = SumOfValue();
		int count = this.numcasesleft();
			return sum / count;
	}
 	public int midVal(){
		int value = 0;
		int spot = numcasesleft() / 2;
		int count = 0;
		for (int i = 0; i < cases.length; i++) {
			if(cases[i] > 0){
				count++;
			}
			if(count >= spot){
				value = cases[i];
				break;
			}
 		}
		return value;
	}
 	//public static void main(String[] args) {
		// TODO Auto-generated method stub
 	//}
 }
 //-------------------------------------------------------------------------------------------------------------------
// Main class starts
 import javax.swing.JOptionPane;
 public class DealOrNoDealMain {
 	public static void pickfirstcase(Cases mycase, Cases valuecases){
		String t[] = mycase.casesleft();
		String values = valuecases.valuesleft();
		int n = JOptionPane.showOptionDialog(null,
				values,
				"what case do you want",
				JOptionPane.YES_NO_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null, t, t[1]);
		mycase.swap(n+1, 0);
	}
	public static void pickcases(Cases mycase, Cases valuecases){
		for (int r = 0; r < casestopick; r++) {		
		String t[] = mycase.casesleft();
		String values = valuecases.valuesleft();
		int n = JOptionPane.showOptionDialog(null,
				values,
				"what case do you want, " + (casestopick - r),
				JOptionPane.YES_NO_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null, t, t[1]);
		int location = mycase.location(n);
		int value = mycase.ValueOfCase(location);
		JOptionPane.showMessageDialog(null, "The case has " + value);
		mycase.setLocation(location);
		valuecases.findvalue(value);
		}
	}
	public static int casestopick = 6;
	public static void main(String[] args) {
		Cases test = new Cases();
		test.shuffle();
		Cases test2 = new Cases();
 		pickfirstcase(test,test2);
		boolean choose = true;
		int value = 0;
		while(choose){
			pickcases(test,test2);
			value = test2.midVal() + (int)test2.average() / 2;
			int ch = JOptionPane.showConfirmDialog(null, "Your offer is " + value + ". Deal or No Deal?");
			if(ch ==0){
				choose = false;
			}
			if(casestopick > 1){
				casestopick = casestopick - 1;
			}
			if(test.numcasesleft() <= 2){
				choose = false;
			}
 		}
		int gob = test.ValueOfCase(0);
		int ta = 131448; // average of all the array numbers, not sure if I did the extra credit part right but it works
		JOptionPane.showMessageDialog(null, "Your case had " + gob);
		if(gob < ta){
			JOptionPane.showMessageDialog(null, "Your case was low, so you guessed good!  :)");
		}
		if(gob > ta){
			JOptionPane.showMessageDialog(null, "Your case was high, so that's statistically bad...  :(");
		}
 	}
 }

Comments