-
1-D Cellular Automata
I'm having trouble figuring out SetRule() and PropagateNewGeneration(). SetRule() is suppose to convert a rule from 0-255 to an array of eight booleans. For example, rule #16 (0010000)would yield
rules[0] = false
rules[1] = false
rules[2] = false
rules[3] = false
rules[4] = false
rules[5] = true
rules[6] = false
rules[7] = true
PropagateNewGeneration() uses this_gen and rules[] to create next_gen. next_gen will be two blanks longer than this_gen and will apply the rule that if position 2, 3, and 4 in this_gen is "010" (2) then the the position 3 in next_gen will be 1. (Also, all 1s and other chars to be converted to "*" and all 0s to be converted to " ")
Here's my code:
Code:
import java.util.Scanner;
public class Foothill
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int rule;
String user_input;
Automaton aut;
do
{
System.out.print("Enter Rule (0 - 255): ");
user_input = input.nextLine();
rule = Integer.parseInt(user_input);
}
while (rule < 0 || rule > 255);
aut = new Automaton
(rule, " * "
+ " ");
System.out.println("Start");
aut.ShowResults(50);
System.out.println("End");
}
}
class Automaton
{
public static final int MAX_LENGTH = 80;
public static final int MAX_GENS = 500;
private boolean rules[];
private StringBuffer this_gen;
public Automaton(int new_rule, String first_gen)
{
rules = new boolean[8];
SetRule(new_rule);
SetFirstGen(first_gen);
}
public boolean SetFirstGen(String first_gen)
{
StringBuffer sb_first_gen = new StringBuffer(first_gen);
if (sb_first_gen.length() < 3 || sb_first_gen.length() > MAX_LENGTH)
{
this_gen = new StringBuffer(" *** ");
return false;
}
for (int k = 0; k < first_gen.length(); k++)
if (sb_first_gen.charAt(k) != ' ')
sb_first_gen.setCharAt(k, '*');
this_gen = new StringBuffer(" ");
this_gen.append(sb_first_gen);
this_gen.append(" ");
return true;
}
void SetRule(int new_rule)
{
int bit_to_examine = new_rule, k;
for (k=0; k<8; k++)
{
if ((bit_to_examine & (2^k)) != 0)
rules[k] = true;
else
rules[k] = false;
}
}
public void ShowResults(int num_generations)
{
if (num_generations < 1 || num_generations > MAX_GENS )
num_generations = MAX_GENS;
for (int k = 0; k < num_generations; k++)
{
System.out.println(this_gen);
PropagateNewGeneration();
}
}
private void PropagateNewGeneration()
{
int triplet_val, k;
String bit_result;
StringBuffer next_gen = new StringBuffer(" ");
next_gen.append(this_gen);
for (k=0; k < this_gen.length(); k++)
{
triplet_val = 0;
bit_result = "";
bit_result += this_gen.charAt(k) + this_gen.charAt(k+1) +
this_gen.charAt(k+2);
if (bit_result == "***")
triplet_val = 7;
else if (bit_result == "** ")
triplet_val = 6;
else if (bit_result == "* *")
triplet_val = 5;
else if (bit_result == "* ")
triplet_val = 4;
else if (bit_result == " **")
triplet_val = 3;
else if (bit_result == " * ")
triplet_val = 2;
else if (bit_result == " *")
triplet_val = 1;
else
triplet_val = 0;
if (rules[triplet_val] == false)
next_gen.setCharAt(k+1, ' ');
if (rules[triplet_val] == true)
next_gen.setCharAt(k+1, '*');
}
this_gen = next_gen;
next_gen.append(" ");
}
}
-
I think you forgot to state what your trouble actually was... also, posting your code within [code] /code] tags would preserve the indentation of your code, making it easy for others, who wish to help you, read it