-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCodeVisualTest.java
More file actions
163 lines (142 loc) · 4.15 KB
/
Copy pathMorseCodeVisualTest.java
File metadata and controls
163 lines (142 loc) · 4.15 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
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;
import java.io.File;
import java.lang.Thread;
import java.util.ArrayList;
import java.util.Random;
class MorseCodeVisualTest {
static int timeunit = 500;
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception
{
Random rand = new Random();
boolean hardmode = false;
String decode = "";
Dictionary<String, String> dictionary = new Hashtable<>();
ArrayList<String> easywords = new ArrayList<>();
ArrayList<String> hardwords = new ArrayList<>();
File file = new File("MorseKey.txt");
File easy = new File("EasyWords.txt");
File hard = new File("HardWords.txt");
Scanner easyscanner = new Scanner(easy);
Scanner hardscanner = new Scanner(hard);
Scanner keyScanner = new Scanner(file);
while(easyscanner.hasNext())
{
easywords.add(easyscanner.nextLine());
}
while(hardscanner.hasNext())
{
hardwords.add(hardscanner.nextLine());
}
while(keyScanner.hasNext())
{
String key = "";
key = keyScanner.next();
dictionary.put(key, keyScanner.next());
}
easyscanner.close();
hardscanner.close();
keyScanner.close();
int repetitions = 0;
int correct = 0;
boolean cont = true;
System.out.println("Hard Mode? (Y/N)");
if(scanner.nextLine().toUpperCase().equals("Y"))
{
hardmode = true;
}
clear();
changeSpeed();
while(cont)
{
repetitions++;
if(!hardmode)
{
decode = easywords.get(rand.nextInt(easywords.size()));
}
else
{
decode = hardwords.get(rand.nextInt(hardwords.size()));
}
System.out.println("Ready?");
scanner.nextLine();
clear();
delay(5);
String[] splitting = decode.split("");
for(int i = 0; i < splitting.length; i++)
{
String[] letter = dictionary.get(splitting[i]).split("");
for(int ii = 0; ii < letter.length; ii++)
{
if(letter[ii].equals("."))
{
blink(1);
}
else
{
blink(3);
}
}
delay(3);
}
System.out.println("What was the word communicated?");
String answer = scanner.nextLine();
clear();
answer = answer.toLowerCase();
if(answer.equals(decode))
{
System.out.println("Well done.");
correct++;
}
else
{
System.out.println("You are stupid, it was " + decode + ", not " + answer + ".");
}
System.out.println("Score: " + correct + " for " + repetitions + ".");
System.out.println("Try again? (Y/N), Change speed (C)");
String command = scanner.nextLine().toUpperCase();
if(command.equals("N"))
{
cont = false;
}
else if(command.equals("C"))
{
changeSpeed();
}
clear();
}
scanner.close();
}
public static void delay(int units)
{
try
{
Thread.sleep(units * timeunit);
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void blink(int units)
{
clear();
System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
delay(units);
clear();
delay(1);
}
public static void changeSpeed()
{
clear();
System.err.println("Set speed: (Default = 500ms)");
timeunit = Integer.parseInt(scanner.nextLine());
}
public static void clear()
{
System.out.print("\033[H\033[2J");
System.out.flush();
}
}