I have been asked to publish the Java source code for the Empirical Rabbit Timer, so here it is:
/*
* Empirical Rabbit Timer
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.text.DecimalFormat;
public class RabbitTimer extends JFrame implements KeyListener {
int ProblemIndex = 81, ClumpSize = 9, ProblemInc = 90;
int ProblemNumber = 0, ProblemMax = 10000;
int Buckets = 12, BucketWidth = 5;
int[] Bucket = new int[Buckets+1];
String[] ProblemId = new String[ProblemMax];
int State = 1;
long StartTime;
float SolutionTime;
JLabel xlabel = new JLabel("Input File Error", JLabel.CENTER);
JTextField xtext = new JTextField("0% Hit the . key or s for stats", 80);
RabbitTimer() {
super("Empirical Rabbit Timer");
setSize(600,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
xtext.addKeyListener(this);
BorderLayout bord = new BorderLayout();
setLayout(bord);
xlabel.setFont(new Font("Times New Roman", Font.PLAIN, 24));
add(xlabel, BorderLayout.CENTER);
add(xtext, BorderLayout.SOUTH);
setVisible(true);
try{
// Open the file
FileInputStream fstream = new FileInputStream("rabbitin.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
if(ProblemNumber < ProblemMax)ProblemId[ProblemNumber++] = strLine;
}
// Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
ProblemNumber = ProblemMax+1;
}
if(ProblemNumber <= ProblemMax)
xlabel.setText(ProblemId[ProblemIndex] + ". Ready?");
// Update the time taken
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(State == 2) {
SolutionTime = (System.currentTimeMillis() - StartTime) / 10;
SolutionTime /= 100;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
xlabel.setText(ProblemId[ProblemIndex] + ". " +
twoPlaces.format(SolutionTime) + " sec. Move?");
}
}
};
new Timer(delay, taskPerformer).start();
}
public void keyTyped(KeyEvent input) {
char key = input.getKeyChar();
switch (State) {
case 1:
// State = Ready?
String outstring;
switch(key) {
case 's':
outstring = "" + Bucket[0];
for (int i = 1; i < Buckets+1; i++)
outstring = outstring + "," + Bucket[i];
System.out.println(outstring);
break;
case '.':
if(ProblemIndex < ProblemNumber) {
State = 2;
xtext.setText("Hit any key");
StartTime = System.currentTimeMillis();
xlabel.setForeground(Color.red);
xlabel.setText(ProblemId[ProblemIndex] + ". " +
"0.00 sec. Move?");
}
break;
default:
Toolkit.getDefaultToolkit().beep();
}
break;
case 2:
// State = Move?
if(key == KeyEvent.VK_ENTER) {
SolutionTime = (System.currentTimeMillis()-StartTime) / 10;
SolutionTime /= 100;
State = 3;
xlabel.setText(ProblemId[ProblemIndex] + ". " +
SolutionTime + " sec. Score?");
xlabel.setForeground(Color.black);
xtext.setText("Hit 0, 1, r=redo or b=back");
} else Toolkit.getDefaultToolkit().beep();
break;
case 3:
// State = Score?
switch (key) {
case '0':
case '1':
System.out.println(ProblemId[ProblemIndex] + "," +
SolutionTime + "," + key);
if (key == '0') SolutionTime = 999;
for (int i = 0; i < Buckets; i++) {
if (SolutionTime >= i*BucketWidth &&
SolutionTime < (i+1)*BucketWidth) Bucket[i]++;
}
if (SolutionTime > Buckets*BucketWidth)
Bucket[Buckets]++;
if(ProblemIndex < ProblemNumber){
ProblemIndex++;
if (ProblemIndex % ClumpSize == 0)
ProblemIndex += ProblemInc - ClumpSize;
}
case 'r':
State = 1;
if (ProblemIndex < ProblemNumber)
xlabel.setText
(ProblemId[ProblemIndex] + ". Ready?");
else {xlabel.setText("The End");
ProblemIndex = ProblemNumber;}
int Percent = ProblemIndex * 100 / ProblemNumber;
xtext.setText(Percent +
"% Hit the . key or s for stats");
break;
case 'b':
State = 2;
xlabel.setText
(ProblemId[ProblemIndex] + ". Move?");
xlabel.setForeground(Color.red);
xtext.setText("Hit any key (t for time)");
break;
default:
Toolkit.getDefaultToolkit().beep();
}
}
}
public void keyPressed(KeyEvent txt) {
}
public void keyReleased(KeyEvent txt) {
}
public static void main(String[] args) {
RabbitTimer rab = new RabbitTimer();
}
}
No comments:
Post a Comment