/* * @(#)Life.java 1.0 2005/01/01 * * Copyright Michael John Wensley */ import java.util.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Java applet implementation of Life. * @author Michael John Wensley */ public class Life extends Applet implements Runnable, MouseListener { private boolean cells[][]; private boolean dcells[][]; private Pipe queue; private Thread clock; private Thread work; private int glx; /** * Things to do when started as a full application */ public static void main(String[] args) throws java.io.IOException { Life centre = new Life(); centre.init(); do { System.out.print(centre); centre.evolve(); } while (true); } public String toString() { String out = "=============\n"; for(int i=0; i < cells.length; i++) { for(int j=0; j< cells[i].length; j++) { if (cells[i][j]) out = out + "Û"; else out = out + " "; } out = out + "\n"; } out = out + "+=+=+=+="; return out; } private int modulo(int value,int segment) { do { if (value >= segment) value = value - segment; if (value < 0 ) value = value + segment; } while ((value < 0 ) || (value >= segment)); return value; } static char getKey() throws java.io.IOException { char option = (char)System.in.read(); System.in.skip((long)System.in.available()); return Character.toUpperCase(option); } public void init() { String w = getParameter("c3"); String h = getParameter("c4"); String gl = getParameter("gl"); int xs = Integer.parseInt(w); int ys = Integer.parseInt(h); glx = Integer.parseInt(gl); // int xs = getSize().width; // int ys = getSize().height; cells = new boolean[ys][xs]; dcells = new boolean[ys][xs]; for(int i=0; i < cells.length; i++) for(int j=0; j< cells[i].length; j++) cells[i][j] = false; addMouseListener(this); queue = new Pipe(20); gliders(); } private void rand() { for (int y=0; y < cells.length; y++) for (int x=0; x < cells[y].length; x++) { cells[y][x] = (Math.random() >= 0.5); dcells[y][x] = !cells[y][x]; } } private void clear() { for (int y=0; y < cells.length; y++) for (int x=0; x < cells[y].length; x++) { cells[y][x] = false; dcells[y][x] = !cells[y][x]; } } private void gliders() { cells[0][0] = false; cells[0][1] = true; cells[0][2] = false; cells[1][0] = false; cells[1][1] = false; cells[1][2] = true; cells[2][0] = true; cells[2][1] = true; cells[2][2] = true; for (int y=0; y < cells.length; y++) for (int x=0; x < cells[y].length; x++) { cells[y][x] = cells[y % glx][x % glx]; dcells[y][x] = !cells[y][x]; } } public void paint( Graphics g) { super.paint(g); g.setColor( Color.black ); int cellwidth, cellheight; cellwidth = getSize().width / cells[0].length; cellheight = getSize().height / cells.length; for(int i=0; i < cells.length; i++) { for(int j=0; j< cells[i].length; j++) { // if (dcells[i][j] != cells[i][j]) { dcells[i][j] = cells[i][j]; if (cells[i][j]) { g.setColor(Color.green); g.fillRect(j * cellwidth,i * cellheight,cellwidth,cellheight); } else { g.setColor(Color.darkGray); g.fillRect(j * cellwidth,i * cellheight,cellwidth,cellheight); } // } } } } public void update( Graphics g) { paint(g); } public void start() { clock = new Thread(this); clock.start(); work = new Thread(this); work.start(); } public void run() { while (clock == Thread.currentThread()) { try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { } queue.deposit(this); } while (work == Thread.currentThread()) { Object c = queue.fetch(); if (c == this) { evolve(); repaint(); } if (c instanceof MouseEvent) { MouseEvent e = (MouseEvent)c; int cy = ( e.getY() * cells.length ) / getSize().width; int cx = ( e.getX() * cells[cy].length ) / getSize().height; if (e.getButton() == MouseEvent.BUTTON1) { cells[cy][cx] = true; } if (e.getButton() == MouseEvent.BUTTON2) { cells[cy][cx] = false; } if (e.getButton() == MouseEvent.BUTTON3) { if (e.isShiftDown()) { clear(); } else if (e.isControlDown()) { rand(); } else { gliders(); } } } } } public void stop() { clock = null; work = null; queue.deposit(new Object()); } public void evolve() { boolean oldcells[][] = new boolean[cells.length][cells[0].length]; // for(int i=0; i < cells.length; i++) // for(int j=0; j< cells[i].length; j++) // oldcells[i][j] = cells[i][j]; // Duplicate array for(int i=0; i < cells.length; i++) System.arraycopy(cells[i], 0, oldcells[i], 0, cells[i].length); for(int i=0; i < cells.length; i++) for(int j=0; j< cells[i].length; j++) { int nc = 0; int y = cells.length; int x = cells[i].length; if (oldcells[modulo(i-1,y)][modulo(j-1,x)]) nc++; if (oldcells[modulo(i+0,y)][modulo(j-1,x)]) nc++; if (oldcells[modulo(i+1,y)][modulo(j-1,x)]) nc++; if (oldcells[modulo(i-1,y)][modulo(j+0,x)]) nc++; if (oldcells[modulo(i+1,y)][modulo(j+0,x)]) nc++; if (oldcells[modulo(i-1,y)][modulo(j+1,x)]) nc++; if (oldcells[modulo(i+0,y)][modulo(j+1,x)]) nc++; if (oldcells[modulo(i+1,y)][modulo(j+1,x)]) nc++; if (oldcells[i][j]) { switch (nc) { case 2 : case 3 : cells[i][j] = true; break; default : cells[i][j] = false; break; } } else { if (nc == 3) cells[i][j] = true; else cells[i][j] = false; } } } public String getAppletInfo() { return "Java implementation of Life. Copyright Michael John Wensley"; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { queue.deposit(e); } }