| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class Life { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static void main(String[] args){ |
| 34 |
|
| 35 |
Timer t=new Timer(); |
| 36 |
|
| 37 |
|
| 38 |
Life x=new Life(args); |
| 39 |
|
| 40 |
|
| 41 |
x.process(t); |
| 42 |
|
| 43 |
|
| 44 |
Shared.closeStream(x.outstream); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public Life(String[] args){ |
| 52 |
|
| 53 |
{ |
| 54 |
PreParser pp=new PreParser(args, null, false); |
| 55 |
args=pp.args; |
| 56 |
outstream=pp.outstream; |
| 57 |
} |
| 58 |
|
| 59 |
{ |
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return parser; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
int changes=advance(); |
| 178 |
|
| 179 |
|
| 180 |
|
| 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 |
|
| 196 |
} |
| 197 |
linesOut++; |
| 198 |
bytesOut+=buffer.length; |
| 199 |
} |
| 200 |
delay(delay); |
| 201 |
return changes; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 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 |
|
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 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 |
|
| 250 |
return changed; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 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 |
|
| 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 |
|
| 295 |
e.printStackTrace(); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 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}, |
| 324 |
{0, 0, 0, 1, 1, 0, 0, 0, 0, 0} |
| 325 |
}; |
| 326 |
|
| 327 |
private final byte[] charMap=new byte[] {' ', '@'}; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
private long linesOut=0; |
| 333 |
private long bytesOut=0; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
private final FileFormat ffout; |
| 340 |
|
| 341 |
|
| 342 |
|
| 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 |
} |