first commit
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=numberOfCells
|
||||
comment0.target=Automaton(int)
|
||||
comment0.text=\n\ Create\ a\ 1D\ automaton\ consisting\ of\ the\ given\ number\ of\ cells.\n\ @param\ numberOfCells\ The\ number\ of\ cells\ in\ the\ automaton.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ print()
|
||||
comment1.text=\n\ Print\ the\ current\ state\ of\ the\ automaton.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ update()
|
||||
comment2.text=\n\ Update\ the\ automaton\ to\ its\ next\ state.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ reset()
|
||||
comment3.text=\n\ Reset\ the\ automaton.\n
|
||||
comment4.params=left\ center\ right
|
||||
comment4.target=int\ calculateNextState(int,\ int,\ int)
|
||||
comment4.text=\n\ Calculate\ the\ next\ state\ of\ the\ center\ cell\n\ given\ current\ left,\ center\ and\ right\ cell\n\ values.\n\ This\ implements\ Wolfram\ code\ 110.\n\ @see\ https\://en.wikipedia.org/wiki/Wolfram_code\n\ @param\ left\ The\ state\ of\ the\ cell\ to\ the\ left\ of\ center.\n\ @param\ center\ The\ state\ of\ the\ center\ cell.\n\ @param\ right\ The\ state\ of\ the\ cell\ to\ the\ right\ of\ center.\n\ @return\ The\ new\ value\ of\ center\ (0\ or\ 1).\n
|
||||
numComments=5
|
@@ -0,0 +1,86 @@
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Model a 1D elementary cellular automaton.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29 - version 3
|
||||
*/
|
||||
public class Automaton
|
||||
{
|
||||
// The number of cells.
|
||||
private final int numberOfCells;
|
||||
// The state of the cells.
|
||||
private int[] state;
|
||||
|
||||
/**
|
||||
* Create a 1D automaton consisting of the given number of cells.
|
||||
* @param numberOfCells The number of cells in the automaton.
|
||||
*/
|
||||
public Automaton(int numberOfCells)
|
||||
{
|
||||
this.numberOfCells = numberOfCells;
|
||||
// Allow an extra element to avoid 'fencepost' errors.
|
||||
state = new int[numberOfCells + 1];
|
||||
// Seed the automaton with a single 'on' cell.
|
||||
state[numberOfCells / 2] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the current state of the automaton.
|
||||
*/
|
||||
public void print()
|
||||
{
|
||||
for(int cellValue : state) {
|
||||
System.out.print(cellValue == 1 ? "*" : " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the automaton to its next state.
|
||||
*/
|
||||
public void update()
|
||||
{
|
||||
// Build the new state in a separate array.
|
||||
int[] nextState = new int[state.length];
|
||||
// Use 0 for the non-existent value to the left of
|
||||
// the first cell.
|
||||
int left = 0;
|
||||
int center = state[0];
|
||||
for(int i = 0; i < numberOfCells; i++) {
|
||||
int right = state[i + 1];
|
||||
nextState[i] = calculateNextState(left, center, right);
|
||||
left = center;
|
||||
center = right;
|
||||
}
|
||||
state = nextState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the automaton.
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
Arrays.fill(state, 0);
|
||||
// Seed the automaton with a single 'on' cell in the middle.
|
||||
state[numberOfCells / 2] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the next state of the center cell
|
||||
* given current left, center and right cell
|
||||
* values.
|
||||
* This implements Wolfram code 110.
|
||||
* @see https://en.wikipedia.org/wiki/Wolfram_code
|
||||
* @param left The state of the cell to the left of center.
|
||||
* @param center The state of the center cell.
|
||||
* @param right The state of the cell to the right of center.
|
||||
* @return The new value of center (0 or 1).
|
||||
*/
|
||||
private int calculateNextState(int left, int center, int right)
|
||||
{
|
||||
return (center + right + center * right + left * center * right) % 2;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=numberOfCells
|
||||
comment0.target=AutomatonController(int)
|
||||
comment0.text=\n\ Create\ an\ AutomatonController.\n\ @param\ numberOfCells\ The\ number\ of\ cells\ in\ the\ automaton.\n
|
||||
comment1.params=
|
||||
comment1.target=AutomatonController()
|
||||
comment1.text=\n\ Create\ an\ AutomatonController\ with\n\ a\ default\ number\ of\ cells.\n
|
||||
comment2.params=numSteps
|
||||
comment2.target=void\ run(int)
|
||||
comment2.text=\n\ Run\ the\ automaton\ for\ the\ given\ number\ of\ steps.\n\ @param\ numSteps\ The\ number\ of\ steps.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ step()
|
||||
comment3.text=\n\ Run\ the\ automaton\ for\ a\ single\ step.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ reset()
|
||||
comment4.text=\n\ Reset\ the\ automaton.\n
|
||||
numComments=5
|
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Set up and control an elementary cellular automaton.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class AutomatonController
|
||||
{
|
||||
// The automaton.
|
||||
private Automaton auto;
|
||||
|
||||
/**
|
||||
* Create an AutomatonController.
|
||||
* @param numberOfCells The number of cells in the automaton.
|
||||
*/
|
||||
public AutomatonController(int numberOfCells)
|
||||
{
|
||||
auto = new Automaton(numberOfCells);
|
||||
auto.print();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an AutomatonController with
|
||||
* a default number of cells.
|
||||
*/
|
||||
public AutomatonController()
|
||||
{
|
||||
this(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the automaton for the given number of steps.
|
||||
* @param numSteps The number of steps.
|
||||
*/
|
||||
public void run(int numSteps)
|
||||
{
|
||||
for(int step = 1; step <= numSteps; step++) {
|
||||
step();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the automaton for a single step.
|
||||
*/
|
||||
public void step()
|
||||
{
|
||||
auto.update();
|
||||
auto.print();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the automaton.
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
auto.reset();
|
||||
auto.print();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
Project: automaton
|
||||
Aothors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 7.
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=AutomatonController
|
||||
dependency1.to=Automaton
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=737
|
||||
package.editor.height=400
|
||||
package.editor.width=629
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=700
|
||||
readme.editor.width=900
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=769
|
||||
target1.editor.width=980
|
||||
target1.editor.x=375
|
||||
target1.editor.y=141
|
||||
target1.height=50
|
||||
target1.name=Automaton
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=90
|
||||
target1.x=170
|
||||
target1.y=160
|
||||
target2.editor.height=777
|
||||
target2.editor.width=994
|
||||
target2.editor.x=286
|
||||
target2.editor.y=23
|
||||
target2.height=50
|
||||
target2.name=AutomatonController
|
||||
target2.naviview.expanded=false
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=160
|
||||
target2.x=70
|
||||
target2.y=70
|
Reference in New Issue
Block a user