package com.flotow;import java.util.Scanner;import java.lang.Math.*;public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String shape; int r000; int r001; int r010; int r011; int r1; int runs = 0; int[] in = new int[1002]; for(int i = 0; i < in.length; i++){ if(i == 500){ in[i] = 1; }else{ in[i] = 0; } } System.out.println("Enter number of iterations."); System.out.println("----------------------------------"); int cycles = scan.nextInt(); int nums = 0; System.out.println("Choose the rule set. For the following cases\nEnter 1 if the rule should output a 1\nor Enter 0 if it should output 0."); System.out.println("001 -> "); r000 = scan.nextInt(); System.out.println("101 -> "); r001 = scan.nextInt(); System.out.println("10 -> "); r010 = scan.nextInt(); System.out.println("11 -> "); r011 = scan.nextInt(); System.out.println("00 -> "); r1 = scan.nextInt();
System.out.println(""); System.out.println("What shape would you like to draw the fractal with?"); System.out.println(""); String wait = scan.nextLine(); shape = wait; String wait2 = scan.nextLine(); shape = wait2; System.out.println("Starting..."); System.out.println("----------------------------------"); while (true) { while (nums < cycles) { if(runs == 0) { Automata a = new Automata(in,r000,r001,r010,r011,r1,shape); nums++; in = a.arrayOut(); } if(runs>0){ if(runs == 1){ RandomizeArray r = new RandomizeArray(); in = r.out(); } Automata b = new Automata(in,r000,r001,r010,r011,r1,shape); in = b.arrayOut(); nums++; runs = 2; } } runs = 1; System.out.println("Go Again? ENTER 1 for yes or 0 for no."); int again = scan.nextInt(); if(again != 1){ break; } System.out.println("Enter number of iterations:"); System.out.println("----------------------------------"); cycles = scan.nextInt(); nums = 0; System.out.println("Choose the rule set. For the following cases\nEnter 1 if the rule should output a 1\nor Enter 0 if it should output 0."); System.out.println("001 -> "); r000 = scan.nextInt(); System.out.println("101 -> "); r001 = scan.nextInt(); System.out.println("10 -> "); r010 = scan.nextInt(); System.out.println("11 -> "); r011 = scan.nextInt(); System.out.println("00 -> "); r1 = scan.nextInt(); System.out.println(""); System.out.println("What shape would you like to draw the fractal with?"); System.out.println(""); wait = scan.nextLine(); wait2 = scan.nextLine(); shape = wait2; System.out.println("Starting..."); System.out.println("----------------------------------"); } }}class Automata{ int[] X = new int[1002]; int[] newX = new int[1002]; int r000; int r001; int r010; int r011; int r1; public Automata(int[] X, int r000, int r001, int r010, int r011,int r1, String shape) { this.r000 = r000; this.r001 = r001; this.r010 = r010; this.r011 = r011; this.r1 = r1;
this.X = this.newX; CountCharacter c = new CountCharacter(shape); int spaces = c.stringCountOut(); String totalSpaces = ""; for(int s = 0; s < spaces; s++){ totalSpaces = totalSpaces+" "; } for(int j = 0; j < X.length-1; j++){ if(X[j]==0){ System.out.print(totalSpaces); }else{ System.out.print(shape); } } for (int i = 0; i < X.length-1; i++) { if (i == 0) { if ((X[i] == 0 && X[i + 1] == 0 && X[1000] ==1)) { this.newX[i] = this.r000; } else if ((X[i] == 1 && X[i + 1] == 0 && X[1000] == 1)) { this.newX[i] = this.r001;; } else if ((X[i] == 1 && X[i + 1] == 0)) { this.newX[i] = this.r010; } else if ((X[i] == 1 && X[i + 1] == 1)) { this.newX[i] = this.r011; }else if ((X[i] == 0 && X[i+1]==0)) { this.newX[i] = this.r1; } } else if (i >= 1 && i <= 998) { if ((X[i] == 0 && X[i + 1] == 0 && X[i-1] ==1)) { this.newX[i] = this.r000; } else if ((X[i] == 1 && X[i + 1] == 0 && X[i-1] == 1)) { this.newX[i] = this.r001; } else if ((X[i] == 1 && X[i + 1] == 0)) { this.newX[i] = this.r010; } else if ((X[i] == 1 && X[i + 1] == 1)) { this.newX[i] = this.r011; }else if ((X[i] == 0 && X[i+1]==0)) { this.newX[i] = this.r1; } } else if (i == 999) { if ((X[i] == 0 && X[i + 1] == 0 && X[i-1] ==1)) { this.newX[i] = this.r000; }else if ((X[i] == 1 && X[i + 1] == 0 && X[i-1] == 1)) { this.newX[i] = this.r001; }else if ((X[i] == 1 && X[0] == 0)) { this.newX[i] = this.r010+1; }else if ((X[i] == 1 && X[0] == 1)) { this.newX[i] = this.r011; }else if ((X[i] == 0 && X[i+1]==0)) { this.newX[i] = this.r1; } } } for(int k = 0; k < X.length-1; k++){ this.X[k] = this.newX[k]; } System.out.println(""); } public int[] arrayOut(){ return this.X; }}class RandomizeArray{ int[] rando = new int[1002]; double randomNum = 0; public RandomizeArray(){ for(int i = 0; i < 1002; i++){ randomNum = Math.random(); if(i==600){ rando[i] = 1; }else{ rando[i] = 0; } } } public int[] out(){ return this.rando; }}class CountCharacter { String in; int count; public CountCharacter(String in) { this.count = 0; for (int i = 0; i < in.length(); i++) { this.count++; } } public int stringCountOut(){ return this.count; }}