-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathKeyboard.java
More file actions
141 lines (119 loc) · 5 KB
/
Keyboard.java
File metadata and controls
141 lines (119 loc) · 5 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
/**
* We use an abstract class for Keyboard for future possibilities of different keyboard layouts
* that a user wants to script input for
*/
public class Keyboard {
/**
* The keyboard mapping to use for scripting the lines from input files
*/
private char[][] keyboardLayout;
/**
* Constructor for this class, sets value of keyboard
* @param keyboardLayout - the keyboard mapping to be used for this instance of Keyboard
*/
public Keyboard(char[][] keyboardLayout) {
this.keyboardLayout = keyboardLayout;
}
/**
* Takes the input and determines the script necessary to obtain that input
* @param input - a String to convert into a script
* @return a String with the script representing how to obtain the input for this particular keyboard
*/
String scriptLine(String input) {
// Initializes the builder string and converts input to uppercase to match the keyboard layouts
String result = "";
String uppercase = input.toUpperCase();
// The Point objects we will use to store locations and calculate the paths
Point initial = new Point();
Point destination = new Point();
// Iterate through each character in the string, performing the correct conversions on each
for(int i = 0; i < uppercase.length(); i++) {
char letter = uppercase.charAt(i);
if(letter == ' ')
// A space
result += 'S';
else if(keyboardContains(letter)) {
// A character inside the keyboard
result += scriptLetter(letter, initial, destination);
} else {
// Not a character inside the keyboard - error
result += 'E';
}
if(i < uppercase.length() - 1)
result += ",";
}
return result;
}
/**
* Takes letterToScript and converts it to a sequence of U, D, L and R's, ending with a #
* @param letterToScript - the letter to convert
* @return a string formatted as explained above
*/
private String scriptLetter(char letterToScript, Point initialPoint, Point destinationPoint) {
String script = "";
// Sets the correct location of the destination of the letter we are now searching for
setDestinationPoint(letterToScript, destinationPoint);
// Calculate the displacement (the path)
int verticalDisplacement = destinationPoint.getI() - initialPoint.getI();
int horizontalDisplacement = destinationPoint.getJ() - initialPoint.getJ();
// Based on value of displacement, add appropriate letters to the string we are building
if(verticalDisplacement < 0) {
verticalDisplacement *= -1;
script += addLetters(verticalDisplacement, 'U');
} else {
script += addLetters(verticalDisplacement, 'D');
}
if(horizontalDisplacement < 0) {
horizontalDisplacement *= -1;
script += addLetters(horizontalDisplacement, 'L');
} else {
script += addLetters(horizontalDisplacement, 'R');
}
script += "#";
// Current destination point becomes initial point for the next letter (represents the cursor)
initialPoint.setI(destinationPoint.getI());
initialPoint.setJ(destinationPoint.getJ());
return script;
}
/**
* Builds a string consisting of toAdd repeated numTimes times with commas in following each time
* @param numTimes - the number of times to add the character
* @param toAdd - the character to add to the string we are building
* @return a String formatted as explained above
*/
private String addLetters(int numTimes, char toAdd) {
String builder = "";
for(int i = 0; i < numTimes; i++)
builder += toAdd + ",";
return builder;
}
/**
* Searches for letterToFind in keyboardLayout and sets the location of it in pointToSet
* @param letterToFind - the letter whose location we are searching for
* @param pointToSet - the Point whose location we will set to match letterToFind, represents the destination
*/
private void setDestinationPoint(char letterToFind, Point pointToSet) {
for(int i = 0; i < keyboardLayout.length; i++) {
for(int j = 0; j < keyboardLayout[0].length; j++) {
if(letterToFind == keyboardLayout[i][j]) {
pointToSet.setI(i);
pointToSet.setJ(j);
}
}
}
}
/**
* Checks if keyboardLayout contains letterToCheck
* @param letterToCheck - the letter desired in the keyboard
* @return true if keyboardLayout contains letterToCheck, false otherwise
*/
private boolean keyboardContains(char letterToCheck) {
for(char[] row : keyboardLayout) {
for(char c : row) {
if(c == letterToCheck)
return true;
}
}
return false;
}
}