Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package pl.edu.agh;
  2.  
  3. import com.sun.prism.image.Coords;
  4.  
  5. import java.util.Set;
  6.  
  7. /**
  8.  * Created by ewa on 12/1/16.
  9.  */
  10. public class ElementaryAutomaton extends Automaton1Dim {
  11.     private String rules;
  12.     protected ElementaryAutomaton(CellStateFactory stateFactory, CellNeighbourhood stateStrategy, int size, String rules) {
  13.         super(stateFactory, stateStrategy, size);
  14.         if(rules.length()==8) this.rules=rules;
  15.     }
  16.  
  17.     @Override
  18.     protected Automaton newInstance(CellStateFactory cellStateFactory, CellNeighbourhood neighbourhoodStrategy) {
  19.         int size = super.getSize();
  20.         return new ElementaryAutomaton(cellStateFactory, neighbourhoodStrategy, size, rules);
  21.     }
  22.  
  23.     @Override
  24.     protected CellState nextCellState(Cell currentCell, Set<Cell> neigboursStates) {
  25.         CellState currentCellState = currentCell.getState();
  26.         Coords1D currentCellCoords = (Coords1D)currentCell.getCoords();
  27.         Cell leftFriend = new Cell(BinaryState.DEAD, new Coords1D(-1));
  28.         Cell rightFriend = new Cell(BinaryState.DEAD, new Coords1D(-1));
  29.  
  30.         //Rule 30:
  31.             for(Cell cell : neigboursStates){
  32.                 Coords1D cellCoords = (Coords1D)cell.getCoords();
  33.                 if(cellCoords.getX()==currentCellCoords.getX()-1) {
  34.                    leftFriend = cell;
  35.                 }
  36.                 if(cellCoords.getX()==currentCellCoords.getX()+1){
  37.                    rightFriend = cell;
  38.                 }
  39.             }
  40.  
  41.         CellState leftState = leftFriend.getState();
  42.         CellState rightState = rightFriend.getState();
  43.  
  44.         //lepiej to przejść pętlą czy ifami?
  45.         if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.ALIVE && rightState==BinaryState.ALIVE){
  46.             if (rules.charAt(0)=='0') return BinaryState.DEAD;
  47.             if (rules.charAt(0)=='1') return BinaryState.ALIVE;
  48.         }
  49.  
  50.         if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.ALIVE && rightState==BinaryState.DEAD){
  51.             if (rules.charAt(1)=='0') return BinaryState.DEAD;
  52.             if (rules.charAt(1)=='1') return BinaryState.ALIVE;
  53.         }
  54.  
  55.         if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.DEAD && rightState==BinaryState.ALIVE){
  56.             if (rules.charAt(2)=='0') return BinaryState.DEAD;
  57.             if (rules.charAt(2)=='1') return BinaryState.ALIVE;
  58.         }
  59.  
  60.         if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.DEAD && rightState==BinaryState.DEAD){
  61.             if (rules.charAt(3)=='0') return BinaryState.DEAD;
  62.             if (rules.charAt(3)=='1') return BinaryState.ALIVE;
  63.         }
  64.  
  65.         if(leftState==BinaryState.DEAD && currentCellState==BinaryState.ALIVE && rightState==BinaryState.ALIVE){
  66.             if (rules.charAt(4)=='0') return BinaryState.DEAD;
  67.             if (rules.charAt(4)=='1') return BinaryState.ALIVE;
  68.         }
  69.  
  70.         if(leftState==BinaryState.DEAD && currentCellState==BinaryState.ALIVE && rightState==BinaryState.DEAD){
  71.             if (rules.charAt(5)=='0') return BinaryState.DEAD;
  72.             if (rules.charAt(5)=='1') return BinaryState.ALIVE;
  73.         }
  74.  
  75.         if(leftState==BinaryState.DEAD && currentCellState==BinaryState.DEAD && rightState==BinaryState.ALIVE){
  76.             if (rules.charAt(6)=='0') return BinaryState.DEAD;
  77.             if (rules.charAt(6)=='1') return BinaryState.ALIVE;
  78.         }
  79.  
  80.         if(leftState==BinaryState.DEAD && currentCellState==BinaryState.DEAD && rightState==BinaryState.DEAD){
  81.             if (rules.charAt(7)=='0') return BinaryState.DEAD;
  82.             if (rules.charAt(7)=='1') return BinaryState.ALIVE;
  83.         }
  84.  
  85.         return currentCellState;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement