Advertisement
Guest User

csfsfsfsfffs

a guest
Feb 17th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. //worked with Zachary Muller and Marc Hanna on the logistics of the project
  9. public class Automaton {
  10. private boolean[] ruleSet = new boolean[8];
  11. private ArrayList<ArrayList<Boolean>> Automata = new ArrayList<ArrayList<Boolean>>();
  12. private char falseSym = '0';
  13. private char trueSym = '1';
  14. private int ruleNumber;
  15. private ArrayList<Boolean> initialState = new ArrayList<Boolean>();
  16. private ArrayList<Boolean> current = new ArrayList<Boolean>(); //cant use char, must use obj version
  17. private String binary;
  18. public Automaton(int ruleNumber, ArrayList<Boolean> initialState)
  19. {
  20. setRules(ruleNumber);
  21. Automata.clear();
  22. this.ruleNumber = ruleNumber;
  23. for (int i = 0; i< initialState.size(); i++)
  24. {
  25. this.initialState.add(initialState.get(i));
  26. }
  27.  
  28. Automata.add(initialState);
  29. }
  30. public Automaton(String filename) throws FileNotFoundException
  31. {
  32. Scanner filereader = new Scanner(new File(filename));
  33.  
  34. ruleNumber = filereader.nextInt();
  35. falseSym = filereader.next().charAt(0);
  36. setFalseSymbol(falseSym);
  37. trueSym = filereader.next().charAt(0);
  38. setTrueSymbol(trueSym);
  39. String tempState = filereader.next();
  40.  
  41. for(int i = 0; i<tempState.length(); i++) {
  42. if (tempState.charAt(i) == trueSym)
  43. {
  44. initialState.add(true);
  45.  
  46. }
  47. else
  48. {
  49. initialState.add(false);
  50. }
  51. }
  52.  
  53.  
  54. }
  55.  
  56. public void setRules(int ruleNumber)
  57. {
  58. this.ruleNumber = ruleNumber;
  59. int leadingZeroes;
  60. binary = Integer.toBinaryString(ruleNumber);
  61. String missing = "";
  62. if (binary.length() < 8) {
  63. leadingZeroes = 8 - binary.length();
  64. for (int i = 0; i < leadingZeroes; i++) {
  65. missing += "0";
  66. }
  67. binary = missing + binary;
  68. }
  69. for (int i = 0; i < binary.length(); i++)
  70. {
  71. if (binary.charAt(i) == '0')
  72. {
  73. ruleSet[i] = false;
  74. }
  75. else
  76. {
  77. ruleSet[i] = true;
  78. }
  79. }
  80. }
  81. public String toString()
  82. {
  83. String ECATotal = "";
  84. for(int i = 0; i<Automata.size(); i++)
  85. {
  86. if(i < Automata.size() -1)
  87. {
  88. ECATotal = ECATotal + getStateString(i) + '\n';
  89. }
  90. else
  91. {
  92. ECATotal = ECATotal + getStateString(i);
  93. }
  94. }
  95. return ECATotal;
  96. }
  97. public void save(String filename) throws IOException
  98. {
  99. PrintWriter saver = new PrintWriter(new File(filename));
  100. String binaryTotal = "";
  101. for(int i = 0; i< initialState.size(); i++) //Marc helped me with this for loop.
  102. {
  103. if (initialState.get(i) == true)
  104. {
  105. binaryTotal = binaryTotal + '1';
  106. }
  107. else
  108. {
  109. binaryTotal = binaryTotal + '0';
  110. }
  111. }
  112. saver.println(binaryTotal);
  113. saver.close();
  114. }
  115. public void evolve(int numSteps, int ruleNumber)
  116. {
  117. setRules(ruleNumber);
  118. //Marc Hanna explained to me the logic of this method.
  119. int stepCount = 0;
  120. //method generates an entire new line for the evolution
  121. while(stepCount<numSteps)
  122. {
  123. stepCount++;
  124. for(int i = 0; i< initialState.size(); i++)
  125. {
  126. int leftNeigh = 0;
  127. int rightNeigh = 0;
  128.  
  129. if(i == 0)
  130. {
  131. leftNeigh = initialState.size() -1;
  132. rightNeigh = i + 1;
  133. }
  134. else if(i == initialState.size() -1)
  135. {
  136. leftNeigh = i - 1;
  137. rightNeigh = 0;
  138. }
  139. else if(i>0 && i < initialState.size() -1 )
  140. {
  141. leftNeigh = i - 1;
  142. rightNeigh = i + 1;
  143. }
  144. if(initialState.get(leftNeigh) && initialState.get(i) && initialState.get(rightNeigh))
  145. {
  146. current.add(i, (ruleSet[0]));
  147. }
  148. else if(initialState.get(leftNeigh) && initialState.get(i) && initialState.get(rightNeigh)==false)
  149. {
  150. current.add(i, (ruleSet[1]));
  151. }
  152. else if(initialState.get(leftNeigh) && initialState.get(i)==false && initialState.get(rightNeigh))
  153. {
  154. current.add(i, (ruleSet[2]));
  155. }
  156. else if(initialState.get(leftNeigh) && initialState.get(i)==false && initialState.get(rightNeigh)==false)
  157. {
  158. current.add(i, (ruleSet[3]));
  159. }
  160. else if(initialState.get(leftNeigh)==false && initialState.get(i) && initialState.get(rightNeigh))
  161. {
  162. current.add(i, (ruleSet[4]));
  163. }
  164. else if(initialState.get(leftNeigh)==false && initialState.get(i) && initialState.get(rightNeigh)==false)
  165. {
  166. current.add(i, (ruleSet[5]));
  167. }
  168. else if(initialState.get(leftNeigh)==false && initialState.get(i)==false && initialState.get(rightNeigh))
  169. {
  170. current.add(i, (ruleSet[6]));
  171. }
  172. else if(!initialState.get(leftNeigh) && !initialState.get(i) && !initialState.get(rightNeigh))
  173. {
  174. current.add(i, (ruleSet[7]));
  175. }
  176. Automata.add(current);
  177. current.clear();
  178.  
  179. }
  180. }
  181. }
  182. public void setFalseSymbol(char s)
  183. {
  184. falseSym = s;
  185. }
  186. public void setTrueSymbol(char s)
  187. {
  188. trueSym = s;
  189. }
  190. public char getTrueSymbol()
  191. {
  192. return trueSym;
  193. }
  194. public char getFalseSymbol()
  195. {
  196. return falseSym;
  197. }
  198. public int getTotalSteps()
  199. {
  200. return Automata.size() -1;
  201. }
  202. public String getStateString(int stepNum)
  203. {
  204. return "S";
  205. }
  206. public int getRuleNum()
  207. {
  208. return ruleNumber;
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. }
  219. marcos code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement