-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCryptogram.java
More file actions
234 lines (194 loc) · 7.29 KB
/
Copy pathLetterCryptogram.java
File metadata and controls
234 lines (194 loc) · 7.29 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
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.Map.Entry;
public class LetterCryptogram extends Cryptogram {
private Map<Character, Character> mapping;
public LetterCryptogram(String phrase) {
super(phrase);
mapping = new HashMap<>();
generateMapping();
}
public LetterCryptogram() {
this(getRandomPhrase());
}
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();
}
private void generateMapping() {
List<String> temp = Arrays.asList(Cryptogram.ALPHABET.split(""));
Collections.shuffle(temp);
for (int i = 0; i < Cryptogram.ALPHABET.length(); i++) {
mapping.put(Cryptogram.ALPHABET.charAt(i), temp.get(i).charAt(0));
}
}
public boolean guess(String place, char value) {
char letter = place.charAt(0);
if (!Character.isAlphabetic(letter)) {
return false;
}
boolean containsLetter = false;
for (int i = 0; i < phrase.length(); i++) {
if (!mapping.containsKey(phrase.charAt(i)))
continue;
containsLetter = containsLetter || mapping.get(phrase.charAt(i)) == letter;
}
if (!containsLetter)
return false;
for (int i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (Character.isAlphabetic(c) && mapping.get(c) == letter) {
guess[i] = value;
}
}
return true;
}
public boolean undo(String place) {
char letter = place.charAt(0);
if (!Character.isAlphabetic(letter)) {
return false;
}
boolean containsLetter = false;
for (int i = 0; i < phrase.length(); i++) {
if (!mapping.containsKey(phrase.charAt(i)))
continue;
containsLetter = containsLetter || mapping.get(phrase.charAt(i)) == letter;
}
if (!containsLetter)
return false;
for (int i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (Character.isAlphabetic(c) && mapping.get(c) == letter) {
if (guess[i] == ' ') {
return false;
}
guess[i] = ' ';
}
}
return true;
}
public void render() {
for (char c : phrase.toCharArray()) {
if (Character.isAlphabetic(c)) {
System.out.print(mapping.get(c) + " ");
} 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 {
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;
return phrase.charAt(firstIndex) == guess[firstIndex];
}
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 (Entry<Character, Character> 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 (Entry<Character, Character> 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);
}
//Move to the next line of the file.
//Load the correct cipher for the puzzle.
mapping = new HashMap<>();
line = reader.readLine();
parts = line.split(",");
String[] split;
for (int i = 0; i < parts.length; i++) {
split = parts[i].split("|");
mapping.put(split[0].charAt(0), split[2].charAt(0));
}
}
}
reader.close();
}
}