// Particles for a lattice gas. Goofy rool. import java.lang.Object; import java.util.Vector; import java.lang.Math; import HexCoord; import HexGrid; public class Particle extends Object { HexGrid myHexGrid; public HexCoord location; public int direction; // 0 is north, goes clockwise public boolean bumped; public Particle(HexGrid hg, int x, int y, int d) { location = new HexCoord(hg, x, y); myHexGrid = hg; hg.setElementAt(this, location); direction = d; } public String toString() { return new String("Particle: direction " + direction + " at " + location); } public void rotate(int i) { direction = (direction + i) % 6; if (direction < 0) direction += 6; } // move in the given direction and check for collisions // the collision detection stuff is funky - basically, only the first // particle gets to be on that spot. Everyone on the spot where there's // a collision is added to the list public void step() { HexCoord newLocation = new HexCoord(location); newLocation.moveInDirection(direction); newLocation.renormalize(); if (myHexGrid.elementAt(newLocation) == null) { bumped = false; // remove ourselves if appropriate if (myHexGrid.elementAt(location) == this) myHexGrid.setElementAt(null, location); myHexGrid.setElementAt(this, newLocation); location = newLocation; } else { if (bumped) // bumped last time rotate(Math.random() < 0.5 ? -1 : 1); // rotate randomly bumped = true; } } }