-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexagonyPrinter.java
340 lines (319 loc) · 9.32 KB
/
HexagonyPrinter.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//Takes input in the form of a single argument
import java.util.*;
class HexagonyPrinter
{
static final boolean MINIFY_SOURCE = true;
static final String CHARSET = "UTF-8";
static final char[] SPECIAL_CHARS = {
'\t', '\n', '', '\f', '\r', ' ', '!',
'\"', '#', '$', '%', '&', '\'', '(',
')', '*', '+', ',', '-', '.', '/',
':', ';', '<', '=', '>', '?', '@',
'[', '\\', ']', '^', '_', '`', '{',
'|', '}', '~'};
static final char[] NUMBERS = {
'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9'};
public static void main(String[] args)
{
if(args.length < 1)
{
System.out.print("No arguments were inputted");
System.exit(0);
}
int[] bytes = getBytes(args[0]);
String hexagonyCode = generateHexagonyCode(bytes);
System.out.print(hexagonyCode);
}
private static String generateHexagonyCode(int[] dataBytes)
{
int sideLength = getSideLength(dataBytes.length);
char[][] hexCodeArray = makeEmptyHexagon(sideLength);
hexCodeArray = addOps(hexCodeArray,sideLength);
hexCodeArray = addData(hexCodeArray,dataBytes,sideLength);
hexCodeArray = replaceNumbers(hexCodeArray,sideLength);
String hexCodeString = writeOutputString(sideLength, hexCodeArray);
return hexCodeString;
}
private static char[][] replaceNumbers(char[][] hexGrid, int sideLength)
{
for(int row = 0; row < hexGrid.length; row++)
{
for(int col = 0; col < hexGrid[row].length; col++)
{
if(hexGrid[row][col]==';')
{
char p1 = hexGrid[row][col - 1];
char p2 = hexGrid[row + 1][col + (row >= sideLength - 1 ? 0 : 1)];
char p3 = hexGrid[row - 1][col + (row > sideLength - 1 ? 1 : 0)];
boolean allNums =
(p1 >= ('1'+256) && p1 <= ('8'+256)) &&
(p2 >= ('1'+256) && p2 <= ('8'+256)) &&
(p3 >= ('1'+256) && p3 <= ('8'+256));
if(allNums)
{
hexGrid[row][col] = '!';
hexGrid[row][col - 1] = (char)(hexGrid[row][col - 1]-304);
hexGrid[row + 1][col + (row >= sideLength - 1 ? 0 : 1)] = (char)(hexGrid[row + 1][col + (row >= sideLength - 1 ? 0 : 1)]-304);
hexGrid[row - 1][col + (row > sideLength - 1 ? 1 : 0)] = (char)(hexGrid[row - 1][col + (row > sideLength - 1 ? 1 : 0)]-304);
}
}
}
}
return hexGrid;
}
private static String writeOutputString(int sideLength, char[][] hexGrid) {
String ret = "";
if(MINIFY_SOURCE)
{
for(char[] i : hexGrid)
for(char j : i)
ret+=""+j;
ret=ret.substring(0,ret.length()-2);
}
else
for(char[] i : hexGrid)
{
for(int j = 0; j < sideLength * 2 - i.length; j++)
ret+=" ";
for(char j : i)
ret += " " + j;
ret += "\n";
}
return ret;
}
private static char[][] addData(char[][] hexGrid,int[] dataBytes,int sideLength)
{
boolean pastDataEnd = false;
String direction = "E";
int currentRow = 0;
int currentCol = 0;
char currentChr = '.';
int lastRow = 0;
int lastCol = 0;
char lastChr = '.';
int dataIndex = 0;
char memValue = '.';
while(true)
{
if(currentChr == '@' && lastChr != '$')
{break;}
if(currentChr == ';' && lastChr == '.')
{
if(dataIndex<dataBytes.length)
hexGrid[lastRow][lastCol] = (char)dataBytes[dataIndex++];
else if(memValue != 'Ġ')
{
hexGrid[lastRow][lastCol] = 'Ġ';
}
else if(!pastDataEnd)
{
hexGrid[lastRow][lastCol] = 'Ġ';
pastDataEnd = true;
}
}
else if (currentChr == '/')
{
direction =
direction.equals("E") ? "NW" :
direction.equals("SE") ? "W" :
direction.equals("SW") ? "SW" :
direction.equals("W") ? "SE" :
direction.equals("NW") ? "E" :
direction.equals("NE") ? "NE" :
"Error";
}
else if (currentChr == '_')
{
direction =
direction.equals("E") ? "E" :
direction.equals("SE") ? "NE" :
direction.equals("SW") ? "NW" :
direction.equals("W") ? "W" :
direction.equals("NW") ? "SW" :
direction.equals("NE") ? "SE" :
"Error";
}
else if (currentChr == '\\')
{
direction =
direction.equals("E") ? "SW" :
direction.equals("SE") ? "SE" :
direction.equals("SW") ? "E" :
direction.equals("W") ? "NE" :
direction.equals("NW") ? "NW" :
direction.equals("NE") ? "W" :
"Error";
}
lastRow = currentRow;
lastCol = currentCol;
lastChr = currentChr;
if(direction == "E")
{
currentCol++;
if(currentCol > hexGrid[currentRow].length - 1)
{
currentCol = 0;
if(currentRow == sideLength - 1)
currentRow = hexGrid.length - 1;
else if(currentRow == 0 || currentRow == hexGrid.length - 1)
currentRow = sideLength - 1;
else if(currentRow > sideLength - 1)
currentRow = currentRow - (sideLength - 1);
else if(currentRow < sideLength - 1)
currentRow = currentRow + (sideLength - 1);
}
}
else if(direction == "W")
{
currentCol--;
if(currentCol < 0)
{
if(currentRow == sideLength - 1)
currentRow = 0;
else if(currentRow == 0 || currentRow == hexGrid.length - 1)
currentRow = sideLength - 1;
else if(currentRow>sideLength - 1)
currentRow = currentRow - (sideLength - 1);
else if(currentRow<sideLength - 1)
currentRow=currentRow + (sideLength - 1);
currentCol = hexGrid[currentRow].length;
}
}
else if(direction == "NE")
{
if(currentRow > sideLength - 1)
currentCol++;
currentRow--;
if(currentRow < 0)
currentRow = hexGrid.length-1;
else if(currentCol > hexGrid[currentRow].length)
{
currentCol = 0;
currentRow = sideLength + currentRow;
}
}
else if(direction == "SE")
{
if(currentRow < sideLength - 1)
currentCol++;
currentRow++;
if(currentRow > hexGrid.length - 1)
currentRow = 0;
else if(currentCol > hexGrid[currentRow].length)
{
currentCol = 0;
currentRow = currentRow - sideLength + 1;
}
}
else if(direction == "NW")
{
if(currentRow <= sideLength - 1 && currentRow != 0)
currentCol--;
currentRow--;
if(currentRow < 0)
currentRow = hexGrid.length - 1;
if(currentCol < 0)
{
currentRow = sideLength + currentRow;
currentCol = hexGrid[currentRow].length - 1;
}
}
else if(direction == "SW")
{
if(currentRow >= sideLength - 1 && currentRow != hexGrid.length - 1)
currentCol--;
currentRow++;
if(currentRow > hexGrid.length - 1)
currentRow = 0;
else if(currentCol < 0)
{
currentRow = currentRow - sideLength;
currentCol = hexGrid[currentRow].length - 1;
}
}
boolean newMemValue = true;
for (char i : SPECIAL_CHARS)
newMemValue = newMemValue && i != currentChr;
if (newMemValue)
memValue = currentChr;
currentChr = hexGrid[currentRow][currentCol];
}
return hexGrid;
}
private static char[][] addOps(char[][] hexCodeArray, int sideLength)
{
//Add Mirrors
for(int i=0;i<sideLength-1;i++)
hexCodeArray[i][0]='_';
for(int i=1;i<sideLength;i++)
hexCodeArray[i][hexCodeArray[i].length-1]='/';
for(int i=0;i<hexCodeArray[hexCodeArray.length-1].length-2;i++)
hexCodeArray[hexCodeArray.length-1][i]='\\';
//Add printers and terminator
int row = 2, col = 2;
for(row = 2; row < sideLength * 2 - 2; row += 2)
for(col = 2; col < hexCodeArray[row].length - 2; col += 2)
hexCodeArray[row][col] = ';';
row -= 2;
col -= 2;
hexCodeArray[row][col] = '@';
hexCodeArray[row][col-1] = '$';
hexCodeArray[row+1][col] = '$';
return hexCodeArray;
}
private static char[][] makeEmptyHexagon(int sideLength)
{
char[][] hexCodeArray = new char[sideLength * 2 - 1][];
for(int i = 0; i < sideLength; i++)
hexCodeArray[i] = new char[sideLength + i];
for(int i = 0; i < sideLength - 1; i++)
hexCodeArray[sideLength + i] = new char[2 * sideLength - i - 2];
for (char[] i: hexCodeArray)
Arrays.fill(i, '.');
return hexCodeArray;
}
private static int getSideLength(int length)
{
//The number of print operators will we a centered hexagonal number minus 1.
//Each print operator can print 3 bytes.
//The side length of the entire hexagon is 2 times the length of the
// hexagon formed by the print operators plus one.
//Encoding for even-numbered side lengths isn't implemented yet.
int i = 1;
while(true)
{
int centeredHexNum = (3 * i * (i - 1)) + 1;
if((centeredHexNum - 1) * 3 >= length)
return i*2+1;
i++;
}
}
private static int[] getBytes(String in)
{
byte[] bytes = {};
try
{
bytes = in.getBytes(CHARSET);
}
catch (java.io.UnsupportedEncodingException e)
{
e.printStackTrace();
}
int[] ret = new int[bytes.length];
for(int i = 0; i < bytes.length; i++)
{
int byteAsInt = (int)bytes[i];
if (byteAsInt < 0)
byteAsInt += 256;
for(int j : SPECIAL_CHARS)
if(byteAsInt == j)
byteAsInt += 256;
for(int j : NUMBERS)
if(byteAsInt == j)
byteAsInt += 256;
ret[i] = byteAsInt;
}
return ret;
}
}