1    	package acl2s.lib.bool;
2    	
3    	import java.awt.BorderLayout;
4    	import java.awt.Color;
5    	import java.awt.Component;
6    	import java.awt.Dimension;
7    	import java.awt.FlowLayout;
8    	import java.awt.Font;
9    	import java.awt.GridLayout;
10   	import java.awt.event.ActionEvent;
11   	import java.awt.event.ActionListener;
12   	import java.awt.event.MouseEvent;
13   	import java.awt.event.MouseListener;
14   	
15   	import javax.swing.BoxLayout;
16   	import javax.swing.ButtonGroup;
17   	import javax.swing.JButton;
18   	import javax.swing.JFrame;
19   	import javax.swing.JLabel;
20   	import javax.swing.JLayeredPane;
21   	import javax.swing.JPanel;
22   	import javax.swing.JTextArea;
23   	import javax.swing.border.CompoundBorder;
24   	import javax.swing.border.EmptyBorder;
25   	import javax.swing.border.LineBorder;
26   	
27   	import acl2s.lib.session.BootstrapParseContext;
28   	
29   	public class SwingTCNGame extends JPanel implements ActionListener, MouseListener {
30   		/**
31   		 * 
32   		 */
33   		private static final long serialVersionUID = 1228195622538582083L;
34   	
35   		JTextArea problemArea;
36   	
37   		JLayeredPane answerPane;
38   		JTextArea answerTextArea;
39   		JPanel answerButtonsPanel;
40   	
41   		JLabel recordLabel;
42   	
43   		boolean answerShown; // iff answerTextArea added to answerPanel
44   		BooleanFormula formula;
45   	
46   		long startingTime;
47   		Object guess;
48   	
49   		int numCorrect;
50   		int numAttempted;
51   		long millisAccum;
52   	
53   		VarOccChoice varOccConfig;
54   		OpsChoice opsConfig;
55   		
56   		public SwingTCNGame() {
57   			setLayout(new BorderLayout());
58   			
59   			problemArea = new JTextArea(4,70);
60   			problemArea.setFont(Font.decode("Monospaced BOLD 16"));
61   			problemArea.setEditable(false);
62   			problemArea.setLineWrap(false);
63   			problemArea.addMouseListener(this);
64   			problemArea.setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
65   	                                                 new EmptyBorder(2,2,2,2)));
66   			add(problemArea, BorderLayout.NORTH);
67   			
68   			JPanel lp = new JPanel();
69   			lp.setLayout(new FlowLayout());
70   			
71   			answerPane = new JLayeredPane();
72   			
73   			answerButtonsPanel = new JPanel();
74   			answerButtonsPanel.setLayout(new BoxLayout(answerButtonsPanel, BoxLayout.Y_AXIS));
75   			JButton tButton = new JButton(T_STRING);
76   			tButton.setAlignmentX(Component.CENTER_ALIGNMENT);
77   			tButton.addActionListener(this);
78   			answerButtonsPanel.add(tButton);
79   			JButton cButton = new JButton(C_STRING);
80   			cButton.setAlignmentX(Component.CENTER_ALIGNMENT);
81   			cButton.addActionListener(this);
82   			answerButtonsPanel.add(cButton);
83   			JButton nButton = new JButton(N_STRING);
84   			nButton.setAlignmentX(Component.CENTER_ALIGNMENT);
85   			nButton.addActionListener(this);
86   			answerButtonsPanel.add(nButton);
87   			
88   			answerTextArea = new JTextArea(16, 16);
89   			answerTextArea.setFont(Font.decode("Monospaced BOLD 14"));
90   			answerTextArea.setEditable(false);
91   			answerTextArea.setLineWrap(false);
92   			answerTextArea.addMouseListener(this);
93   			answerTextArea.setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
94   	                                                    new EmptyBorder(4,4,4,4)));
95   			
96   			
97   			
98   			answerPane.setPreferredSize(new Dimension(cButton.getPreferredSize().width,
99   					                                  answerTextArea.getPreferredSize().height));
100  	
101  			answerPane.add(answerButtonsPanel, new Integer(0));
102  			answerButtonsPanel.setSize(answerPane.getPreferredSize());
Event do_not_call: "java.lang.Integer(int arg1)" is not recommended for performance reasons. Consider using Integer.valueOf(int) instead.
103  			answerPane.add(answerTextArea, new Integer(1));
104  			answerTextArea.setSize(answerPane.getPreferredSize());
105  	
106  			
107  			lp.add(answerPane);
108  			
109  			JPanel radioPanel = new JPanel(new GridLayout(10,1));
110  			ButtonGroup grp = new ButtonGroup();
111  			VarOccChoice voc = new VarOccChoice(1,2,this);
112  			grp.add(voc);
113  			radioPanel.add(voc);
114  			voc = new VarOccChoice(2,3,this); // default
115  			grp.add(voc);
116  			radioPanel.add(voc);
117  			voc.setSelected(true);
118  			varOccConfig = voc;
119  			voc = new VarOccChoice(3,4,this);
120  			grp.add(voc);
121  			radioPanel.add(voc);
122  			voc = new VarOccChoice(2,4,this);
123  			grp.add(voc);
124  			radioPanel.add(voc);
125  			voc = new VarOccChoice(3,5,this);
126  			grp.add(voc);
127  			radioPanel.add(voc);
128  			
129  			grp = new ButtonGroup();
130  			OpsChoice oc = new OpsChoice(BooleanBinaryOperator.TWO_BASIC,this);
131  			grp.add(oc);
132  			radioPanel.add(oc);
133  			oc = new OpsChoice(BooleanBinaryOperator.THREE_BASIC,this); // default;
134  			grp.add(oc);
135  			radioPanel.add(oc);
136  			oc.setSelected(true);
137  			opsConfig = oc;
138  			oc = new OpsChoice(BooleanBinaryOperator.FOUR_BASIC,this); // default;
139  			grp.add(oc);
140  			radioPanel.add(oc);
141  			
142  			lp.add(radioPanel);
143  			
144  			add(lp,BorderLayout.CENTER);
145  			
146  			JPanel rp = new JPanel();
147  			rp.setLayout(new FlowLayout());
148  			JButton resetButton = new JButton("Reset");
149  			resetButton.addActionListener(new ActionListener() {
150  				public void actionPerformed(ActionEvent e) {
151  					reset();
152  				}
153  			});
154  			rp.add(resetButton);
155  			recordLabel = new JLabel("Record: ----------------   Avg time: ---------");
156  			rp.add(recordLabel);
157  			
158  			add(rp,BorderLayout.SOUTH);
159  					
160  			reset();
161  		}
162  		
163  		public void reset() {
164  			numAttempted = 0;
165  			numCorrect = 0;
166  			millisAccum = 0;
167  			updateRecordInfo();
168  			answerShown = true;
169  			answerPane.setLayer(answerTextArea, 1);
170  			answerPane.setLayer(answerButtonsPanel, 0);
171  			problemArea.setText("Click either white text area to continue\nnow and after each problem ...");
172  			answerTextArea.setText("");
173  		}
174  		
175  		public void updateRecordInfo() {
176  			float pct;
177  			float time;
178  			if (numAttempted == 0) {
179  				pct = 100f;
180  				time = 0f;
181  			} else {
182  				pct = 100f * (float)numCorrect / (float) numAttempted;
183  				time = (float) millisAccum / (float) numAttempted / 1000f;
184  			}
185  			recordLabel.setText("Record: " + numCorrect + " / " + numAttempted + "  (" +
186  					            pct + "%)    Avg time: " + time + "s");
187  		}
188  		
189  		public void step() {
190  			if (answerShown) {
191  				formula = RandomBooleanFormula.fromNumVarsOccType(varOccConfig.numVars, varOccConfig.numOcc, opsConfig.ops, true, false, BooleanFormula.ALL_TYPES);
192  				problemArea.setText(formula.toString() + "\n\n" + formula.toACL2Expr().toString(BootstrapParseContext.instance));
193  				answerTextArea.setText("?");
194  				guess = null;
195  				answerShown = false;
196  				answerPane.setLayer(answerTextArea, 1);
197  				answerPane.setLayer(answerButtonsPanel, 2);
198  				startingTime = System.currentTimeMillis();
199  			} else {
200  				StringBuilder aText = new StringBuilder();
201  				numAttempted++;
202  				long endingTime = System.currentTimeMillis();
203  				millisAccum += endingTime - startingTime;
204  				if (guess != null) {
205  					if (guess == formula.getType()) {
206  						aText.append("Correct!\n");
207  						numCorrect++;
208  					} else {
209  						aText.append("INCORRECT!\n");
210  					}
211  				} else {
212  					aText.append("\n");
213  				}
214  				aText.append("(");
215  				aText.append(formula.getType().toString());
216  				aText.append(")\n\n");
217  				aText.append(formula.getTruthTable().toStringTF());
218  				
219  				answerTextArea.setText(aText.toString());
220  				updateRecordInfo();
221  				
222  				guess = null;
223  				answerShown = true;
224  				answerPane.setLayer(answerTextArea, 1);
225  				answerPane.setLayer(answerButtonsPanel, 0);
226  			}
227  		}
228  		
229  		public void mouseClicked(MouseEvent e) {
230  			if (answerShown) {
231  				step();
232  			}
233  		}
234  		
235  		public void actionPerformed(ActionEvent e) {
236  			if (e.getSource() instanceof VarOccChoice) {
237  				varOccConfig = (VarOccChoice) e.getSource();
238  				reset();
239  			} else if (e.getSource() instanceof OpsChoice) {
240  				opsConfig = (OpsChoice) e.getSource();
241  				reset();
242  			} else if (answerShown) {
243  				step();
244  			} else {
245  				String v = e.getActionCommand();
246  				if (v.equals(T_STRING)) {
247  					guess = BooleanFormula.Type.TAUTOLOGY;
248  					step();
249  				} else if (v.equals(C_STRING)) {
250  					guess = BooleanFormula.Type.CONTRADICTION;
251  					step();
252  				} else if (v.equals(N_STRING)) {
253  					guess = BooleanFormula.Type.NEITHER;
254  					step();
255  				} else {
256  					// ignore??
257  				}
258  			}
259  		}
260  	
261  		
262  		// UNUSED:
263  		public void mouseEntered(MouseEvent e) {}
264  		public void mouseExited(MouseEvent e) {}
265  		public void mousePressed(MouseEvent e) {}
266  		public void mouseReleased(MouseEvent e) {}
267  		
268  		
269  		
270  		
271  		public static void main(String[] args) {
272  			JFrame f = new JFrame("Boolean Logic Game");
273  			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
274  			f.getContentPane().add(new SwingTCNGame());
275  			f.pack();
276  			f.setVisible(true);
277  		}
278  	
279  		public static String T_STRING = BooleanFormula.Type.TAUTOLOGY.toString();
280  		public static String C_STRING = BooleanFormula.Type.CONTRADICTION.toString();
281  		public static String N_STRING = BooleanFormula.Type.NEITHER.toString();
282  		
283  	}