import java.applet.*; import java.awt.*; import java.awt.event.*; public class dice_results extends Applet implements ActionListener { static final int TYPES = 53; Choice[] scoringChoice; Choice[] targetChoice; Button calculateButton; Label answerLabel; static final int WRONG = -1000; public void init() { Font font = new Font("SansSerif", Font.PLAIN, 18); setBackground(new Color(158, 204, 117)); Panel scoringPanel = new Panel(); add(scoringPanel); Label scoringLabel = new Label ("Scoring:"); scoringLabel.setFont(font); scoringPanel.add(scoringLabel); scoringChoice = new Choice[3]; for (int i=0; i<3; i++) { scoringChoice[i] = new Choice(); scoringChoice[i].setFont(font); for (int j=1; j<7; j++) scoringChoice[i].addItem(String.valueOf(j)); scoringPanel.add(scoringChoice[i]); } Panel targetPanel = new Panel(); add(targetPanel); Label targetLabel = new Label ("Target:"); targetLabel.setFont(font); targetPanel.add(targetLabel); targetChoice = new Choice[2]; for (int i=0; i<2; i++) { targetChoice[i] = new Choice(); targetChoice[i].setFont(font); for (int j=1; j<13; j++) targetChoice[i].addItem(String.valueOf(j)); targetPanel.add(targetChoice[i]); } calculateButton = new Button("Calculate best combo"); calculateButton.setFont(font); add(calculateButton); calculateButton.addActionListener(this); answerLabel = new Label("Answer will appear here"); answerLabel.setFont(font); add(answerLabel); } String getBestValueString(int a, int b, int c, int target) { int best = 0; int thisValue = 0; for (int typeNum=0; typeNum 0.000001) return (WRONG); return (intAnswer); } double doubleValue(double a, double b, double c, int typeNum) { switch (typeNum) { // 21 pairings of two of the 6 types of operations, each has maximum of 4 groupings reduced by redundancies case 0: return (a + b + c); // ++ case 1: return (a + b - c); // +- case 2: return (a*(b + c)); // +* case 3: return (a*b + c); // +* case 4: return ((a + b)/c); // +/ case 5: return (a + b/c); // +/ case 6: return (a / (b+c)); // +/ case 7: return (Math.pow(a, b+c)); // +p case 8: return (Math.pow(a+b, c)); // +p case 9: return (a + Math.pow (b, c)); // +p case 10: return (Math.pow(a+b, 1/c)); // +r case 11: return (a + Math.pow(b, 1/c)); // +r case 12: return (Math.pow(a, 1/(b+c))); // +r case 13: return (a - b - c); // -- case 14: return (a*(b-c)); // -* case 15: return (a*b - c); // -* case 16: return (a - b*c); // -* case 17: return ((a-b)/c); // -/ case 18: return (a - b/c); // -/ case 19: return (a/b - c); // -/ case 20: if (b==c) return (WRONG); // test return (a /(b-c)); // -/ case 21: return (Math.pow(a-b, c)); // -p case 22: return (Math.pow(a, b-c)); // -p case 23: return (a - Math.pow(b, c)); // -p case 24: return (Math.pow(a, b) - c); // -p case 25: return (a - Math.pow(b, 1/c)); // -r case 26: if (b>a) return (WRONG); // test return (Math.pow(a-b, 1/c)); // -r case 27: if (c>b) return (WRONG); // test return (Math.pow(a, 1/(b-c))); // -r case 28: return (Math.pow(a, 1/b) - c); // -r case 29: return (a*b*c); // ** case 30: return ((a*b)/c); // */ case 31: return (a / (b*c)); // */ case 32: return (Math.pow(a, b*c)); // *p case 33: return (Math.pow(a, b) * c); // *p case 34: return (Math.pow(a*b, c)); // *p case 35: return (Math.pow(a*b, 1/c)); // *r case 36: return (Math.pow(a, 1/b) * c); // *r case 37: return (Math.pow(a, 1/(b*c))); // *r // // redundant with */ case 38: return (Math.pow(a, b/c)); // /p case 39: return (a / Math.pow(b, c)); // /p case 40: return (Math.pow(a, b) / c); // /p case 41: return (Math.pow(a/b, c)); // /p case 42: return (Math.pow(a, 1/(b/c))); // /r case 43: return (Math.pow(a/b, 1/c)); // /r case 44: return (a / Math.pow(b, 1/c)); // /r case 45: return (Math.pow(a, 1/b) / c); // /r case 46: return (Math.pow(a, Math.pow(b, c))); // pp case 47: return (Math.pow(Math.pow(a, b), c)); // pp case 48: return (Math.pow(a, Math.pow(b, 1/c))); // pr case 49: return (Math.pow(Math.pow(a, 1/b), c)); // pr case 50: return (Math.pow(a, 1/Math.pow(b,c))); // pr case 51: return (Math.pow(Math.pow(a, b), 1/c)); // pr case 52: return (Math.pow(Math.pow(a, 1/b), 1/c)); // rr default: return (WRONG); } } int absoluteValue(int i) { if (i>0) return i; return(-i); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == calculateButton) answerLabel.setText("Best: " + getBestValueString(scoringChoice[0].getSelectedIndex() + 1, scoringChoice[1].getSelectedIndex() + 1, scoringChoice[2].getSelectedIndex() + 1, (targetChoice[0].getSelectedIndex() + 1)* (targetChoice[1].getSelectedIndex() + 1))); } } // END OF Class dice_results