Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- //worked with Zachary Muller and Marc Hanna on the logistics of the project
- public class Automaton {
- private boolean[] ruleSet = new boolean[8];
- private ArrayList<ArrayList<Boolean>> Automata = new ArrayList<ArrayList<Boolean>>();
- private char falseSym = '0';
- private char trueSym = '1';
- private int ruleNumber;
- private ArrayList<Boolean> initialState = new ArrayList<Boolean>();
- private ArrayList<Boolean> current = new ArrayList<Boolean>(); //cant use char, must use obj version
- private String binary;
- public Automaton(int ruleNumber, ArrayList<Boolean> initialState)
- {
- setRules(ruleNumber);
- Automata.clear();
- this.ruleNumber = ruleNumber;
- for (int i = 0; i< initialState.size(); i++)
- {
- this.initialState.add(initialState.get(i));
- }
- Automata.add(initialState);
- }
- public Automaton(String filename) throws FileNotFoundException
- {
- Scanner filereader = new Scanner(new File(filename));
- ruleNumber = filereader.nextInt();
- falseSym = filereader.next().charAt(0);
- setFalseSymbol(falseSym);
- trueSym = filereader.next().charAt(0);
- setTrueSymbol(trueSym);
- String tempState = filereader.next();
- for(int i = 0; i<tempState.length(); i++) {
- if (tempState.charAt(i) == trueSym)
- {
- initialState.add(true);
- }
- else
- {
- initialState.add(false);
- }
- }
- }
- public void setRules(int ruleNumber)
- {
- this.ruleNumber = ruleNumber;
- int leadingZeroes;
- binary = Integer.toBinaryString(ruleNumber);
- String missing = "";
- if (binary.length() < 8) {
- leadingZeroes = 8 - binary.length();
- for (int i = 0; i < leadingZeroes; i++) {
- missing += "0";
- }
- binary = missing + binary;
- }
- for (int i = 0; i < binary.length(); i++)
- {
- if (binary.charAt(i) == '0')
- {
- ruleSet[i] = false;
- }
- else
- {
- ruleSet[i] = true;
- }
- }
- }
- public String toString()
- {
- String ECATotal = "";
- for(int i = 0; i<Automata.size(); i++)
- {
- if(i < Automata.size() -1)
- {
- ECATotal = ECATotal + getStateString(i) + '\n';
- }
- else
- {
- ECATotal = ECATotal + getStateString(i);
- }
- }
- return ECATotal;
- }
- public void save(String filename) throws IOException
- {
- PrintWriter saver = new PrintWriter(new File(filename));
- String binaryTotal = "";
- for(int i = 0; i< initialState.size(); i++) //Marc helped me with this for loop.
- {
- if (initialState.get(i) == true)
- {
- binaryTotal = binaryTotal + '1';
- }
- else
- {
- binaryTotal = binaryTotal + '0';
- }
- }
- saver.println(binaryTotal);
- saver.close();
- }
- public void evolve(int numSteps, int ruleNumber)
- {
- setRules(ruleNumber);
- //Marc Hanna explained to me the logic of this method.
- int stepCount = 0;
- //method generates an entire new line for the evolution
- while(stepCount<numSteps)
- {
- stepCount++;
- for(int i = 0; i< initialState.size(); i++)
- {
- int leftNeigh = 0;
- int rightNeigh = 0;
- if(i == 0)
- {
- leftNeigh = initialState.size() -1;
- rightNeigh = i + 1;
- }
- else if(i == initialState.size() -1)
- {
- leftNeigh = i - 1;
- rightNeigh = 0;
- }
- else if(i>0 && i < initialState.size() -1 )
- {
- leftNeigh = i - 1;
- rightNeigh = i + 1;
- }
- if(initialState.get(leftNeigh) && initialState.get(i) && initialState.get(rightNeigh))
- {
- current.add(i, (ruleSet[0]));
- }
- else if(initialState.get(leftNeigh) && initialState.get(i) && initialState.get(rightNeigh)==false)
- {
- current.add(i, (ruleSet[1]));
- }
- else if(initialState.get(leftNeigh) && initialState.get(i)==false && initialState.get(rightNeigh))
- {
- current.add(i, (ruleSet[2]));
- }
- else if(initialState.get(leftNeigh) && initialState.get(i)==false && initialState.get(rightNeigh)==false)
- {
- current.add(i, (ruleSet[3]));
- }
- else if(initialState.get(leftNeigh)==false && initialState.get(i) && initialState.get(rightNeigh))
- {
- current.add(i, (ruleSet[4]));
- }
- else if(initialState.get(leftNeigh)==false && initialState.get(i) && initialState.get(rightNeigh)==false)
- {
- current.add(i, (ruleSet[5]));
- }
- else if(initialState.get(leftNeigh)==false && initialState.get(i)==false && initialState.get(rightNeigh))
- {
- current.add(i, (ruleSet[6]));
- }
- else if(!initialState.get(leftNeigh) && !initialState.get(i) && !initialState.get(rightNeigh))
- {
- current.add(i, (ruleSet[7]));
- }
- Automata.add(current);
- current.clear();
- }
- }
- }
- public void setFalseSymbol(char s)
- {
- falseSym = s;
- }
- public void setTrueSymbol(char s)
- {
- trueSym = s;
- }
- public char getTrueSymbol()
- {
- return trueSym;
- }
- public char getFalseSymbol()
- {
- return falseSym;
- }
- public int getTotalSteps()
- {
- return Automata.size() -1;
- }
- public String getStateString(int stepNum)
- {
- return "S";
- }
- public int getRuleNum()
- {
- return ruleNumber;
- }
- }
- marcos code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement