import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise25_7 extends JFrame {
private Integer[] numbersPicked = {0, 0, 0, 0};
private int[] fileNumber = new int[4];
private MyArrayList<Integer> list;
private ImageIcon card1Icon, card2Icon, card3Icon, card4Icon;
private JLabel jlblCard1, jlblCard2, jlblCard3, jlblCard4;
private JButton jbtRefresh = new JButton("Refresh");
private JButton jbtVerify = new JButton("Verify");
private JTextField jtfExpression = new JTextField(10);
// private int expressionNumber1, expressionNumber2, expressionNumber3, expressionNumber4, imageFileNumber1, imageFileNumber2, imageFileNumber3, imageFileNumber4;
private String imageFile1 = "image/card/";
private String imageFile2 = "image/card/";
private String imageFile3 = "image/card/";
private String imageFile4 = "image/card/";
public Exercise25_7() {
pickCards();
JPanel p1 = new JPanel(new BorderLayout());
p1.add(jbtRefresh, BorderLayout.EAST);
JPanel p2 = new JPanel(new GridLayout(1, 4));
p2.add(jlblCard1);
p2.add(jlblCard2);
p2.add(jlblCard3);
p2.add(jlblCard4);
JPanel p3 = new JPanel(new GridLayout(1, 3));
p3.add(new JLabel("Enter an expression: "));
p3.add(jtfExpression);
p3.add(jbtVerify);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
jbtRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearData();
pickCards();
}
});
jbtVerify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = 0;
String expression = jtfExpression.getText();
boolean correct = checkExpression(expression);
if (correct) {
result = EvaluateExpression.evaluateExpression(expression);
}
else {
System.out.println("Expression includes invalid numbers");
}
if (result == 24)
System.out.println(expression + " = 24");
else if ((result != 24) && correct)
System.out.println(expression + " does not equal 24");
}
});
}
public boolean checkExpression(String expression) {
list = new MyArrayList<Integer>(numbersPicked);
java.util.StringTokenizer tokens = new java.util.StringTokenizer(expression, "()+-/*", true);
// String tokens[] = expression.split("[ ()+-*/]");
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken().trim();
if (token.length() == 0)
continue;
else if (Character.isDigit(token.charAt(0))){
if (!checkNum(Integer.parseInt(token)))
return false;
}
}
return true;
}
public boolean checkNum(int num) {
boolean found = false;
for (int i = 0; i < list.size(); i++){
if (list.get(i) == num){
found = true;
list.remove(i);
}
}
return found;
}
public void pickCards() {
int picked = 0;
//private boolean notPicked = true;
while (picked < 4) {
int number = (int)(Math.random() * 13 + 1);
if (! isPicked(number)){
picked++;
}
}
for (int i = 0; i < numbersPicked.length; i++)
System.out.print(numbersPicked[i] + ", ");
System.out.println();
for (int i = 0; i < fileNumber.length; i++){
int set = (int)(Math.random() * 4 + 1);
if (set == 1)
fileNumber[i] = numbersPicked[i];
else if (set == 2)
fileNumber[i] = numbersPicked[i] + 13;
else if (set == 3)
fileNumber[i] = numbersPicked[i] + 26;
else
fileNumber[i] = numbersPicked[i] + 39;
}
for (int i = 0; i < fileNumber.length; i++)
System.out.print(fileNumber[i] + ", ");
System.out.println();
imageFile1 = imageFile1 + fileNumber[0] + ".png";
imageFile2 = imageFile2 + fileNumber[1] + ".png";
imageFile3 = imageFile3 + fileNumber[2] + ".png";
imageFile4 = imageFile4 + fileNumber[3] + ".png";
System.out.println("image file1 " + imageFile1);
System.out.println("image file2 " + imageFile2);
System.out.println("image file3 " + imageFile3);
System.out.println("image file4 " + imageFile4);
card1Icon = new ImageIcon(imageFile1);
card2Icon = new ImageIcon(imageFile2);
card3Icon = new ImageIcon(imageFile3);
card4Icon = new ImageIcon(imageFile4);
jlblCard1 = new JLabel(card1Icon);
jlblCard2 = new JLabel(card2Icon);
jlblCard3 = new JLabel(card3Icon);
jlblCard4 = new JLabel(card4Icon);
}
public boolean isPicked(int num) {
for (int i = 0; i < numbersPicked.length; i++){
if (numbersPicked[i] == num)
return true;
else if (numbersPicked[i] == 0) {
numbersPicked[i] = num;
return false;
}
}
numbersPicked[0] = num;
return false;
}
public void clearData() {
for (int i = 0; i < numbersPicked.length; i++)
numbersPicked[i] = 0;
imageFile1 = "image/card/";
imageFile2 = "image/card/";
imageFile3 = "image/card/";
imageFile4 = "image/card/";
jtfExpression.setText("");
}
public static void main(String[] args) {
Exercise25_7 frame = new Exercise25_7();
frame.setTitle("Exercise 25.7");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(400, 300);
frame.pack();
frame.setVisible(true);
}
}