-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinFileWriter.java
289 lines (231 loc) · 8.01 KB
/
BinFileWriter.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
import java.io.*;
import static java.math.BigInteger.valueOf;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
public class BinFileWriter extends Follower {
public static void WriteLab(String fileName, int[][] Lab, int Columns, int Rows, int EnterX, int EnterY, int ExitX, int ExitY) throws IOException {
File file = new File(fileName);
FileOutputStream fileOut = new FileOutputStream(file);
RandomAccessFile raf = new RandomAccessFile(file, "rws");
int FILEID = 0x43425252;
byte[] fileid = valueOf(FILEID).toByteArray();
fileOut.write(fileid, 0, 4);
byte ESC = 27;
fileOut.write(ESC);
//hardcode
byte[] byteArray = new byte[2];
byteArray[0] = (byte) (Columns & 0xFF);
byteArray[1] = (byte) ((Columns >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
byteArray[0] = (byte) (Rows & 0xFF);
byteArray[1] = (byte) ((Rows >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
byteArray[0] = (byte) (EnterX+1 & 0xFF);
byteArray[1] = (byte) ((EnterX+1 >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
byteArray[0] = (byte) (EnterY+1 & 0xFF);
byteArray[1] = (byte) ((EnterY+1 >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
byteArray[0] = (byte) (ExitX+1 & 0xFF);
byteArray[1] = (byte) ((ExitX+1 >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
byteArray[0] = (byte) (ExitY+1 & 0xFF);
byteArray[1] = (byte) ((ExitY+1 >> 8) & 0xFF);
fileOut.write(byteArray, 0, 2);
int REZ = 0xff000000;
byte[] reze = valueOf(REZ).toByteArray();
for (int i = 0; i < 5; i++) {
fileOut.write(reze, 0, 4);
}
byte Separator = 35;
fileOut.write(Separator);
byte Wall = 88;
fileOut.write(Wall);
byte Path = 32;
fileOut.write(Path);
int counter = -1;
int words = 0;
int sign = Lab[0][0];
Lab[EnterX+1][EnterY+1] = 0;
Lab[ExitX][ExitY] = 0;
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++) {
if (Lab[j][i] == sign) {
if (counter == 255) {
if (sign == 0) {
fileOut.write(Separator);
fileOut.write(Path);
fileOut.write(counter);
words++;
} else {
fileOut.write(Separator);
fileOut.write(Wall);
fileOut.write(counter);
words++;
}
counter = -1;
}
counter++;
} else if (Lab[j][i] != sign) {
if (sign == 0) {
fileOut.write(Separator);
fileOut.write(Path);
fileOut.write(counter);
words++;
} else {
fileOut.write(Separator);
fileOut.write(Wall);
fileOut.write(counter);
words++;
}
sign = Lab[j][i];
counter = 0;
}
}
}
fileOut.write(Separator);
fileOut.write(Wall);
fileOut.write(counter);
words++;
words = words;
raf.seek(0);
raf.skipBytes(29);
byte[] bytecount = new byte[4];
bytecount[3] = (byte)(words >>> 24);
bytecount[2] = (byte)(words >>> 16);
bytecount[1] = (byte)(words >>> 8);
bytecount[0] = (byte)words;
raf.write(bytecount[0]);
raf.write(bytecount[1]);
raf.write(bytecount[2]);
raf.write(bytecount[3]);
Lab[EnterX][EnterY] = 2;
Lab[ExitX][ExitY] = 3;
fileOut.close();
raf.close();
}
public static void WriteSolve(String fileName, ArrayList<Integer> coords, int EnterX, int EnterY) throws IOException {
RandomAccessFile raf = new RandomAccessFile(fileName, "rws");
long size = raf.length();
int siz = (int) size;
raf.seek(33);
raf.writeInt(siz);
raf.seek(size);
raf.writeInt(0x43425252); //hardcode
raf.writeByte(0x00);
int x = EnterX;
int y = EnterY;
byte Direction = 69;
int steps = -1;
int count = -1;
if (x < coords.get(0)) {
Direction = 69; //E
} else if (y < coords.get(1)) {
Direction = 83; //S
} else if (x > coords.get(0)) {
Direction = 87; //W
} else {
Direction = 78; //N
}
for (int i = 0; i < coords.size(); i++) {
byte tmp;
if (x < coords.get(i)) {
tmp = 69; //E
} else if (y < coords.get(i + 1)) {
tmp = 83; //S
} else if (x > coords.get(i)) {
tmp = 87; //W
} else {
tmp = 78; //N
}
if (Direction == tmp) {
steps++;
if (steps == 255) {
raf.writeByte(steps);
raf.writeByte(Direction);
steps = -1;
count++;
}
} else {
raf.writeByte(Direction);
raf.writeByte(steps);
Direction = tmp;
steps = 0;
count++;
}
x = coords.get(i);
y = coords.get(i + 1);
i++;
}
raf.writeByte(Direction);
raf.writeByte(steps);
count++;
raf.seek(size + 4);
raf.writeByte(count);
raf.close();
}
/*public static void main(String[] args) {
String inputFile = args[0];
int[][] Lab = null;
short[] Data = null;
try {
Data = Data(inputFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
int Columns = Data[0];
int Rows = Data[1];
int EnterX = Data[2];
int EnterY = Data[3];
int ExitX = Data[4];
int ExitY = Data[5];
System.out.println("Columns: " + Columns);
System.out.println("Rows: " + Rows);
System.out.println("EnterX: " + EnterX);
System.out.println("EnterY: " + EnterY);
System.out.println("ExitX: " + ExitX);
System.out.println("ExitY: " + ExitY);
try {
Lab = BinToInt(inputFile,Columns,Rows,EnterX,EnterY,ExitX,ExitY);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
WriteLab("proba.bin", Lab, Columns, Rows, EnterX-1, EnterY-1, ExitX-1, ExitY-1);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("tu");
try {
Data = Data("proba.bin");
} catch (IOException e) {
throw new RuntimeException(e);
}
Columns = Data[0];
Rows = Data[1];
EnterX = Data[2];
EnterY = Data[3];
ExitX = Data[4];
ExitY = Data[5];
System.out.println("Columns: " + Columns);
System.out.println("Rows: " + Rows);
System.out.println("EnterX: " + EnterX);
System.out.println("EnterY: " + EnterY);
System.out.println("ExitX: " + ExitX);
System.out.println("ExitY: " + ExitY);
System.out.println("tu");
try {
Lab = BinToInt("proba.bin",Columns,Rows,EnterX,EnterY,ExitX,ExitY);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++) {
System.out.print(+Lab[j][i]);
}
System.out.println();
}
}*/
}