Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import java.util.HashSet;
- public abstract class Automaton {
- private Map<CellCoordinates, CellState> cells;
- private CellNeighborhood neighborsStrategy;
- private CellStateFactory stateFactory;
- public Automaton(CellNeighborhood neighborsStrategy, CellStateFactory stateFactory){
- this.neighborsStrategy=neighborsStrategy;
- this.stateFactory=stateFactory;
- }
- public void init() {
- cells=new HashMap<>();
- CellCoordinates currentCoords = initialCoordinates();
- while (hasNextCoordinates(currentCoords)) {
- currentCoords = nextCoordinates(currentCoords);
- cells.put(currentCoords, stateFactory.initialState(currentCoords));
- }
- }
- public Automaton nextState(){
- Automaton automaton = newInstance(stateFactory, neighborsStrategy);
- automaton.init();
- CellIterator iterator = cellIterator();
- iterator.currentState=initialCoordinates();
- CellIterator newIterator = automaton.cellIterator();
- newIterator.currentState=automaton.initialCoordinates();
- while(iterator.hasNext()){
- Cell cell = iterator.next();
- newIterator.next();
- Set<CellCoordinates> neighbors = this.neighborsStrategy.cellNeighbors(cell.coords);
- Set<Cell> mappedNeighbors = mapCoordinates(neighbors);
- CellState newState = nextCellState(cell, mappedNeighbors);
- newIterator.setState(newState);
- }
- return automaton;
- }
- public void insertStructure(Map<? extends CellCoordinates, ? extends CellState> map){
- for(CellCoordinates coords : map.keySet()){
- if(cells.containsKey(coords))
- setCellState(coords, map.get(coords));
- }
- }
- public CellIterator cellIterator(){
- return new CellIterator();
- }
- private Set<Cell> mapCoordinates(Set<CellCoordinates> coords){
- Set<Cell> tmp = new HashSet<>();
- for(CellCoordinates cellCoords: coords){
- tmp.add(new Cell(cells.get(cellCoords), cellCoords));
- }
- return tmp;
- }
- private void setCellState(CellCoordinates coords, CellState newState){
- cells.replace(coords, newState);
- }
- abstract protected Automaton newInstance(CellStateFactory factory, CellNeighborhood neighborhood);
- abstract protected CellCoordinates nextCoordinates(CellCoordinates coords);
- abstract protected boolean hasNextCoordinates(CellCoordinates coords);
- abstract protected CellCoordinates initialCoordinates();
- abstract protected CellState nextCellState(Cell currentCell, Set<Cell> neighborsState);
- public class CellIterator{
- private CellIterator(){}
- private CellCoordinates currentState;
- public void setCurrentState(CellCoordinates currentState){
- this.currentState=currentState;
- }
- public boolean hasNext(){
- if(hasNextCoordinates(currentState)){
- return true;
- }
- else{
- return false;
- }
- }
- public Cell next(){
- currentState=nextCoordinates(currentState);
- Cell cell = new Cell(cells.get(currentState), currentState);
- return cell;
- }
- public void setState(CellState newState){
- Automaton.this.setCellState(currentState, newState);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement