Advertisement
Coriic

Untitled

Dec 5th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. CELL STATE
  2. @Override
  3. protected CellState nextCellState(Cell currentCell, Set<Cell> neighborsStates) {
  4. boolean isAntYourNeighbor=false;
  5. ArrayList<Cell> possibleAnts = new ArrayList<>();
  6. //finding which neighbor has ants
  7. for(Cell cell : neighborsStates){
  8. if(!(((LangtonCell)(cell.state)).ants.isEmpty())){
  9. isAntYourNeighbor=true;
  10. possibleAnts.add(cell);
  11. }
  12. }
  13. LangtonCell newState = new LangtonCell();
  14. //checking if currentCell has ants
  15. if(!(((LangtonCell)(currentCell.state)).ants.isEmpty())){
  16. //changing cell under ant whether ants are even
  17. newState.cellState=((LangtonCell)(currentCell.state)).changeCellState();
  18. //clearing ants from current cell
  19. newState.ants.clear();
  20. }
  21. else{
  22. newState.cellState=((LangtonCell)(currentCell.state)).cellState;
  23. newState.ants.clear();
  24. }
  25. //checking if current cell has ant neighbors
  26. if(isAntYourNeighbor){
  27. //changing direction of all ant neighbors and adding right ants
  28. for(Cell antCell : possibleAnts){
  29. ((LangtonCell)(antCell.state)).rotateAnts();
  30. for(Ant ant : ((LangtonCell)(antCell.state)).ants){
  31. //calculating coords on which ant neighbor is pointing
  32. Coords2D checkingCoords = null;
  33. if(ant.antState.equals(AntState.EAST)){
  34. checkingCoords = new Coords2D(Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).x+1,0, width, height), ((Coords2D)(antCell.coords)).y);
  35. }
  36. else if(ant.antState.equals(AntState.NORTH)){
  37. checkingCoords = new Coords2D(((Coords2D)(antCell.coords)).x, Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).y+1,1,width, height));
  38. }
  39. else if(ant.antState.equals(AntState.WEST)){
  40. checkingCoords = new Coords2D(Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).x-1,0,width, height), ((Coords2D)(antCell.coords)).y);
  41. }
  42. else{
  43. checkingCoords = new Coords2D(((Coords2D)(antCell.coords)).x, Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).y-1,1,width, height));
  44. }
  45.  
  46. //checking if ant neighbor is pointing on current cell
  47. if(checkingCoords.equals((currentCell.coords))){
  48. newState.ants.add(ant);
  49. }
  50. }
  51. }
  52.  
  53. }
  54. return newState;
  55. }
  56.  
  57.  
  58. AUTOMATON
  59. public Automaton nextState(){
  60. Automaton automaton = newInstance(stateFactory, neighborsStrategy);
  61. automaton.init();
  62. CellIterator iterator = cellIterator();
  63. iterator.currentState=initialCoordinates();
  64. CellIterator newIterator = automaton.cellIterator();
  65. newIterator.currentState=automaton.initialCoordinates();
  66. while(iterator.hasNext()){
  67. Cell cell = iterator.next();
  68. newIterator.next();
  69. Set<CellCoordinates> neighbors = this.neighborsStrategy.cellNeighbors(cell.coords);
  70. Set<Cell> mappedNeighbors = mapCoordinates(neighbors);
  71. CellState newState = nextCellState(cell, mappedNeighbors);
  72. newIterator.setState(newState);
  73. }
  74. return automaton;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement