Class Simulator

java.lang.Object
application.model.simulation.Grid
application.model.simulation.Simulator
All Implemented Interfaces:
java.lang.Iterable<Block>, java.util.Iterator<Block>
Direct Known Subclasses:
Pattern

public class Simulator
extends Grid
Simulations are run using Simulator.

Example Usage:
 Simulator simulator = new Simulator(new HROT("B3/S23"));
 simulator.fromRLE("bo$obo$o2bo$bobo$2bo!", new Coordinate(0, 0));
 simulator.step();
 iterateCells(cell -> {
      System.out.println(cell);
 });
 
  • Constructor Details

    • Simulator

      public Simulator​(Rule rule)
      Initialises the simulator
      Parameters:
      rule - The rule to be loaded
  • Method Details

    • getRule

      public Rule getRule()
      Gets the rule of the simulator
      Returns:
      Returns the rule of the simulator
    • getGeneration

      public int getGeneration()
      Gets the generation of the simulator
      Returns:
      Returns the generation of the simulator
    • getCellsChanged

      public java.util.Set<Coordinate> getCellsChanged()
      Gets the cells that changed in the previous generation
      Returns:
      Returns the cells that changed in the previous generation
    • setRule

      public void setRule​(Rule rule)
      Sets the rule of the simulator.
      IMPORTANT: DO NOT CHANGE THE RULE OF THE SIMULATOR WITH ANY OTHER FUNCTION! (Because bugzzz...)
      Parameters:
      rule - Rule to set the simulator to
    • setGeneration

      public void setGeneration​(int generation)
      Set the generation of the simulator
      Parameters:
      generation - Generation to set the simulator to
    • identify

      public Pattern identify()
      Identify a pattern (still life / oscillator / spaceship) with a max period of 2000
      Returns:
      The identified pattern
    • identify

      public Pattern identify​(int maxPeriod)
      Identify a pattern (still life / oscillator / spaceship) with the specified max period
      Parameters:
      maxPeriod - The max period to check for periodicity
      Returns:
      The identified pattern
    • identify

      public Pattern identify​(int maxPeriod, java.util.function.Function<Grid,​java.lang.Boolean> continueIdentification)
      Identify a pattern (still life / oscillator / spaceship) with the specified max period
      Parameters:
      maxPeriod - The max period to check for periodicity
      continueIdentification - Checks if the identification should continue
      Returns:
      The identified pattern
    • step

      public void step()
      Step the simulation forward 1 generation
    • step

      public void step​(java.util.function.Function<Coordinate,​java.lang.Boolean> step)
      Step a portion of the simulation forward 1 generation
      Parameters:
      step - A function that returns whether the cell at that coordinate should be stepped forward.
    • setCell

      public void setCell​(Coordinate coordinate, int state)
      Sets the cell at position coordinate to the specified state
      Overrides:
      setCell in class Grid
      Parameters:
      coordinate - The coordinate of the cell
      state - The state of the cell
    • setCell

      public void setCell​(int x, int y, int state)
      Sets the cell at position (x, y) to the specified state
      Overrides:
      setCell in class Grid
      Parameters:
      x - The x-coordinate of the cell
      y - The y-coordinate of the cell
      state - The state of the cell
    • clearCells

      public void clearCells​(Coordinate start, Coordinate end)
      Clears all cells between the coordinates specified
      Overrides:
      clearCells in class Grid
      Parameters:
      start - The starting coordinate
      end - The end coordinate
    • toRLE

      public java.lang.String toRLE​(Coordinate startCoordinate, Coordinate endCoordinate)
      Converts the pattern in the given bounds into an RLE
      Overrides:
      toRLE in class Grid
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - The end coordinate
      Returns:
      Returns the RLE