Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.edu.agh;
- import com.sun.prism.image.Coords;
- import java.util.Set;
- /**
- * Created by ewa on 12/1/16.
- */
- public class ElementaryAutomaton extends Automaton1Dim {
- private String rules;
- protected ElementaryAutomaton(CellStateFactory stateFactory, CellNeighbourhood stateStrategy, int size, String rules) {
- super(stateFactory, stateStrategy, size);
- if(rules.length()==8) this.rules=rules;
- }
- @Override
- protected Automaton newInstance(CellStateFactory cellStateFactory, CellNeighbourhood neighbourhoodStrategy) {
- int size = super.getSize();
- return new ElementaryAutomaton(cellStateFactory, neighbourhoodStrategy, size, rules);
- }
- @Override
- protected CellState nextCellState(Cell currentCell, Set<Cell> neigboursStates) {
- CellState currentCellState = currentCell.getState();
- Coords1D currentCellCoords = (Coords1D)currentCell.getCoords();
- Cell leftFriend = new Cell(BinaryState.DEAD, new Coords1D(-1));
- Cell rightFriend = new Cell(BinaryState.DEAD, new Coords1D(-1));
- //Rule 30:
- for(Cell cell : neigboursStates){
- Coords1D cellCoords = (Coords1D)cell.getCoords();
- if(cellCoords.getX()==currentCellCoords.getX()-1) {
- leftFriend = cell;
- }
- if(cellCoords.getX()==currentCellCoords.getX()+1){
- rightFriend = cell;
- }
- }
- CellState leftState = leftFriend.getState();
- CellState rightState = rightFriend.getState();
- //lepiej to przejść pętlą czy ifami?
- if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.ALIVE && rightState==BinaryState.ALIVE){
- if (rules.charAt(0)=='0') return BinaryState.DEAD;
- if (rules.charAt(0)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.ALIVE && rightState==BinaryState.DEAD){
- if (rules.charAt(1)=='0') return BinaryState.DEAD;
- if (rules.charAt(1)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.DEAD && rightState==BinaryState.ALIVE){
- if (rules.charAt(2)=='0') return BinaryState.DEAD;
- if (rules.charAt(2)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.ALIVE && currentCellState==BinaryState.DEAD && rightState==BinaryState.DEAD){
- if (rules.charAt(3)=='0') return BinaryState.DEAD;
- if (rules.charAt(3)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.DEAD && currentCellState==BinaryState.ALIVE && rightState==BinaryState.ALIVE){
- if (rules.charAt(4)=='0') return BinaryState.DEAD;
- if (rules.charAt(4)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.DEAD && currentCellState==BinaryState.ALIVE && rightState==BinaryState.DEAD){
- if (rules.charAt(5)=='0') return BinaryState.DEAD;
- if (rules.charAt(5)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.DEAD && currentCellState==BinaryState.DEAD && rightState==BinaryState.ALIVE){
- if (rules.charAt(6)=='0') return BinaryState.DEAD;
- if (rules.charAt(6)=='1') return BinaryState.ALIVE;
- }
- if(leftState==BinaryState.DEAD && currentCellState==BinaryState.DEAD && rightState==BinaryState.DEAD){
- if (rules.charAt(7)=='0') return BinaryState.DEAD;
- if (rules.charAt(7)=='1') return BinaryState.ALIVE;
- }
- return currentCellState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement