Custom status


“It’s possible to customize the status in order to support more complex Cellular Automata than the Game of Life example.

The following code represent a Game of Life implementation but using a custom status.

The custom status

You can add what you want inside and use the status inside the cellular automata and the transition function.

public class GoLStatus extends DefaultStatus {

	public boolean isAlive;

	public GoLStatus(boolean isAlive) {
		super("GoLStatus", null);
		this.isAlive = isAlive;
	}

	@Override
	public String toString() {
		return isAlive ? "1 " : "0 ";
	}

}

The executor

In the transition function you should cast the DefaultStatus with your object in order to use all params and feature inside your custom status.

public class GoLDSExecutor extends CellularAutomataExecutor {

	@Override
	public DefaultCell singleRun(DefaultCell cell, List<DefaultCell> neighbors) {

		Long alives = neighbors.stream().filter(item -> ((GoLStatus) item.currentStatus).isAlive).count();
		
		boolean isAlive = ((GoLStatus)cell.currentStatus).isAlive;
		
		DefaultCell toReturn = new DefaultCell(null, cell.getRow(), cell.getCol());
		if (!isAlive && alives == 3) {
			toReturn.currentStatus = new GoLStatus(true);
		} else if (isAlive && (alives == 2 || alives == 3)) {
			toReturn.currentStatus = new GoLStatus(true);
		} else {
			toReturn.currentStatus = new GoLStatus(false);
		}

		return toReturn;
	}

}

The main application

The main application has to use the custom status and not the DefaultStatus instance.


public class GoLDSApplication {

	public static GoLStatus status = new GoLStatus(false);
	public static int totalInteraction = 1, width = 10, height = 10;

	public static void main(String[] args) throws Exception {

		Long start = System.currentTimeMillis();

		CellularAutomataConfigurationBuilder builder = new CellularAutomataConfigurationBuilder();

		List<DefaultCell> initalState = new ArrayList<DefaultCell>();

		initalState.add(new DefaultCell(new GoLStatus(true), 1, 1));
		initalState.add(new DefaultCell(new GoLStatus(true), 1, 2));
		initalState.add(new DefaultCell(new GoLStatus(true), 1, 3));
		initalState.add(new DefaultCell(new GoLStatus(true), 2, 1));

		CellularAutomata ca = new CellularAutomata();
		ca.init(builder.setWidth(width)
						.setHeight(height)
						.setInfinite(false)
						.setTotalIterations(totalInteraction)
						.setNeighborhoodType(NeighborhoodType.MOORE)
						.setDefaultStatus(status)
						.setInitalState(initalState)
				.build());

		Long end = System.currentTimeMillis();

		System.out.println(ca.toString());
		System.out.println("Init elapsed: " + (end - start) / 1000 + " seconds.");
		System.out.println();

		GoLDSExecutor gol = new GoLDSExecutor();

		try {
			start = System.currentTimeMillis();
			ca = gol.run(ca);
			System.out.println(ca.toString());
			end = System.currentTimeMillis();
			System.out.println("Single thread --> Elapsed: " + (end - start) + " seconds.");
			System.out.println();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}