Results 1 to 2 of 2
  1. #1
    cest.lavie16 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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:
    Java 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(" ");  
       }
    }
    Last edited by cest.lavie16; 04-26-2011 at 02:11 AM. Reason: Indentation/Clarification of problem with code

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

Similar Threads

  1. Replies: 6
    Last Post: 02-05-2010, 04:53 PM
  2. Some help needed: 2D Cellular Automata
    By markus-sukram in forum AWT / Swing
    Replies: 1
    Last Post: 04-27-2008, 08:02 PM
  3. Replies: 1
    Last Post: 03-17-2008, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •