-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberCryptogram.java
More file actions
255 lines (216 loc) · 8.14 KB
/
Copy pathNumberCryptogram.java
File metadata and controls
255 lines (216 loc) · 8.14 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class NumberCryptogram extends Cryptogram {
private Map<Character, Integer> mapping;
public NumberCryptogram(String phrase) {
super(phrase);
mapping = new HashMap<>();
generateMapping();
}
public NumberCryptogram() {
this(getRandomPhrase());
}
private void generateMapping() {
List<Integer> temp = IntStream.range(1, 27).boxed().collect(Collectors.toList());
Collections.shuffle(temp);
for (int i = 0; i < Cryptogram.ALPHABET.length(); i++) {
mapping.put(Cryptogram.ALPHABET.charAt(i), temp.get(i));
}
}
public String getMappedPhrase() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < phrase.length(); i++) {
if (mapping.containsKey(phrase.charAt(i))) {
s.append(mapping.get(phrase.charAt(i)));
} else {
s.append(phrase.charAt(i));
}
}
return s.toString();
}
public boolean guess(String place, char value) {
int number;
try {
number = Integer.parseInt(place);
} catch (NumberFormatException e) {
return false;
}
if (number < 1 || number > 26) {
return false;
}
boolean containsNumber = false;
for (int i = 0; i < phrase.length(); i++) {
if (!mapping.containsKey(phrase.charAt(i)))
continue;
containsNumber = containsNumber || mapping.get(phrase.charAt(i)) == number;
}
if (!containsNumber)
return false;
for (int i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (Character.isAlphabetic(c) && mapping.get(c) == number) {
guess[i] = value;
}
}
return true;
}
public boolean undo(String place) {
int number;
try {
number = Integer.parseInt(place);
} catch (NumberFormatException e) {
return false;
}
if (number < 1 || number > 26) {
return false;
}
boolean containsNumber = false;
for (int i = 0; i < phrase.length(); i++) {
if (!mapping.containsKey(phrase.charAt(i)))
continue;
containsNumber = containsNumber || mapping.get(phrase.charAt(i)) == number;
}
if (!containsNumber)
return false;
for (int i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (Character.isAlphabetic(c) && mapping.get(c) == number) {
if (guess[i] == ' ') {
return false;
}
guess[i] = ' ';
}
}
return true;
}
public void render() {
for (char c : phrase.toCharArray()) {
if (Character.isAlphabetic(c)) {
int number = mapping.get(c);
System.out.print(number < 10 ? number + " " : number + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isAlphabetic(phrase.charAt(i))) {
if (guess[i] != ' ') {
System.out.print(guess[i] + " ");
} else if (guess[i] < 10) {
System.out.print("_ ");
} else {
System.out.print("_ ");
}
} else {
System.out.print(" ");
}
}
System.out.println();
}
public boolean isCorrectGuess(String place, char value) {
int firstIndex = phrase.indexOf(value);
if(firstIndex==-1)
return false;
if (phrase.charAt(firstIndex) == guess[firstIndex])
return true;
return false;
}
public void save(String playerName) {
try {
File inputFile = new File(Game.GAME_FILE);
File tempFile = new File("temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line;
boolean playerFound = false;
// Loop through the file, check if playerName is found on any line.
// If it is, overwrite the next three lines with updated information.
while ((line = reader.readLine()) != null) {
if (line.equals(playerName)) {
playerFound = true;
writer.write(line + "\n");
writer.write(phrase + "\n");
for (char c : guess) {
writer.write(c + ",");
}
writer.write("\n");
for (Map.Entry<Character, Integer> m : mapping.entrySet()) {
writer.write(m.getKey() + "|" + m.getValue() + ",");
}
writer.write("\n");
reader.readLine();
reader.readLine();
} else {
writer.write(line + "\n");
}
}
reader.close();
// If the player is not found in the file, add the information to the end of the file.
if (!playerFound) {
writer.write(playerName + "\n");
writer.write(phrase + "\n");
for (char c : guess) {
writer.write(c + ",");
}
writer.write("\n");
for (Map.Entry<Character, Integer> m : mapping.entrySet()) {
writer.write(m.getKey() + "|" + m.getValue() + ",");
}
writer.write("\n");
}
writer.close();
// Rename the temporary file to the original file.
inputFile.delete();
if (!tempFile.renameTo(inputFile)) {
throw new IOException("Could not rename file");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadPreviousGame() throws IOException {
File file = new File(Game.GAME_FILE);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = " ";
while ((line = reader.readLine()) != null) {
if(line.equals(Game.players.getCurrentPlayer().getUsername())) {
//Set phrase as the current line.
phrase = reader.readLine();
//Move to the next line of the file.
line = reader.readLine();
//Split line
String[] parts = line.split(",");
//Fill guess with appropriate mappings.
this.guess = new char[phrase.length()];
for (int i = 0; i < parts.length; i++) {
this.guess[i] = parts[i].charAt(0);
}
//Load the correct cipher for the puzzle.
mapping = new HashMap<>();
line = reader.readLine();
parts = line.split(",");
String split = null;
for (int i = 0; i < parts.length; i++) {
split = parts[i];
int intValue;
if(split.length() > 3) {
intValue = Integer.parseInt(split.substring(2, 4));
mapping.put(split.charAt(0), intValue);
}
else {
mapping.put(split.charAt(0), Integer.parseInt(split.substring(2)));
}
}
}
}
reader.close();
}
}