Advertisement
Coriic

Untitled

Dec 4th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.HashSet;
  5.  
  6. public abstract class Automaton {
  7. private Map<CellCoordinates, CellState> cells;
  8. private CellNeighborhood neighborsStrategy;
  9. private CellStateFactory stateFactory;
  10. public Automaton(CellNeighborhood neighborsStrategy, CellStateFactory stateFactory){
  11. this.neighborsStrategy=neighborsStrategy;
  12. this.stateFactory=stateFactory;
  13. }
  14. public void init() {
  15. cells=new HashMap<>();
  16. CellCoordinates currentCoords = initialCoordinates();
  17. while (hasNextCoordinates(currentCoords)) {
  18. currentCoords = nextCoordinates(currentCoords);
  19. cells.put(currentCoords, stateFactory.initialState(currentCoords));
  20. }
  21. }
  22. public Automaton nextState(){
  23. Automaton automaton = newInstance(stateFactory, neighborsStrategy);
  24. automaton.init();
  25. CellIterator iterator = cellIterator();
  26. iterator.currentState=initialCoordinates();
  27. CellIterator newIterator = automaton.cellIterator();
  28. newIterator.currentState=automaton.initialCoordinates();
  29. while(iterator.hasNext()){
  30. Cell cell = iterator.next();
  31. newIterator.next();
  32. Set<CellCoordinates> neighbors = this.neighborsStrategy.cellNeighbors(cell.coords);
  33. Set<Cell> mappedNeighbors = mapCoordinates(neighbors);
  34. CellState newState = nextCellState(cell, mappedNeighbors);
  35. newIterator.setState(newState);
  36. }
  37. return automaton;
  38. }
  39. public void insertStructure(Map<? extends CellCoordinates, ? extends CellState> map){
  40. for(CellCoordinates coords : map.keySet()){
  41. if(cells.containsKey(coords))
  42. setCellState(coords, map.get(coords));
  43. }
  44. }
  45. public CellIterator cellIterator(){
  46. return new CellIterator();
  47. }
  48. private Set<Cell> mapCoordinates(Set<CellCoordinates> coords){
  49. Set<Cell> tmp = new HashSet<>();
  50. for(CellCoordinates cellCoords: coords){
  51. tmp.add(new Cell(cells.get(cellCoords), cellCoords));
  52. }
  53. return tmp;
  54. }
  55. private void setCellState(CellCoordinates coords, CellState newState){
  56. cells.replace(coords, newState);
  57. }
  58. abstract protected Automaton newInstance(CellStateFactory factory, CellNeighborhood neighborhood);
  59. abstract protected CellCoordinates nextCoordinates(CellCoordinates coords);
  60. abstract protected boolean hasNextCoordinates(CellCoordinates coords);
  61. abstract protected CellCoordinates initialCoordinates();
  62. abstract protected CellState nextCellState(Cell currentCell, Set<Cell> neighborsState);
  63. public class CellIterator{
  64. private CellIterator(){}
  65. private CellCoordinates currentState;
  66. public void setCurrentState(CellCoordinates currentState){
  67. this.currentState=currentState;
  68. }
  69. public boolean hasNext(){
  70. if(hasNextCoordinates(currentState)){
  71. return true;
  72. }
  73. else{
  74. return false;
  75. }
  76. }
  77. public Cell next(){
  78. currentState=nextCoordinates(currentState);
  79. Cell cell = new Cell(cells.get(currentState), currentState);
  80. return cell;
  81. }
  82. public void setState(CellState newState){
  83. Automaton.this.setCellState(currentState, newState);
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement