-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypingQuiz.java
More file actions
95 lines (78 loc) · 3.01 KB
/
TypingQuiz.java
File metadata and controls
95 lines (78 loc) · 3.01 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
import java.util.Scanner;
import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
public class TypingQuiz
{
private Synthesizer synthesizer;
private String[] words =
{"blue", "hackathon", "water", "purple", "keyboard", "telephone", "dolphin", "popsicle", "cardboard", "confetti"};
private int score = 0;
public boolean evaluate(String attempt, int num)
{
if (attempt.equalsIgnoreCase(words[num]))
return true;
else
return false;
}
public void runGame()
{
Scanner input = new Scanner(System.in);
System.out.println("Hello! Welcome to Typing Test. You will be given 10 words to type.\n" +
"Please type in the word after you hear it and hit \"Enter\". You will hear a sound indicating if you typed it correctly.\n" +
"You will get your final score at the end of the game. Good luck and happy typing!");
for (int i = 0; i < 10; i++)
{
System.out.println("Please type in " + words[i]);
synthesizer.speakPlainText(
"Please type in " + words[i], null);
String attempt = input.nextLine();
if (evaluate(attempt, i))
{
score++;
synthesizer.speakPlainText("Correct", null);
System.out.println("Correct!");
}
else
{
synthesizer.speakPlainText("Incorrect", null);
System.out.println("Incorrect!");
}
}
synthesizer.speakPlainText("Your score is " + score + " out of 10.", null);
System.out.println("Your score is " + score + "/10.");
}
public static void main(String[] args)
{
// Below code is taken from: https://www.geeksforgeeks.org/converting-text-speech-java/
try {
// Set property as Kevin Dictionary
System.setProperty(
"freetts.voices",
"com.sun.speech.freetts.en.us"
+ ".cmu_us_kal.KevinVoiceDirectory");
// Register Engine
Central.registerEngineCentral(
"com.sun.speech.freetts"
+ ".jsapi.FreeTTSEngineCentral");
TypingQuiz game = new TypingQuiz();
game.synthesizer=Central.createSynthesizer(
new SynthesizerModeDesc(Locale.US));
// Allocate synthesizer
game.synthesizer.allocate();
// Resume Synthesizer
game.synthesizer.resume();
// Speaks the given text
// until the queue is empty.
game.runGame();
game.synthesizer.waitEngineState(
Synthesizer.QUEUE_EMPTY);
// Deallocate the Synthesizer.
game.synthesizer.deallocate();
}
catch (Exception e) {
e.printStackTrace();
}
}
}