-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingGenerator.java
185 lines (159 loc) · 4.6 KB
/
SettingGenerator.java
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
/* Project Title: Enigma
* Description: Outputs randomly generated Enigma keys into a text file
*
* Created by: Isaac Newell
* Date: 04/22/2017
*/
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.Math;
import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
public class SettingGenerator
{
// Selects the rotor order
public static String rotors()
{
String[] ws = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII"};
ArrayList<String> choices = new ArrayList<String>(ws.length);
for (String s : ws)
{
choices.add(s);
}
String output = "";
for (int i = 1; i <= 3; i++)
{
int index = (int)(Math.random()*choices.size());
output += choices.get(index);
if (i != 3) output += " ";
// Removes each choice because there can be no duplicates
choices.remove(index);
}
return output;
}
// Randomly selects a reflector
public static String reflector()
{
String[] choices = {"A", "B", "C"};
int index = (int)(Math.random() * choices.length);
return choices[index];
}
// Randomly selects ring settings
public static String ringSettings()
{
String output = "";
for (int i = 1; i <= 3; i++)
{
int s = (int)(Math.random() * 26 + 1);
String setting = s + "";
if (s < 10)
setting = "0" + setting;
output += setting;
if (i != 3) output += " ";
}
return output;
}
// Randomly selects ground settings (start positions)
public static String groundSettings()
{
String output = "";
for (int i = 1; i <= 3; i++)
{
int s = (int)(Math.random() * 26 + 65);
String setting = String.valueOf((char)s);
output += setting;
if (i != 3) output += " ";
}
return output;
}
// Randomly selects plugboard settings
public static String plugs()
{
ArrayList<String> choices = new ArrayList<String>(26);
for (char c = 65; c <= 90; c++)
{
choices.add(String.valueOf(c));
}
String plugs = "";
for (int i = 1; i <= 10; i++)
{
// Current pair of letters
String pair = "";
// Randomly pick two distinct letters to pair and remove them from choices
int index1 = (int)(Math.random() * choices.size());
pair += choices.get(index1);
choices.remove(index1);
int index2 = (int)(Math.random() * choices.size());
pair += choices.get(index2);
choices.remove(index2);
//Add a space
pair += " ";
// Insert the pair into plugs alphabetically by first character
// (i.e. AP BQ DM ...)
boolean inserted = false;
for (int j = 0; j < plugs.length(); j += 3)
{
if (pair.compareTo(plugs.substring(j, j+1)) < 0)
{
plugs = plugs.substring(0, j) + pair + plugs.substring(j);
inserted = true;
break;
}
}
// If no spot in the middle put at the end
if (!inserted)
{
plugs += pair;
}
}
return plugs.substring(0, plugs.length()-1);
}
// Uses commandline input to generate settings for a speficied number of
// days to a specified text file
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter number of days: ");
String days = kb.nextLine();
int numDays = Integer.parseInt(days);
System.out.print("Enter output file name: ");
String fileName = kb.nextLine();
kb.close();
try
{
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);
pw.println("Enigma Machine Daily Key Settings");
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
Date d = new Date();
cal.setTime(d);
String date = dateFormat.format(d);
pw.println("Daily Key Settings for " + numDays + " days starting " + date);
pw.println();
for (int day = 1; day <= numDays; day++)
{
// Puts the proper date stamp on each key
date = dateFormat.format(cal.getTime());
pw.println("Day " + day + " (" + date + ")");
pw.println("Rotors: " + rotors());
pw.println("Reflector: " + reflector());
pw.println("Ring: " + ringSettings());
pw.println("Ground: " + groundSettings());
pw.println("Plugs: " + plugs());
pw.println();
cal.add(Calendar.DATE, 1);
}
pw.close();
}
catch (IOException e)
{
}
}
}