// CA.java
// 200C winter2004 MAT
// Eunsu Kang
// Misreading letters using 1D Cellular Automata

import java.awt.*;
import java.applet.*;
import java.util.Random;

public class CA extends Applet {

public void start() {
setBackground(Color.black);
repaint();
}

//Color color_red= new Color(255, 0, 0);
//Color color_white= new Color(255, 255, 255);
Color color1= new Color(95, 9, 149);
Color color2= new Color(165, 57, 234);

int font_size = 12;
Font font = new Font("serif", Font.BOLD, font_size);
String vowel[] = {" ", "a", " ", "e", " ", "i"," ", "o"," ", "u"," "};
int vowel_length = 11;
String consonant[] = { "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};
int consonant_length = 21;

int iterations = 60;
int word_length = 40;

Random myRandom = new Random();

/*
The rule specify mappings from 000 -> ?, 001 -> ?, .... 111 -> ?
Where the xyz -> ? refers to the state of the
x - left neightbour, y - oneself, z - right neighbour
? to the state of oneself at the next time step
*/
//int rules[] = {0,1,0,1,0,0,0,0}; // Rule 10_2
//int rules[] = {1,0,1,1,0,1,0,0}; // Rule 45_3
//int rules[] = {1,0,0,1,0,0,0,1}; // Rule me_4
int rules[] = {1,1,1,1,0,0,0,0}; // Rule me2_1
//int rules[] = {0,0,0,1,0,0,0,0}; // Rule me3_5
//int rules[] = {0,0,1,1,1,1,0,0}; // Rule me4_6
//int rules[] = {0,1,0,1,1,0,1,0}; // Rule 90_7

public void paint (Graphics g) {
int i,j,k;
int state[] = new int[word_length];
int newstate[] = new int[word_length];

//setBackground(Color.black);

//g.setColor(color);
g.setFont(font);

for (i=0; i < word_length; i++) {
int r = myRandom.nextInt(); // random numbers -> integer
state[i] = Math.abs(r % 2); // get 0, 1 from random integers
System.out.println("r = " + r + " state[" + i + "] = " + state[i]);

if( state[i] == 0) {
int string_random = Math.abs(myRandom.nextInt() % vowel_length); // chhosing 0ne of 0-10 randomly
g.setColor(color1);
g.drawString( vowel[string_random], i * font_size, font_size); // array, x, y
} else {
int string_random = Math.abs(myRandom.nextInt() % consonant_length);
g.setColor(color2);
g.drawString( consonant[string_random], i * font_size, font_size );
}
}


for( i = 0; i < iterations; i++) {

// calculating the state. important part from the c-code
for (j = 0; j < word_length; j++) {
k = 4*state[( j-1+word_length ) % word_length] + 2*state[j] + state[(j+1) % word_length];
newstate[j] = rules[k];
}

for (j = 0; j < word_length; j++)
state[j] = newstate[j];

// Drawing the next line
for( j = 0; j < word_length; j++) {
if( state[j] == 0) { // drawing vowel letters
int string_random = Math.abs(myRandom.nextInt() % vowel_length);
g.setColor(color1);
g.drawString( vowel[string_random], j * font_size, font_size + font_size * (i+1));
} else { // drawing consonant letters
int string_random = Math.abs(myRandom.nextInt() % consonant_length);
g.setColor(color2);
g.drawString( consonant[string_random], j * font_size, font_size + font_size * (i+1));
}
}
}

}
}

// celluar automata http://en.wikipedia.org/wiki/Cellular_automaton
// John Von Neumann: http://ei.cs.vt.edu/~history/VonNeumann.html
// Game of Life: http://www-gap.dcs.st-and.ac.uk/~history/Mathematicians/Conway.html
// John Horton Conway: http://www-gap.dcs.st-and.ac.uk/~history/Mathematicians/Conway.html
// One-dimensional Cellular Automata http://math.hws.edu/xJava/CA/CA.html
// ***1D Boolean Cellular Automata: http://astronomy.swin.edu.au/~pbourke/fractals/ca/
// Stephen Wolfram: http://www.wolframscience.com/