Results 1 to 3 of 3
  1. #1
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Generate new generation binary // need help

    This is my code

    Java Code:
    import java.util.Scanner;
    
    public class test
    {
       public static void main(String[] args)
       {
          int rule;
          String str_user_input;
          Scanner input_stream = new Scanner(System.in);
          Automaton aut;
    
          // get rule from user
          do
          {
             System.out.print("Enter Rule (0 - 255): ");
             // get the answer in the form of a string:
             str_user_input = input_stream.nextLine();
             // and convert it to a number so we can compute:
             rule = Integer.parseInt(str_user_input);
    
          } while (rule < 0 || rule > 255);
    
          // create automaton with this rule and single central dot
          aut = new Automaton 
             (rule, "                               *                               ");
    
          // now show it
          System.out.println("   start");
          aut.ShowResults(50);
          System.out.println("   end");
       }
    }
    
    class Automaton
    {
       // class constants
       public final static int MAX_LEN  = 80;
       public final static int MAX_GENS  = 500;
       
       // private members
       private boolean rules[];  // allocate rules[8] in constructor
       private StringBuffer this_gen; // same here
       
       // constructor
       public Automaton(int new_rule, String first_gen)
       {
          rules = new boolean[8];
          SetRule(new_rule);
          SetFirstGen(first_gen);
       }
      
       // mutators
       public boolean SetFirstGen(String first_gen)
       {
          StringBuffer sb_first_gen = new StringBuffer(first_gen);
          
          // don't allow super-short automata
          if ( sb_first_gen.length() < 3 )
          {
             this_gen = new StringBuffer(" *** ");
             return false;
          }
    
          // sanitize first_gen so all non-blank chars converted to '*'
          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 = 0, k, bit_result; // ignore if your solution uses other vars
    
          // nothing to filter
          for (k = 0; k < 8; k++)
          {
             if ( (bit_to_examine & 1) != 0)
                rules[0] = true;   
             else
                rules[0] = false;
             if ( (bit_to_examine & 2) != 0)
                rules[1] = true;   
             else
                rules[1] = false;
             if ( (bit_to_examine & 3) != 0)
                rules[2] = true;   
             else
                rules[2] = false;
             if ( (bit_to_examine & 4) != 0)
                rules[3] = true;   
             else
                rules[3] = false;
             if ( (bit_to_examine & 5) != 0)
                rules[4] = true;   
             else
                rules[4] = false;
             if ( (bit_to_examine & 6) != 0)
                rules[5] = true;   
             else
                rules[5] = false;
             if ( (bit_to_examine & 7) != 0)
                rules[6] = true;   
             else
                rules[6] = 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;  // ignore if your solution doesn't need this
          StringBuffer next_gen;
       
          // add leading position to far left
          next_gen = new StringBuffer(" ");
       
          // I can't figure this part out
    
          // add extra position to far right
          next_gen.append(" ");
       
          // and finally pass the torch to the new generation
          this_gen = next_gen;
       }
    }
    This part that i can't figure it out
    I don't know how to implement this
    "void PropagateNewGeneration() - this is the workhorse helper function. It will use the two private members this_gen and rule[], and create the next generation from them. This method implements a boundary condition that forces the two extreme positions of each generation to be off (i.e., " "). There are other boundary conditions choices, and this is not the one that will give us unlimited growth, but it has a simplifying effect that keeps the action contained to the width of the seed String. Recall that we took the seed and added a blank before and after that String. This will result in a String that is 2 longer than the seed. Since the rule always contracts the String by two cells (think about it) this means you need to tag-on blanks, again, to both ends of new generation. Add two blanks, apply the rule, add two blanks, apply the rule, etc. This keeps the length of each generation fixed at (the length of the seed) + 2. As I said, this is not the best theoretical boundary condition, but it is an easy one to implement."

    Java Code:
      private void PropagateNewGeneration()
       {
          int triplet_val;  // ignore if your solution doesn't need this
          StringBuffer next_gen;
       
          // add leading position to far left
          next_gen = new StringBuffer(" ");
       
          // I can't figure this part out
    
          // add extra position to far right
          next_gen.append(" ");
       
          // and finally pass the torch to the new generation
          this_gen = next_gen;
       }

  2. #2
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    I can't get it work.

    This is the step by step to generate new generation
    -- PropagateNewGeneration()
    3. Convert first_gen (or) this_gen string to string of 0's and 1's
    4. Loop through triplets of current string, moving over one char with each loop, for 0 through MAX_LEN-1
    5. Determine triplet using shift operator and inner loop: appending each of the three chars into a temp variable
    6. Convert each binary triplet to a decimal equivalent
    7. Look up tiplet's decimal equivalent 'n' against rules[n] to get new this_gen char for each step in the outer loop
    8. Append zero to new this_gen StringBuffer variable
    9. Append new char value to with each successive step through outer loop
    10. Append zero to this_gen StringBuffer at end of outer loop
    11. Convert this_gen back to '*' and ' '
    12. Print this_gen to screen
    13. Return to step 3 and iterate through all remaining generations as determined by num_generations


    This is my code so far :
    Java Code:
     import java.util.Scanner;
    
    public class Foothill
    {
       public static void main(String[] args)
       {
          int rule;
          String str_user_input;
          Scanner input_stream = new Scanner(System.in);
          Automaton aut;
    
          // get rule from user
          do
          {
             System.out.print("Enter Rule (0 - 255): ");
             // get the answer in the form of a string:
             str_user_input = input_stream.nextLine();
             // and convert it to a number so we can compute:
             rule = Integer.parseInt(str_user_input);
    
          } while (rule < 0 || rule > 255);
    
          // create automaton with this rule and single central dot
          aut = new Automaton 
             (rule, "                               *                               ");
    
          // now show it
          System.out.println("   start");
          aut.ShowResults(50);
          System.out.println("   end");
       }
    }
    
    class Automaton
    {
       // class constants
       public final static int MAX_LEN  = 80;
       public final static int MAX_GENS  = 500;
       
       // private members
       private boolean rules[];  // allocate rules[8] in constructor
       private StringBuffer this_gen; // same here
       private String binaryStr8;
       
       // constructor
       public Automaton(int new_rule, String first_gen)
       {
          rules = new boolean[8];
          SetRule(new_rule);
          SetFirstGen(first_gen);
       }
      
       // mutators
       public boolean SetFirstGen(String first_gen)
       {
          StringBuffer sb_first_gen = new StringBuffer(first_gen);
          
          // don't allow super-short automata
          if ( sb_first_gen.length() < 3 )
          {
             this_gen = new StringBuffer(" *** ");
             return false;
          }
    
          // sanitize first_gen so all non-blank chars converted to '*'
          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, bit_result; // ignore if your solution uses other vars
    
          String binaryStr4 = Long.toString(bit_to_examine,2);
          binaryStr8 = String.format("%8s", binaryStr4).replace(' ', '0');;
          
          for (int g = 7; g >= 0; g--)
          {
             int ArrRules = g-g;
             if ( (binaryStr8.charAt(g) & 1) == 1)
             {
                rules[ArrRules] = true;
             }
             else
                {
                rules[ArrRules] = 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;  // ignore if your solution doesn't need this
          StringBuffer next_gen;
          String bit_result="";
          // add leading position to far left
          next_gen = new StringBuffer(" ");
          
          for (int k = 0; k < this_gen.length(); k++)
          {
          if (this_gen.charAt(k) !=  ' ')
             next_gen.append('1');
          else
             next_gen.append('0');
          }
          
          for (int k = 0 ; k < (next_gen.length() - 2) ; k++)
          {
             triplet_val=0;
             bit_result = bit_result+next_gen.charAt(k) + next_gen.charAt(k+1) + next_gen.charAt(k+2);
             char k1 = bit_result.charAt(k); 
             if (k1 == '1')
                triplet_val = triplet_val+(k) + 4;
             char k2 = bit_result.charAt(k+1); 
             if (k2 == '1' )
                triplet_val = triplet_val+(k) + 2;
             char k3 = bit_result.charAt(k+2);
             if (k3 == '1')
                triplet_val = triplet_val+(k) + 1;
             for (int h = 1; h < 80; h++)
             {
                if (triplet_val==0)
                {
                   String s1 = Character.toString(binaryStr8.charAt(7));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==1)
                {
                   String s1 = Character.toString(binaryStr8.charAt(6));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==2)
                {
                   String s1 = Character.toString(binaryStr8.charAt(5));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==3)
                {
                   String s1 = Character.toString(binaryStr8.charAt(4));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==4)
                {
                   String s1 = Character.toString(binaryStr8.charAt(3));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==5)
                {
                   String s1 = Character.toString(binaryStr8.charAt(2));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==6)
                {
                   String s1 = Character.toString(binaryStr8.charAt(1));
                   next_gen.replace(h,h+1,s1);
                }
                else if (triplet_val==7)
                {
                   String s1 = Character.toString(binaryStr8.charAt(0));
                   next_gen.replace(h,h+1,s1);
                }
                
             }
          }
          
          
          
          // add extra position to far right
          next_gen.append(" ");
          
          StringBuffer temp = new StringBuffer("");
          for (int k = 0; k < next_gen.length(); k++)
          {
             
          if (next_gen.charAt(k) !=  ' ')
             temp.append('*');
          else
             temp.append(' ');
          }
          next_gen = temp;
          // and finally pass the torch to the new generation
          this_gen = next_gen;
       }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. War file generation
    By rummy in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 01:57 PM
  2. Report generation
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2009, 01:26 PM
  3. Auto id generation
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 12:27 AM
  4. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM
  5. String generation
    By codingisfun22 in forum Advanced Java
    Replies: 3
    Last Post: 12-03-2007, 06:43 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
  •