-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeedReader.java
More file actions
182 lines (174 loc) · 6.09 KB
/
SpeedReader.java
File metadata and controls
182 lines (174 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.awt.Font;
public class SpeedReader{
private double wpm; // caps out at around 25250 wpm on my computer
private Deque<String> a;
private Deque<String> b;
private boolean ready;
private final int DEFAULT_SPEED = 500;
private int counter;
private Stopwatch timer;
public SpeedReader(double wpm){
this.wpm = wpm;
a = new Deque();
b = new Deque();
ready = true;
counter = 0;
timer = new Stopwatch();
}
public String nextWord(){
String temp;
if(!b.isEmpty())
temp = b.removeFirst();
else if (!StdIn.isEmpty()){
temp = StdIn.readString();
if(StdIn.isEmpty())
ready = false;
}
else temp = a.removeFirst();
a.addLast(temp);
return temp;
}
public void runNow(){
StdDraw.setCanvasSize(400, 80*5);
StdDraw.clear();
do{
if (StdDraw.mousePressed()) ready = false;
if(StdDraw.hasNextKeyTyped())
playBack();
if(!ready){
waitPLS();
}
String temp = nextWord();
draw(temp);
if(wpm == 0) ready = false;
else StdDraw.show((int) (1000.0*60*(temp.length()+5) / (10.0*wpm)));
}
while(!StdIn.isEmpty() || !a.isEmpty());
}
public void playBack(){
char STOP = '5';
char BAC = '0';
char BACK = '4';
char BACK2 = '1';
char FOR = '2';
char FORWARD = '6';
char FORWARD2 = '3';
char SLOW = '7';
char RESET = '8';
char FAST = '9';
char temp = StdDraw.nextKeyTyped();
if(temp == STOP){
ready = !ready;
}
else if(temp == BAC){
for(int i = 0; i < 2 && !a.isEmpty(); i++){
counter--;
b.addFirst(a.removeLast());
}
draw(nextWord());
}
else if(temp == BACK){
for(int i = 0; i < wpm / 60 + 2 && !a.isEmpty(); i++){
counter--;
b.addFirst(a.removeLast());
}
draw(nextWord());
}
else if(temp == BACK2){
for(int i = 0; i < wpm / 6 + 2 && !a.isEmpty(); i++){
counter--;
b.addFirst(a.removeLast());
}
draw(nextWord());
}
else if(temp == FOR){
draw(nextWord());
}
else if(temp == FORWARD){
if(!b.isEmpty()){
for(int i = 0; i < wpm / 60 && !b.isEmpty(); i++){
counter++;
a.addLast(b.removeFirst());
}
draw(nextWord());
}
else{
for(int i = 0; i < wpm / 60 && !StdIn.isEmpty(); i++){
counter++;
a.addLast(StdIn.readString());
}
draw(nextWord());
}
}
else if(temp == FORWARD2){
if(!b.isEmpty()){
for(int i = 0; i < wpm / 6 && !b.isEmpty(); i++){
counter++;
a.addLast(b.removeFirst());
}
draw(nextWord());
}
else{
for(int i = 0; i < wpm / 6 && !StdIn.isEmpty(); i++){
counter++;
a.addLast(StdIn.readString());
}
draw(nextWord());
}
}
else if(temp == SLOW){
wpm -= 50;
}
else if(temp == RESET){
wpm = DEFAULT_SPEED;
}
else if(temp == FAST){
wpm += 50;
}
else StdOut.println(temp);
}
public void waitPLS(){
StdDraw.show(100);
while(!ready){
if(StdDraw.mousePressed())
ready = true;
if(StdDraw.hasNextKeyTyped()){
playBack();
}
StdDraw.show(100);
}
ready = true;
}
public void draw(String word) {
double HEIGHT = 5.0;
double LENGTH = 5.0;
double N = 0.25;
//double mid = (double) N*word.length()/2;
double mid = LENGTH / 2;
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setXscale(0, LENGTH);
//StdDraw.setYscale(0, HEIGHT);
StdDraw.setYscale(-4*HEIGHT, HEIGHT);
// write status text
StdDraw.line(mid, HEIGHT, mid, 3*HEIGHT / 4);
StdDraw.line(mid, 0, mid, HEIGHT / 4);
StdDraw.setFont(new Font("SansSerif", Font.BOLD, 32));
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.redText(mid, HEIGHT / 2, word);
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 8));
StdDraw.text(6.0*mid/4, 0, "Words: " + counter + " WPM: " + wpm + " " + timer.toTime()
+ " Avg: " + (int)(60 * counter / timer.elapsedTime()));
//StdOut.println(word + " " + N*word.length() + " " + N*word.length()/2);
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
StdDraw.paragraph(0, -1, LENGTH, -4*HEIGHT, a);
counter++;
}
public static void main(String[] args){
int wpm = 500;
if(args.length > 0)
wpm = Integer.parseInt(args[0]);
SpeedReader test = new SpeedReader(wpm);
test.runNow();
}
}