Debian Codegraph @

current/driver/Life.java

raw symbols SCIP
1 package driver;
2
3 import java.io.PrintStream;
4 import java.util.Random;
5
6 import fileIO.ByteFile1;
7 import fileIO.ByteFile2;
8 import fileIO.FileFormat;
9 import fileIO.ReadWrite;
10 import fileIO.TextStreamWriter;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Shared;
15 import shared.Timer;
16 import shared.Tools;
17
18 /**
19 * @author Brian Bushnell
20 * @date April 9, 2020
21 *
22 */
23 public class Life {
24
25 /*--------------------------------------------------------------*/
26 /*---------------- Initialization ----------------*/
27 /*--------------------------------------------------------------*/
28
29 /**
30 * Code entrance from the command line.
31 * @param args Command line arguments
32 */
33 public static void main(String[] args){
34 //Start a timer immediately upon code entrance.
35 Timer t=new Timer();
36
37 //Create an instance of this class
38 Life x=new Life(args);
39
40 //Run the object
41 x.process(t);
42
43 //Close the print stream if it was redirected
44 Shared.closeStream(x.outstream);
45 }
46
47 /**
48 * Constructor.
49 * @param args Command line arguments
50 */
51 public Life(String[] args){
52
53 {//Preparse block for help, config files, and outstream
54 PreParser pp=new PreParser(args, /*getClass()*/null, false);
55 args=pp.args;
56 outstream=pp.outstream;
57 }
58
59 {//Parse the arguments
60 final Parser parser=parse(args);
61 parser.out1="stdout.txt";
62 overwrite=parser.overwrite;
63 append=parser.append;
64
65 out=parser.out1;
66 }
67
68 ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false);
69
70 current=new byte[rows][columns];
71 prev=new byte[rows][columns];
72 next=prev;
73
74 xMax=columns-1;
75 yMax=rows-1;
76 bsw=makeBSW(ffout);
77 buffer=new byte[columns+1];
78 buffer[columns]='\n';
79 }
80
81 /*--------------------------------------------------------------*/
82 /*---------------- Initialization Helpers ----------------*/
83 /*--------------------------------------------------------------*/
84
85 /** Parse arguments from the command line */
86 private Parser parse(String[] args){
87
88 Parser parser=new Parser();
89 for(int i=0; i<args.length; i++){
90 String arg=args[i];
91 String[] split=arg.split("=");
92 String a=split[0].toLowerCase();
93 String b=split.length>1 ? split[1] : null;
94 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
95
96 if(a.equals("verbose")){
97 verbose=Parse.parseBoolean(b);
98 ByteFile1.verbose=verbose;
99 ByteFile2.verbose=verbose;
100 ReadWrite.verbose=verbose;
101 }else if(a.equalsIgnoreCase("x") || a.equalsIgnoreCase("width") || a.equalsIgnoreCase("columns")){
102 columns=Parse.parseIntKMG(b);
103 }else if(a.equalsIgnoreCase("y") || a.equalsIgnoreCase("height") || a.equalsIgnoreCase("rows")){
104 rows=Parse.parseIntKMG(b);
105 }else if(a.equalsIgnoreCase("cycles") || a.equalsIgnoreCase("rounds")){
106 maxCycles=Parse.parseKMG(b);
107 }else if(a.equalsIgnoreCase("prob") || a.equalsIgnoreCase("load")){
108 prob=Float.parseFloat(b);
109 }else if(a.equalsIgnoreCase("display")){
110 display=Parse.parseBoolean(b);
111 }else if(a.equalsIgnoreCase("interval") || a.equalsIgnoreCase("delay")){
112 delay=Parse.parseIntKMG(b);
113 }else{
114 outstream.println("Unknown parameter "+args[i]);
115 assert(false) : "Unknown parameter "+args[i];
116 // throw new RuntimeException("Unknown parameter "+args[i]);
117 }
118 }
119
120 return parser;
121 }
122
123 /*--------------------------------------------------------------*/
124 /*---------------- Outer Methods ----------------*/
125 /*--------------------------------------------------------------*/
126
127 void process(Timer t){
128 init();
129
130 processInner();
131
132 if(bsw!=null){errorState|=bsw.poisonAndWait();}
133
134 t.stop();
135
136 outstream.println(Tools.timeLinesBytesProcessed(t, linesOut, bytesOut, 8));
137 outstream.println();
138
139 if(errorState){
140 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
141 }
142 }
143
144 /*--------------------------------------------------------------*/
145 /*---------------- Inner Methods ----------------*/
146 /*--------------------------------------------------------------*/
147
148 private void init(){
149 Random randy=Shared.threadLocalRandom();
150 for(int y=0; y<rows; y++){
151 for(int x=0; x<columns; x++){
152 prev[y][x]=(byte)(randy.nextFloat()<=prob ? 1 : 0);
153 }
154 }
155 }
156
157 private void processInner(){
158
159 if(display){
160 System.out.print("\033[2J\033[1;1H");
161 // bsw.print("\033[2J\033[1;1H");
162 }
163 for(long cycle=0; cycle<maxCycles; cycle++){
164 runCycle(cycle);
165 }
166 }
167
168 private boolean runCycle(long cycle){
169
170 if(display){
171 System.out.print("\033[1;1H");
172 System.out.println("cycle="+cycle);
173 // bsw.println("\033[1;1H");
174 // bsw.println(""+cycle);
175 }
176
177 int changes=advance();
178
179 // printMatrix(prev); //For testing
180 // printMatrix(current); //For testing
181
182 prev=current;
183 current=next;
184 next=prev;
185
186 return changes>0;
187 }
188
189 private int advance(){
190 int changes=0;
191 for(int y=0; y<rows; y++){
192 changes+=fillRow(y);
193 if(display){
194 printRow2(current[y]);
195 // printRow(current[y]);
196 }
197 linesOut++;
198 bytesOut+=buffer.length;
199 }
200 delay(delay);
201 return changes;
202 }
203
204 // //For testing
205 // private void printMatrix(byte[][] matrix){
206 // System.out.println("-----------------");
207 // for(int i=0; i<matrix.length; i++){
208 // System.out.println(Arrays.toString(matrix[i]));
209 // }
210 // System.out.println("-----------------");
211 // }
212
213 private int fillRow(int y){
214 final byte[] dest=current[y];
215 final byte[] a=getRow(y-1, prev);
216 final byte[] b=getRow(y, prev);
217 final byte[] c=getRow(y+1, prev);
218 int sum;
219 int changed=0;
220
221 // printMatrix(new byte[][] {a, b, c});
222
223 //Calculate first cell
224 sum=a[xMax]+b[xMax]+c[xMax];
225 sum+=a[0]+b[0]+c[0];
226 sum+=a[1]+b[1]+c[1];
227 dest[0]=stateMap[b[0]][sum];
228 sum-=(a[xMax]+b[xMax]+c[xMax]);
229
230 //Calculate middle cells
231 for(int x=1 ; x<xMax; x++){
232 final int left=x-1, right=x+1;
233 sum+=(a[right]+b[right]+c[right]);
234 final byte prevState=b[x];
235 final byte nextState=stateMap[prevState][sum];
236 dest[x]=nextState;
237 sum-=(a[left]+b[left]+c[left]);
238 // System.out.println(prevState+" + "+sum+" -> "+nextState);
239 // changed+=(ns^ps);//1 if something changed
240 }
241 // assert(false);
242
243 //Calculate last cell
244 sum+=a[0]+b[0]+c[0];
245 final byte prevState=b[0];
246 final byte nextState=stateMap[prevState][sum];
247 dest[xMax]=nextState;
248
249 // changed+=(ns^ps);//1 if something changed
250 return changed;
251 }
252
253 //Using bsw
254 private void printRow(byte[] row){
255 for(int i=0; i<row.length; i++){
256 byte state=row[i];
257 buffer[i]=charMap[state];
258 }
259 bsw.print(new String(buffer));
260 linesOut++;
261 bytesOut+=row.length;
262 }
263
264 //Using System.out
265 private void printRow2(byte[] row){
266 for(int i=0; i<row.length; i++){
267 byte state=row[i];
268 buffer[i]=charMap[state];
269 }
270 System.out.print(new String(buffer));
271 linesOut++;
272 bytesOut+=row.length;
273 }
274
275 private byte[] getRow(int y, byte[][] matrix){
276 return y<0 ? matrix[yMax] : y>yMax ? matrix[0] : matrix[y];
277 }
278
279 private static TextStreamWriter makeBSW(FileFormat ff){
280 if(ff==null){return null;}
281 TextStreamWriter bsw=new TextStreamWriter(ff);
282 bsw.start();
283 return bsw;
284 }
285
286 private void delay(int millis){
287 if(millis<1){return;}
288
289 long until=System.currentTimeMillis()+millis;
290 while(System.currentTimeMillis()<until){
291 try {
292 Thread.sleep(millis);
293 } catch (InterruptedException e) {
294 // TODO Auto-generated catch block
295 e.printStackTrace();
296 }
297 }
298 }
299
300 /*--------------------------------------------------------------*/
301 /*---------------- Fields ----------------*/
302 /*--------------------------------------------------------------*/
303
304 private String out=null;
305 private long maxCycles=100;
306
307 private int columns=50;
308 private int rows=20;
309 private float prob=0.25f;
310 private boolean display=true;
311 private int delay=0;
312
313 private final int xMax, yMax;
314 private final byte[] buffer;
315
316 private final TextStreamWriter bsw;
317
318 private byte[][] current;
319 private byte[][] prev;
320 private byte[][] next;
321
322 private final byte[][] stateMap=new byte[][] {
323 {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, //Dead
324 {0, 0, 0, 1, 1, 0, 0, 0, 0, 0} //Live (Add 1)
325 };
326
327 private final byte[] charMap=new byte[] {' ', '@'};
328 // private final byte[] charMap=new byte[] {' ', block}; //Eclipse can't handle block character
329
330 /*--------------------------------------------------------------*/
331
332 private long linesOut=0;
333 private long bytesOut=0;
334
335 /*--------------------------------------------------------------*/
336 /*---------------- Final Fields ----------------*/
337 /*--------------------------------------------------------------*/
338
339 private final FileFormat ffout;
340
341 /*--------------------------------------------------------------*/
342 /*---------------- Common Fields ----------------*/
343 /*--------------------------------------------------------------*/
344
345 private PrintStream outstream=System.err;
346 public static boolean verbose=false;
347 public boolean errorState=false;
348 private boolean overwrite=true;
349 private boolean append=false;
350
351 }