Skip to content

Commit

Permalink
add grid size menu
Browse files Browse the repository at this point in the history
  • Loading branch information
hqm committed Jun 16, 2014
1 parent b78eca7 commit 9f0a449
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 86 deletions.
8 changes: 5 additions & 3 deletions PBox.pde
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public HashMap<Coord, Cell> grid = new HashMap<Coord, Cell>();

// Keep track of which cell a given cell is supposed to swap with. Used to find and prevent
// conflicting swaps.
// Using a cell location as a key, holds the target location that this cell is proposed to swap with
// Using a cell location as a key, holds a list of target locations that this cell is proposed to swap with.
// To skip conflicts, we can make sure a pair of cells only is designated to swap with one another;
// If a swap is valid, cell A should list only cell B as a swap target, and cell B should list just cell A.
public HashMap<Coord, ArrayList<Coord>> swaps = new HashMap<Coord, ArrayList<Coord>>();

// List of all cells
Expand All @@ -80,15 +82,15 @@ public ArrayList<Cell> evenCells = new ArrayList<Cell>();
// List of cells in imaginary plane
public ArrayList<Cell> oddCells = new ArrayList<Cell>();

//For placing a predfined module (list of cells)
public ArrayList<Cell> moduleCells = new ArrayList<Cell>();
// store state of all cells, so we can reset back to original config in RAM
public ArrayList<Cell> stashCells = new ArrayList<Cell>();
int stashedClock = 0;

// for drawing center of mass trails
// for drawing center of mass trails, uses floating point values, hence PVector instead of Coord
public PVector[] trail = new PVector[trailSize];


// clear the grid and regenerate it from cells
void resetGrid() {
grid.clear();
Expand Down
31 changes: 30 additions & 1 deletion RootGUI.pde
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ItemListener {

JLabel movieLabel = new JLabel("Write Movie: off", JLabel.LEFT);
JLabel ncellsLabel = new JLabel("# cells: "+allCells.size(), JLabel.LEFT);


JLabel help = new JLabel(HELP_STRING, JLabel.LEFT);

Expand All @@ -29,12 +29,20 @@ ItemListener {




JMenuItem saveConfigItem = new JMenuItem("Save Config");
JMenuItem clearItem = new JMenuItem("Clear");
JMenuItem loadConfig = new JMenuItem("Load Config");
JMenuItem loadModule = new JMenuItem("Load Module");
JMenu file = new JMenu("File");

JMenu gridMenu = new JMenu("Grid");
JMenuItem grid16 = new JMenuItem("16");
JMenuItem grid32 = new JMenuItem("32");
JMenuItem grid64 = new JMenuItem("64");
JMenuItem grid128 = new JMenuItem("128");


JButton updateButton = new JButton("STASH");
JButton resetButton = new JButton("RESTORE");
JButton clearButton = new JButton("CLEAR");
Expand All @@ -56,6 +64,17 @@ ItemListener {
ruleList.addActionListener(this);
ruleList.setAlignmentX(Component.LEFT_ALIGNMENT);

gridMenu.add(grid16);
grid16.addActionListener(this);
gridMenu.add(grid32);
grid32.addActionListener(this);
gridMenu.add(grid64);
grid64.addActionListener(this);
gridMenu.add(grid128);
grid128.addActionListener(this);



updateButton.addActionListener(this);
resetButton.addActionListener(this);
clearButton.addActionListener(this);
Expand All @@ -80,6 +99,7 @@ ItemListener {
loadModule.addActionListener(this );

menuBar.add(file);
menuBar.add(gridMenu);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Expand Down Expand Up @@ -148,6 +168,14 @@ ItemListener {
app.fast = !app.fast;
} else if (cmd.equals("FORWARD/BACK")) {
app.toggleTime();
} else if (cmd.equals("16")) {
app.gridSize = 16;
} else if (cmd.equals("32")) {
app.gridSize = 32;
} else if (cmd.equals("64")) {
app.gridSize = 64;
} else if (cmd.equals("128")) {
app.gridSize = 128;
}
}

Expand All @@ -171,3 +199,4 @@ ItemListener {
+"': toggle default cursor value<br>"
+"</html>";
}

104 changes: 23 additions & 81 deletions Rule3.pde
Original file line number Diff line number Diff line change
Expand Up @@ -27,119 +27,61 @@ class Rule3 extends Rule {
b = new Coord(x2, y2, z2);
}
}
// moves XY plane

Swap dxy1 = new Swap(
1, 0, 0,
1, 0, 0);


Swap dxy2 = new Swap(
1, 0, 0,
0, 1, 0);

Coord testxy = new Coord(4, 0, 0);

// moves XZ plane

Swap dxz1 = new Swap(
0, 0, 1,
1, 0, 2);

Swap dxz2 = new Swap(
0, 1, 0,
0, 0, 1);

Coord testxz = new Coord(0, 0, 4);

// moves YZ plane
Swap dyz1 = new Swap(
0, 1, 0,
0, 2, 1);

Swap dyz2 = new Swap(
0, 0, 1,
1, 0, 0);

Coord testyz = new Coord(0, 4, 0);


void runRule() {
int phase = clockPhase();

ArrayList<Cell> cells;
if (phase % 2 == 0) {
cells = evenCells;
} else {
cells = oddCells;
}

switch(phase) {
case 0:
rule3(cells, dxy1, dxy2);
rule3(evenCells, phase);
break;
case 1:
rule3(cells, dxz1, dxz2);
rule3(oddCells, phase);
break;
case 2:
rule3(cells, dyz1, dyz2);
rule3(evenCells, phase);
break;
case 3:
rule3(cells, dxy1, dxy2);
rule3(oddCells, phase);
break;
case 4:
rule3(cells, dxz1, dxz2);
rule3(evenCells, phase);
break;
case 5:
rule3(cells, dyz1, dyz2);
rule3(oddCells, phase);
break;
}
}

Coord ptest1 = new Coord(0, 0, 0);
Coord ptest2 = new Coord(0, 0, 0);

void rule3(ArrayList<Cell> cells, Swap s1, Swap s2) {
void rule3(ArrayList<Cell> cells, int phase) {

for (Cell cell : cells) {
addCoords(cell.loc, s1.a, p1); // p1 := cell + delta1
addCoords(cell.loc, s1.b, p2); // p1 := cell + delta1
Cell cell1 = grid.get(p1);
Cell cell2 = grid.get(p2);

if ((cell2 != null && cell.state != cell2.state ) || (cell1 != null && cell.state != cell1.state )) {
addCoords(cell.loc, s1.a, p1); // p1 := cell + delta1
addCoords(cell.loc, s1.b, p2); // p1 := cell + delta1
switch(phase) {
case 0:
case 1:
p1.set(cell.loc);
p2.set(cell.loc);
p1.x -= 1;
p2.x -= 3;
proposeSwap(p1, p2);


subCoords(cell.loc, s1.a, p1); // p1 := cell + delta1
subCoords(cell.loc, s1.b, p2); // p1 := cell + delta1
proposeSwap(p1, p2);
} else {

addCoords(cell.loc, s2.a, p1); // p1 := cell + delta1
addCoords(cell.loc, s2.b, p2); // p1 := cell + delta1
proposeSwap(p1, p2);

subCoords(cell.loc, s2.a, p1); // p1 := cell + delta1
subCoords(cell.loc, s2.b, p2); // p1 := cell + delta1
p1.set(cell.loc);
p2.set(cell.loc);
p1.x += 1;
p2.x += 3;
proposeSwap(p1, p2);
break;
}
}
}

// the default starting configuration
void initConfig() {
int n = 14;
int x=0;
int y=0;
int z=0;
while (n-- > 0) {

addCell(x, y, z, 1);
y++;
}

void initConfig() {
addCell(0, 0, 0, 1);
addCell(1, 0, 0, 1);
}
}

2 changes: 1 addition & 1 deletion Rule4.pde
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Rule4 extends Rule {
for (int x = -N; x < N; x+=1) {
for (int y = -N; y < N; y+=2) {
for (int z = -N; z < N; z+=2) {
addCell(x, y, z, round(random(-1,1)));
addCell(x, y, z, round(random(-1, 1)));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Rule5.pde
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ class Rule5 extends Rule {
}
}
}

0 comments on commit 9f0a449

Please sign in to comment.