-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
199 lines (162 loc) · 7.03 KB
/
Main.java
File metadata and controls
199 lines (162 loc) · 7.03 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
import backend.*;
import backend.cipher.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
static Map<Integer, Class<? extends Cipher>> cipherList = new HashMap<>();
static {
// Substitution Ciphers
cipherList.put(1, CaesarCipher.class);
cipherList.put(2, Monoalphabetic.class);
cipherList.put(3, AtbashCipher.class);
cipherList.put(4, KeywordCipher.class);
/*cipherList.put(5, PlayfairCipher.class);
// Transposition Ciphers
cipherList.put(6, RailFenceCipher.class);
cipherList.put(7, ColumnarTransposition.class);
cipherList.put(8, RouteCipher.class);
// Polyalphabetic Ciphers
cipherList.put(9, VigenereCipher.class);
cipherList.put(10, BeaufortCipher.class);
cipherList.put(11, AutokeyCipher.class);
// Modern Ciphers
cipherList.put(12, AESCipher.class);
cipherList.put(13, RSACipher.class);
cipherList.put(14, DESCipher.class);
cipherList.put(15, BlowfishCipher.class); */
}
public static void startGame() {
Utility.delayedDisplayLn("* ~ * ~ [ Start Game ] ~ * ~ *", 0);
Utility.displayFormat('=', 30);
// prompt user to enter cipher of choice
int counter = 1;
// display list of available ciphers
for (Integer key: cipherList.keySet()) {
if (counter < 10) {
System.out.print(' ');
}
System.out.printf("[%d] %s", counter++, cipherList.get(key).getSimpleName());
System.out.println();
}
Utility.displayFormat('=', 30);
counter = 1; // reset counter
int cipherChoice = Data.getInt("Cipher: ", 1, 5);
String cipherName = cipherList.get(cipherChoice).getSimpleName();
Utility.displayFormat('=', 30);
// prompt user to enter # of problems
int problemCount = Data.getInt("# of Problems(Max: 5): ", 1, 5);
Utility.displayFormat('=', 30);
// prompt user to enter type of problem
String[] problemType = { "Decrypt", "Encrypt", "Mixed" };
for (String type : problemType) {
System.out.printf("[%d] %s\n", counter++, type);
} Utility.displayFormat('=', 30);
int problemTypeChoice = Data.getInt("Problem Type: ", 1, 3);
// start of game logic
int problemNumber = 1;
int score = 0;
while (true) {
while (problemNumber <= problemCount) {
String randomString = null;
Cipher cipher = null;
String answer, solution;
try {
randomString = Data.getRandomString(cipherName);
cipher = cipherList.get(cipherChoice).getDeclaredConstructor().newInstance();
Utility.clearScreen(10);
System.out.printf("[%s - Problem #%d]\n", cipherName, problemNumber++);
Utility.displayFormat('#', 30);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
if (cipher == null || randomString == null) {
System.err.println("Failed to initialize cipher or random string. Skipping this problem.");
continue;
}
int currentProblemType = problemTypeChoice;
// if mixed, map to either 0 or 1 (decrypt/encrypt)
if (problemTypeChoice == 3) {
int randomNumber = (int)Math.round(Math.random());
currentProblemType = (randomNumber == 1) ? 1 : 2;
}
if (currentProblemType == 1) {
// Decrypt
String encrypted = cipher.encrypt(randomString);
System.out.println("Encrypted: " + encrypted);
System.out.print("Answer: ");
answer = scanner.nextLine();
solution = randomString;
} else {
// Encrypt
String decrypted = cipher.decrypt(randomString);
System.out.println("Decrypted: " + decrypted);
System.out.print("Answer: ");
answer = scanner.nextLine();
solution = randomString;
}
Utility.displayFormat('#', 30);
// check if answer is correct
if (answer.trim().equalsIgnoreCase(solution.trim())) {
++score;
// update statistics
// updateStatistics("");
System.out.println("Correct! (+1 point)");
Utility.delay(2);
} else {
System.out.printf("Not quite... the answer is: %s\n", solution);
Utility.delay(2);
}
}
// show evaluation
Utility.clearScreen(10);
Utility.delayedDisplayLn("* ~ * ~ [ Evaluation ] ~ * ~ *", 0);
Utility.displayFormat('=', 30);
System.out.printf("Total Score: %d/%d\n", score, problemCount);
Utility.displayFormat('=', 30);
// prompt user whether to play again
System.out.print("Would you like to play again[y/n]?: ");
char userChoice = Data.getChar("");
if (userChoice == 'y') {
problemNumber = 1;
score = 0;
} else {
break;
}
}
}
public static void displayAchievements() {
Utility.delayedDisplayLn(" ~ * ~ [ Achievements ] ~ * ~ ", 0);
Utility.displayFormat('=', 30);
}
public static void displayStatistics() {
Utility.delayedDisplayLn("* ~ * ~ [ Statistics ] ~ * ~ *", 0);
Utility.displayFormat('=', 30);
}
public static void main(String[] args) {
int userChoice;
while (true) {
Utility.clearScreen(10);
Utility.delayedDisplayLn("~ * ~ * ~ [ nCrypT ] ~ * ~ * ~", 0.1);
Utility.displayFormat('=', 30);
Utility.delayedDisplayLn("[1] Start Game", 0.1);
Utility.delayedDisplayLn("[2] Achievements", 0.1);
Utility.delayedDisplayLn("[3] Statistics", 0.1);
Utility.delayedDisplayLn("[4] Exit", 0.1);
Utility.displayFormat('=', 30);
userChoice = Data.getInt(">> ", 1, 4);
Utility.clearScreen(10);
switch (userChoice) {
case 1 -> startGame();
case 2 -> displayAchievements();
case 3 -> displayStatistics();
case 4 -> {
Utility.delayedDisplay("exiting system...", 2);
System.exit(0);
}
} Utility.pressEnterToContinue();
}
}
}