Modeling cellular automata, using the rule from the game Lights Out, which I used to play all the time on my iPod Touch.
read morecell.java
public class cell {
boolean on;
int row;
int column;
public cell(int i, int j){
this.row=i;
this.column=j;
}
public boolean isOn() {
return on;
}
public void alternate(){
on=!on;
}
public void setOn(boolean on) {
this.on = on;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
}
collection.java
//Ideas for evaluating the state of a given cell set: perhaps we could have an accompanying cell set that
//slightly shades each cell each time it is on at a given step. That way we can see the overall behavior
public class collection {
boolean drawn = false;
int rows;
int columns;
int sideLength;
cell collection[][];
public collection(int rows, int columns, int sideLength) {
this.rows = rows;
this.columns = columns;
this.sideLength = sideLength;
this.collection = new cell[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
collection[i][j] = new cell(i, j);
}
}
}
public void initialConditionForProm_Tara() {
for(int j=50;j<80;j++){
for(int i=50;i<53;i++){
collection[i][j].setOn(true);
}
}
for(int j=53;j<65;j++){
for(int i=50;i<53;i++){
collection[j][i].setOn(true);
}
}
for(int j=65;j<68;j++){
for(int i=50;i<63;i++){
collection[j][i].setOn(true);
}
}
for(int j=65;j<68;j++){
for(int i=50;i<62;i++){
collection[j][i].setOn(true);
}
}
for(int j=62;j<65;j++){
for(int i=50;i<68;i++){
collection[i][j].setOn(true);
}
}
for(int j=75;j<78;j++){
for(int i=63;i<80;i++){
collection[j][i].setOn(true);
}
}
for(int j=63;j<66;j++){
for(int i=78;i<86;i++){
collection[i][j].setOn(true);
}
}
for(int j=64;j<80;j++){
for(int i=97;i<100;i++){
collection[i][j].setOn(true);
}
}
for(int j=64;j<80;j++){
for(int i=107;i<110;i++){
collection[i][j].setOn(true);
}
}
for(int j=77;j<80;j++){
for(int i=100;i<107;i++){
collection[i][j].setOn(true);
}
}
for(int j=64;j<67;j++){
for(int i=100;i<107;i++){
collection[i][j].setOn(true);
}
}
// the m
int currentx = 130;
for(int j=62;j<80;j++){
for(int i=120;i<140;i++){
if(i==currentx){
collection[currentx][j].setOn(true);
collection[currentx+1][j].setOn(true);
collection[currentx+2][j].setOn(true);
collection[currentx+3][j].setOn(true);
}
}
if(j%2==0){
currentx--;
}
}
currentx = 150;
for(int j=62;j<80;j++){
for(int i=140;i<160;i++){
if(i==currentx){
collection[currentx][j].setOn(true);
collection[currentx+1][j].setOn(true);
collection[currentx+2][j].setOn(true);
collection[currentx+3][j].setOn(true);
}
}
if(j%2==0){
currentx--;
}
}
currentx = 150;
for(int j=62;j<80;j++){
for(int i=140;i<160;i++){
if(i==currentx){
collection[currentx][j].setOn(true);
collection[currentx+1][j].setOn(true);
collection[currentx+2][j].setOn(true);
collection[currentx+3][j].setOn(true);
}
}
if(j%2==0){
currentx++;
}
}
currentx = 130;
for(int j=62;j<80;j++){
for(int i=120;i<140;i++){
if(i==currentx){
collection[currentx][j].setOn(true);
collection[currentx+1][j].setOn(true);
collection[currentx+2][j].setOn(true);
collection[currentx+3][j].setOn(true);
}
}
if(j%2==0){
currentx++;
}
}
//question mark
int xcurrent = 100;
int ycurrent = 120;
int xaddition = 0;
int yaddition = 0;
for(double i= 0;i<3;i+=.01){
xaddition = (int) (Math.sin(i)*18);
yaddition = (int) (Math.cos(i)*18);
collection[xcurrent+xaddition][ycurrent+yaddition].setOn(true);
collection[xcurrent+1+xaddition][ycurrent+yaddition].setOn(true);
collection[xcurrent+2+xaddition][ycurrent+yaddition].setOn(true);
collection[xcurrent+3+xaddition][ycurrent+yaddition].setOn(true);
}
for(int j=100;j<103;j++){
for(int i=139;i<156;i++){
collection[j][i].setOn(true);
}
}
for(int j=158;j<161;j++){
for(int i=100;i<103;i++){
collection[i][j].setOn(true);
}
}
collection[67][64].setOn(false);
collection[66][64].setOn(false);
collection[67][63].setOn(false);
//collection[10][11].setOn(true);
//collection[11][12].setOn(true);
}
public void initialConditionsSierpinski(){
collection[100][0].setOn(true);
}
public void migratingSteps() {
boolean cellsThatNeedToSwitch[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (collection[i][j].on) {
cellsThatNeedToSwitch[i][j] = false;
try {
cellsThatNeedToSwitch[i][j + 1] = true;
cellsThatNeedToSwitch[i + 1][j + 1] = true;
cellsThatNeedToSwitch[i - 1][j] = true;
// collection[i][j].alternate();
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToSwitch[i][j]) {
collection[i][j].setOn(true);
}
if (!cellsThatNeedToSwitch[i][j]) {
collection[i][j].setOn(false);
}
}
}
}
public void test() {
boolean cellsThatNeedToSwitch[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (collection[i][j].on) {
cellsThatNeedToSwitch[i][j] = false;
try {
cellsThatNeedToSwitch[i][j + 1] = true;
cellsThatNeedToSwitch[i + 1][j] = true;
cellsThatNeedToSwitch[i - 1][j] = true;
cellsThatNeedToSwitch[i][j - 1] = true;
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToSwitch[i][j]) {
collection[i][j].setOn(true);
}
if (!cellsThatNeedToSwitch[i][j]) {
collection[i][j].setOn(false);
}
}
}
}
public void lightsOffPressThoseOn() {
boolean cellsThatNeedToAlternate[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (collection[i][j].on) {
cellsThatNeedToAlternate[i][j] = true;
try {
cellsThatNeedToAlternate[i][j - 1] = !cellsThatNeedToAlternate[i][j - 1];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i + 1][j] = !cellsThatNeedToAlternate[i + 1][j];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i - 1][j] = !cellsThatNeedToAlternate[i - 1][j];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i][j + 1] = !cellsThatNeedToAlternate[i][j + 1];
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToAlternate[i][j]) {
collection[i][j].alternate();
}
}
}
}
public void lightsOffPressThoseOff() {
boolean cellsThatNeedToAlternate[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (!collection[i][j].on) {
cellsThatNeedToAlternate[i][j] = true;
try {
cellsThatNeedToAlternate[i][j - 1] = !cellsThatNeedToAlternate[i][j - 1];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i + 1][j] = !cellsThatNeedToAlternate[i + 1][j];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i - 1][j] = !cellsThatNeedToAlternate[i - 1][j];
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
cellsThatNeedToAlternate[i][j + 1] = !cellsThatNeedToAlternate[i][j + 1];
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToAlternate[i][j]) {
collection[i][j].alternate();
}
}
}
}
public void attemptAtLife() {
boolean error = false;
boolean error1 = false;
boolean cellsThatNeedToAlternate[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
error = false;
error1= false;
if (collection[i][j].on) {
try {
if (collection[i][j + 2].on)
;
}
catch (ArrayIndexOutOfBoundsException e) {
error = true;
}
try {
if (collection[i+1][j + 1].on)
;
}
catch (ArrayIndexOutOfBoundsException e) {
error = true;
}
if (!error && collection[i+1][j + 1].on) {
collection[i][j].setOn(false);
cellsThatNeedToAlternate[i][j+2] = true;
}
try {
if (collection[i-1][j + 1].on)
;
}
catch (ArrayIndexOutOfBoundsException e) {
error1 = true;
}
try {
if (collection[i][j + 2].on)
;
}
catch (ArrayIndexOutOfBoundsException e) {
error1 = true;
}
if (!error1 && collection[i-1][j + 1].on) {
cellsThatNeedToAlternate[i][j] = true;
cellsThatNeedToAlternate[i][j+2] = true;
}
else {
cellsThatNeedToAlternate[i][j] = false;
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToAlternate[i][j]) {
collection[i][j].alternate();
}
}
}
}
public void sierpinskiAttempt(){
boolean cellsThatNeedToSwitch[][] = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
try{
if (collection[i-1][j-1].on&&!collection[i+1][j-1].on) {
cellsThatNeedToSwitch[i][j]=true;
}
}catch (ArrayIndexOutOfBoundsException e) {
}
try{
if (!collection[i-1][j-1].on&&collection[i+1][j-1].on) {
cellsThatNeedToSwitch[i][j]=true;
}
}catch (ArrayIndexOutOfBoundsException e) {
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cellsThatNeedToSwitch[i][j]) {
collection[i][j].setOn(true);
}
}
}
}
public void initialConditionsLightsOff(){
collection[Math.round(rows/2)+1][Math.round(rows/2)+1].setOn(true);
}
}
player.java
import java.awt.Graphics;
public class player extends java.applet.Applet implements Runnable {
int frame;
int delay;
int wait=1000;
int increment = 1;
int displacement = 20;
collection cells = new collection(11,11, 60);
Thread animator;
/**
* Initialize the applet and compute the delay between frames.
*/
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 100;
delay = (fps > 0) ? (1000 / fps) : 100;
if (!cells.drawn) {
cells.drawn = true;
this.setSize(2*displacement+(cells.sideLength*cells.rows),2*displacement+(cells.sideLength*cells.columns));
//cells.initialConditionForProm_Tara();
cells.initialConditionsLightsOff();
}
}
/**
* This method is called when the applet becomes visible on the screen.
* Create a thread and start it.
*/
public void start() {
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in the start method.
* It does the main animation.
*/
public void run() {
while (Thread.currentThread() == animator) {
// Display the next frame of animation.
// Delay for a while
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
break;
}
// Advance the frame
increment++;
repaint();
frame++;
}
}
/**
* This method is called when the applet is no longer visible. Set the
* animator variable to null so that the thread will exit before displaying
* the next frame.
*/
public void stop() {
animator = null;
}
/**
* Paint a frame of animation.
*/
int constantincrementor = 0;
public void paint(Graphics g) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cells.drawn){
g.drawRect(displacement, displacement, cells.rows * cells.sideLength,
cells.columns * cells.sideLength);
for (int i = 1; i <= cells.rows; i++) {
g.drawLine(displacement, (i * cells.sideLength) + displacement,
(cells.columns * cells.sideLength) + displacement,
(i * cells.sideLength) + displacement);
}
for (int j = 1; j <= cells.columns; j++) {
g.drawLine((j * cells.sideLength) + displacement, displacement,
(j * cells.sideLength) + displacement,
(cells.columns * cells.sideLength) + displacement);
}
for (int i = 0; i < cells.rows; i++) {
for (int j = 0; j < cells.columns; j++) {
if(cells.collection[i][j].on){
g.fillRect(displacement+((i)*cells.sideLength), displacement+((j)*cells.sideLength), cells.sideLength, cells.sideLength);
}
}
}
//cells.migratingSteps();
cells.lightsOffPressThoseOn();
//cells.lightsOffPressThoseOff();
//cells.test();
//cells.attemptAtLife();
//cells.sierpinskiAttempt();
}
}
}