Commits

zetro  committed 14e1b3e

Moved cellular automata from experimental.

  • Participants
  • Parent commits b6bf423

Comments (0)

Files changed (8)

File src/org/bitbucket/zetro/squaredino/backend/AbstractAutomaton.java

 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import org.bitbucket.zetro.squaredino.backend.experimental.BrianBrain;
-import org.bitbucket.zetro.squaredino.backend.experimental.CellularAutomaton;
-import org.bitbucket.zetro.squaredino.backend.experimental.GameOfLife;
-import org.bitbucket.zetro.squaredino.backend.experimental.LangtonAnt;
 
 /**
  * An abstract automaton class for updating cell boards.

File src/org/bitbucket/zetro/squaredino/backend/BrianBrain.java

+package org.bitbucket.zetro.squaredino.backend;
+
+import java.util.Arrays;
+import java.util.List;
+import org.bitbucket.zetro.squaredino.backend.Cell;
+
+/**
+ * Implementation of Brian's Brain.
+ */
+public class BrianBrain extends CellularAutomaton {
+	/**
+	 * States.
+	 */
+	public static final Cell OFF = null,
+			ON = Cell.createCell(1),
+			DYING = Cell.createCell(2);
+
+	@Override
+	public List<Cell> getStates() {
+		return Arrays.asList(ON, DYING, OFF);
+	}
+
+	@Override
+	public Cell getNextState(Cell cell, Cell... neighbours) {
+		if (cell == null) {
+			if (getCount(neighbours, ON) == 2) {
+				return ON;
+			} else {
+				return cell;
+			}
+		} else if (cell.equals(ON)) {
+			return DYING;
+		} else if (cell.equals(DYING)) {
+			return OFF;
+		}
+		throw new IllegalArgumentException(String.valueOf(cell));
+	}
+}

File src/org/bitbucket/zetro/squaredino/backend/CellularAutomaton.java

+package org.bitbucket.zetro.squaredino.backend;
+
+import java.util.List;
+
+/**
+ * An abstract class for implementing diffrent cellular automatons that updates
+ * depending on its neighbours.
+ */
+public abstract class CellularAutomaton {
+	/**
+	 * Returns the next state of a given cell.
+	 *
+	 * @param cell the given cell
+	 * @param neighbours neighbours
+	 * @return the next state of the given cell
+	 */
+	public abstract Cell getNextState(Cell cell, Cell... neighbours);
+
+	/**
+	 * Returns a list of possible (or common) states.
+	 *
+	 * @return a list of possible (or common) states
+	 */
+	public abstract List<Cell> getStates();
+
+	/**
+	 * Returns the amount of neighbours of a specific cell type (state).
+	 *
+	 * @param neighbours neighbours
+	 * @param cell cell type (state)
+	 * @return the amount of neighbours of a specific cell type (state)
+	 */
+	public static int getCount(Cell[] neighbours, Cell cell) {
+		int count = 0;
+		for (Cell neighbour : neighbours) {
+			if (cell.equals(neighbour)) {
+				count++;
+			}
+		}
+		return count;
+	}
+}

File src/org/bitbucket/zetro/squaredino/backend/GameOfLife.java

+package org.bitbucket.zetro.squaredino.backend;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Implementation of (Conway's) Game of Life.
+ */
+public class GameOfLife extends CellularAutomaton {
+	/**
+	 * States.
+	 */
+	public static final Cell ALIVE = Cell.createCell(1), DEAD = null;
+	private Collection<Integer> born, survive;
+
+	/**
+	 * GameOfLife constructor. Standard Game of Life uses rule "B3/S23".
+	 *
+	 * @param born list of neighbour counts that gives living cells
+	 * @param survive list of neighbour counts keeps the state of a cell
+	 */
+	public GameOfLife(Collection<Integer> born, Collection<Integer> survive) {
+		this.born = born;
+		this.survive = survive;
+	}
+
+	@Override
+	public List<Cell> getStates() {
+		return Arrays.asList(ALIVE, DEAD);
+	}
+
+	@Override
+	public Cell getNextState(Cell cell, Cell... neighbours) {
+		int c = getCount(neighbours, ALIVE);
+		if (cell == DEAD && born.contains(c)) {
+			return ALIVE;
+		} else if (survive.contains(c)) {
+			return cell;
+		}
+		return DEAD;
+	}
+}

File src/org/bitbucket/zetro/squaredino/backend/LangtonAnt.java

+package org.bitbucket.zetro.squaredino.backend;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Implementation of Langton's Ant.
+ */
+public class LangtonAnt extends CellularAutomaton {
+	private static final int GROUND_START = 3;
+	private final String turns;
+
+	/**
+	 * LangtonAnt constructor. Standard Langton's Ant uses rule "RL".
+	 *
+	 * @param turns turn rules
+	 */
+	public LangtonAnt(String turns) {
+		this.turns = turns;
+	}
+
+	@Override
+	public List<Cell> getStates() {
+		return Arrays.asList(Cell.createCell(1 << GROUND_START), Cell.createCell((1 << GROUND_START) + 1), null);
+	}
+
+	@Override
+	public Cell getNextState(Cell cell, Cell... neighbours) {
+		int ground = cell != null ? cell.state >>> 3 : 0;
+
+		for (int i = 0; i < neighbours.length; i += 2) {
+			Cell neighbour = neighbours[i];
+			int state = neighbour != null ? neighbour.state : 0;
+			boolean isAnt = (state & 0x01) == 1;
+			int direction = (state & 0x06) >>> 1;
+
+			if (isAnt && (direction + 2) % 4 == i / 2) {
+				direction = (direction - (turns.charAt(ground) == 'R' ? 1 : -1) + 4) % 4;
+				ground = (ground + 1) % turns.length();
+
+				state = (ground << GROUND_START) + (direction << 1) + 1;
+
+				return Cell.createCell(state);
+			}
+		}
+
+		return ground != 0 ? Cell.createCell(ground << GROUND_START) : null;
+	}
+}

File src/org/bitbucket/zetro/squaredino/gui/BoardPanel.java

 import org.bitbucket.zetro.squaredino.backend.Board;
 import org.bitbucket.zetro.squaredino.backend.BoardFactory;
 import org.bitbucket.zetro.squaredino.backend.Cell;
-import org.bitbucket.zetro.squaredino.backend.experimental.GameOfLife;
+import org.bitbucket.zetro.squaredino.backend.GameOfLife;
 import org.bitbucket.zetro.squaredino.misc.Point;
 import org.bitbucket.zetro.squaredino.misc.Selection;
 

File src/org/bitbucket/zetro/squaredino/gui/ExportDialog.java

 
 import org.bitbucket.zetro.squaredino.backend.Board;
 import org.bitbucket.zetro.squaredino.backend.BoardImpl;
-import org.bitbucket.zetro.squaredino.backend.Cell;
-import org.bitbucket.zetro.squaredino.backend.experimental.GameOfLife;
+import org.bitbucket.zetro.squaredino.backend.GameOfLife;
 import org.bitbucket.zetro.squaredino.io.BoardData;
 import org.bitbucket.zetro.squaredino.io.SquareIO;
 

File src/org/bitbucket/zetro/squaredino/io/SquareIO.java

 import org.bitbucket.zetro.squaredino.backend.Board;
 import org.bitbucket.zetro.squaredino.backend.BoardFactory;
 import org.bitbucket.zetro.squaredino.backend.Cell;
-import org.bitbucket.zetro.squaredino.backend.experimental.GameOfLife;
+import org.bitbucket.zetro.squaredino.backend.GameOfLife;
 import org.bitbucket.zetro.squaredino.misc.Point;
 
 /**