Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CELL STATE
- @Override
- protected CellState nextCellState(Cell currentCell, Set<Cell> neighborsStates) {
- boolean isAntYourNeighbor=false;
- ArrayList<Cell> possibleAnts = new ArrayList<>();
- //finding which neighbor has ants
- for(Cell cell : neighborsStates){
- if(!(((LangtonCell)(cell.state)).ants.isEmpty())){
- isAntYourNeighbor=true;
- possibleAnts.add(cell);
- }
- }
- LangtonCell newState = new LangtonCell();
- //checking if currentCell has ants
- if(!(((LangtonCell)(currentCell.state)).ants.isEmpty())){
- //changing cell under ant whether ants are even
- newState.cellState=((LangtonCell)(currentCell.state)).changeCellState();
- //clearing ants from current cell
- newState.ants.clear();
- }
- else{
- newState.cellState=((LangtonCell)(currentCell.state)).cellState;
- newState.ants.clear();
- }
- //checking if current cell has ant neighbors
- if(isAntYourNeighbor){
- //changing direction of all ant neighbors and adding right ants
- for(Cell antCell : possibleAnts){
- ((LangtonCell)(antCell.state)).rotateAnts();
- for(Ant ant : ((LangtonCell)(antCell.state)).ants){
- //calculating coords on which ant neighbor is pointing
- Coords2D checkingCoords = null;
- if(ant.antState.equals(AntState.EAST)){
- checkingCoords = new Coords2D(Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).x+1,0, width, height), ((Coords2D)(antCell.coords)).y);
- }
- else if(ant.antState.equals(AntState.NORTH)){
- checkingCoords = new Coords2D(((Coords2D)(antCell.coords)).x, Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).y+1,1,width, height));
- }
- else if(ant.antState.equals(AntState.WEST)){
- checkingCoords = new Coords2D(Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).x-1,0,width, height), ((Coords2D)(antCell.coords)).y);
- }
- else{
- checkingCoords = new Coords2D(((Coords2D)(antCell.coords)).x, Automaton2Dim.convertToSize(((Coords2D)(antCell.coords)).y-1,1,width, height));
- }
- //checking if ant neighbor is pointing on current cell
- if(checkingCoords.equals((currentCell.coords))){
- newState.ants.add(ant);
- }
- }
- }
- }
- return newState;
- }
- AUTOMATON
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement