// life.java Conway's Game of Life // W.B.Langdon@cs.bham.ac.uk 5 Feb 1997 // $Revision: 1.13 $ package life; //Modifications (in reverse order) //WBL 1 Mar 1997 Allow use as stand alone application and network files //WBL 25 Feb 1997 put into a package //WBL 5 Feb 1997 new file import java.awt.Graphics; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Color; import java.awt.Point; import java.awt.Event; import java.awt.Frame; import java.util.Date; import java.io.*; import java.net.URL; import java.net.MalformedURLException; class pair { int x = 0; int y = 0; pair (int xx, int yy) {x=xx; y=yy;}; pair (int xx) {x=xx; y=xx;}; boolean equal(pair b) {return (this.x==b.x) && (this.y==b.y);}; boolean within(pair a, pair b) { return (this.x>=a.x) && (this.y>=a.y) && (this.x< b.x) && (this.y< b.y); } pair(String xy) throws Exception { int sep = xy.indexOf('x'); x = Integer.parseInt(xy.substring(0,sep)); y = Integer.parseInt(xy.substring(sep+1,xy.length())); } }//end class pair class game { boolean[][] board; // int[][] debug; pair dim = new pair(0,0); // int max = 0; game(pair size, double init_fraction) { dim.x = size.x; dim.y = size.y; board = new boolean[dim.x][dim.y]; // debug = new int[dim.x][dim.y]; for(int i = 0; i < dim.x; i++) for(int j = 0; j < dim.y; j++) { board[i][j] = (Math.random() < init_fraction); } } game(game in, pair size, double init_fraction) { dim.x = size.x; dim.y = size.y; board = new boolean[dim.x][dim.y]; // debug = new int[dim.x][dim.y]; for(int i = 0; i < dim.x; i++) for(int j = 0; j < dim.y; j++) { board[i][j] = (in!=null && i='0' && line.charAt(i)<='9')) { break; } } String s1 = line.substring(i); int sep = s1.indexOf(' '); String n1 = s1.substring(0,sep); String n2 = s1.substring(sep+1); int x = Math.abs(Integer.parseInt(n1)); int y = Math.abs(Integer.parseInt(n2)); file_dim = new pair(x,y); } //System.err.println("File dimensions "+file_dim.x+"x"+file_dim.y); boolean temp_board[][] = new boolean[file_dim.x][file_dim.y]; for(int yy=0; yyin.dim.x)? file_dim.x:in.dim.x, in.dim.y+file_dim.y); board = new boolean[dim.x][dim.y]; for(int xx=0;xx=0 && i =0 && j 3)? false : board[i][j]; */ switch(neighbours(i,j)){ case 0: case 1: case 4: case 5: case 6: case 7: case 8: nextboard[i][j] = false; break; case 2: nextboard[i][j] = board[i][j]; break; case 3: nextboard[i][j] = true; break; default: //nextboard[i][j] = 99; //bug } }} {for(int i = 0; i < dim.x; i++) for(int j = 0; j < dim.y; j++) { board[i][j] = nextboard[i][j]; }} }//end update() }//end class game class date extends java.applet.Applet { private Font f = new Font ("TimesRoman", Font.BOLD, 36); FontMetrics fm = getFontMetrics(f); Date theDate; String s; pair size; public void update() { theDate = new Date(); s = theDate.toString(); size = new pair(fm.stringWidth(s),fm.getHeight()); } date() {update();}; void drawdate(Graphics g, int x, int y) { g.setFont(f); // g.setColor(getBackground()); g.setColor(getForeground()); // g.setColor(Color.blue); g.fillRect(x,y-size.y,size.x,size.y); g.setColor(Color.red); // g.setColor(getForeground()); g.drawString(s,x,y); } }//end class date public class life extends java.applet.Applet implements Runnable { Thread runner; game gol; date TheDate = new date(); static double init_fraction = 0.4; static void help() { System.err.println("-n\t\tnumber of generations"); System.err.println("-geometry nxm\tboard size"); System.err.println("file\t\tStarting configuration"); System.err.println(""); }//end help public static void main(String args[]) throws Exception { System.err.println("# Life $Revision: 1.13 $ W.B.Langdon@cs.bham.ac.uk"); game g = null; int a; int max = -1; pair given_size; for(a = 0; a < args.length; a++) { try { if(args[a].charAt(0)=='?') { a=args.length; } else if(args[a].charAt(0)=='-') { if(args[a].charAt(1)=='n' || args[a].charAt(1)=='N') { if(a+1