1 package folder1; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.util.*; 6 import java.awt.event.*; 7 8 public class PointCardFrame extends JFrame { 9 public static final double EPISON=1E-6; 10 private CenterPanel centerpanel = new CenterPanel(); 11 private HeadPanel headpanel = new HeadPanel(); 12 private TailPanel tailpanel = new TailPanel(); 13 14 public PointCardFrame() { 15 HeadPanel headpanel = new HeadPanel(); 16 TailPanel tailpanel = new TailPanel(); 17 18 add(headpanel, BorderLayout.NORTH); 19 add(centerpanel, BorderLayout.CENTER); 20 add(tailpanel, BorderLayout.SOUTH); 21 22 23 } 24 public static void main(String[] args) { 25 // JFrame frame = new PointCardFrame(); 26 JFrame frame = new PointCardFrame(); 27 frame.setTitle("24PointCard"); 28 frame.pack(); 29 frame.setLocationRelativeTo(null); 30 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 31 frame.setVisible(true); 32 } 33 public class HeadPanel extends JPanel { 34 private JButton jbtRefresh = new JButton("Refresh"); 35 private JButton jbtSolution = new JButton("Find a Solution"); 36 private JTextField textField = new JTextField(15); 37 private String[] operator = {"+" , "-", "*", "/", "(", ")" }; 38 private double[] number=new double[4]; 39 private String[] exp=new String[4]; 40 public HeadPanel() { 41 add(jbtSolution, BorderLayout.WEST); 42 add(textField, BorderLayout.CENTER); 43 add(jbtRefresh, BorderLayout.EAST); 44 45 jbtRefresh.addActionListener( 46 new ActionListener() { 47 public void actionPerformed(ActionEvent e) { 48 centerpanel.removeAll(); 49 centerpanel.Display(); 50 centerpanel.updateUI(); 51 } 52 }); 53 jbtSolution.addActionListener( 54 new ActionListener() { 55 public void actionPerformed(ActionEvent e) { 56 number[0]=(double)centerpanel.cardvaule[0]; 57 number[1]=(double)centerpanel.cardvaule[1]; 58 number[2]=(double)centerpanel.cardvaule[2]; 59 number[3]=(double)centerpanel.cardvaule[3]; 60 exp[0] = String.valueOf(centerpanel.cardvaule[0]); 61 exp[1] = String.valueOf(centerpanel.cardvaule[1]); 62 exp[2] = String.valueOf(centerpanel.cardvaule[2]); 63 exp[3] = String.valueOf(centerpanel.cardvaule[3]); 64 if(is24(4)){ 65 textField.setText(exp[0]); 66 } 67 else 68 textField.setText("没有答案"); 69 } 70 }); 71 72 } 73 public boolean is24 (int n) { 74 if(n==1) return (Math.abs(number[0]-24)<EPISON); 75 for(int i=0;i<n;i++){ 76 for(int j=i+1;j<n;j++){ //进行组合 77 double a,b; 78 String expa,expb; 79 a=number[i]; //保存起来,在方法最后再恢复,以便继续计算 80 b=number[j]; //保存起来,在方法最后再恢复,以便继续计算 81 number[j]=number[n-1]; //将最后一个数挪过来 82 expa=exp[i]; //保存起来,在方法最后再恢复,以便继续计算 83 expb=exp[j]; //保存起来,在方法最后再恢复,以便继续计算 84 exp[j]=exp[n-1]; //将最后一个式子挪过来 85 exp[i]="("+expa+"+"+expb+")"; //看看加法能否算出 86 number[i]=a+b; 87 if (is24(n-1)) return true; 88 exp[i]="("+expa+"-"+expb+")"; //看看减法能否算 89 number[i]=a-b; 90 if (is24(n-1)) return true; 91 exp[i]="("+expb+"-"+expa+")"; 92 number[i]=b-a; 93 if (is24(n-1)) return true; 94 exp[i]="("+expa+"*"+expb+")"; //看看乘法能否算 95 number[i]=a*b; 96 if (is24(n-1)) return true; 97 if(b!=0){ 98 exp[i]="("+expa+"/"+expb+")"; //看看除法能否算 99 number[i]=a/b; 100 if (is24(n-1)) return true; 101 } 102 if(a!=0){ 103 exp[i]="("+expb+"/"+expa+")"; 104 number[i]=b/a; 105 if (is24(n-1)) return true; 106 } 107 number[i]=a; //恢复 108 number[j]=b; 109 exp[i]=expa; 110 exp[j]=expb; 111 } 112 } 113 return false; 114 115 } 116 117 } 118 public class CenterPanel extends JPanel { 119 private int[] cardvaule = new int[4]; 120 private int[] usedcard = new int[4]; 121 122 public CenterPanel() { 123 loop: for (int i = 0; i < 4; i++) { 124 Random random = new Random(); 125 int radomnumber = random.nextInt(52) + 1; 126 for (int k = 0; k < i; k++) { 127 if (usedcard[k] == radomnumber){ 128 i--; 129 continue loop; 130 } 131 132 } 133 String string = String.valueOf(radomnumber); 134 add(new JLabel(new ImageIcon("D:\\card\\" + string + ".PNG"))); 135 usedcard[i] = radomnumber; 136 if (radomnumber % 13 != 0) 137 cardvaule[i] = radomnumber % 13; 138 else 139 cardvaule[i] = 13; 140 } 141 } 142 public void Display() { 143 usedcard[0] = -1; 144 usedcard[1] = -1; 145 usedcard[2] = -1; 146 usedcard[3] = -1; 147 loop: for (int i = 0; i < 4; i++) { 148 Random random = new Random(); 149 int radomnumber = random.nextInt(52) + 1; 150 for (int k = 0; k < i; k++) { 151 if (usedcard[k] == radomnumber){ 152 i--; 153 continue loop; 154 } 155 } 156 String string = String.valueOf(radomnumber); 157 add(new JLabel(new ImageIcon("D:\\card\\" + string + ".PNG"))); 158 usedcard[i] = radomnumber; 159 if (radomnumber % 13 != 0) 160 cardvaule[i] = radomnumber % 13; 161 else 162 cardvaule[i] = 13; 163 } 164 } 165 } 166 public class TailPanel extends JPanel { 167 private JLabel message = new JLabel("Enter an expression"); 168 private JTextField textField = new JTextField("Write an experssion",15); 169 private JButton jbtVerify = new JButton("Verify"); 170 private int result; 171 public TailPanel() { 172 add(message, BorderLayout.WEST); 173 add(textField, BorderLayout.CENTER); 174 add(jbtVerify, BorderLayout.EAST); 175 176 jbtVerify.addActionListener( 177 new ActionListener() { 178 public void actionPerformed(ActionEvent e) { 179 String experssion = textField.getText(); 180 result = EvaluateExpression.main(experssion); 181 if (result == 24) 182 JOptionPane.showMessageDialog(null,"resul is " + result + " right result!"); 183 else if (result == -1999) 184 JOptionPane.showMessageDialog(null,"Wrong expression"); 185 else 186 JOptionPane.showMessageDialog(null,"resul is " + result + " wrong result!"); 187 } 188 }); 189 } 190 191 } 192 193 }