Commits

zetro  committed 7ff1627

Made GUI work with the new cellular automata.

  • Participants
  • Parent commits e91fd66

Comments (0)

Files changed (3)

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

 			return null;
 		}
 
+		// Check if an ant moves from this cell
 		boolean isAnt = cellState >= turns.length();
 		if (isAnt) {
 			cellGround = (cellGround + 1) % turns.length(); // Flip ground

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

 
 	@Override
 	public Cell getNextState(Cell cell, Cell... neighbours) {
-		int ground = cell != null ? cell.state >>> GROUND_START : 0;
+		int cellState = cell != null ? cell.state : 0;
+		int cellGround = cell != null ? cell.state >>> GROUND_START : 0;
+		if (cellGround < 0 || cellGround > table[0].length() / 4) {
+			System.err.println("Warning: Invalid ground value " + cellGround + " in cell " + cell);
+			return null;
+		}
 
-		if (ground < 0 || ground > table[0].length() / 4) {
-			System.err.println("Warning: Invalid ground value " + ground + " in cell " + cell);
-			return null;
+		// Check if an ant moves from this cell
+		boolean isAnt = (cellState & 0x01) == 1;
+		if (isAnt) {
+			int antState = (cellState & (((1 << (GROUND_START - STATE_START)) - 1) << STATE_START)) >>> STATE_START;
+
+			if (antState < 0 || antState > table.length - 1) {
+				System.err.println("Warning: Invalid ant state " + antState + " in cell " + cell);
+				return null;
+			}
+
+			// Update ground
+			cellGround = table[antState].charAt(0 + cellGround * 4) - '0';
 		}
 
 		// Check if any ortogonal neighbour is an ant moving to this cell.
 		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;
-			int antState = (state & (((1 << (GROUND_START - STATE_START)) - 1) << STATE_START)) >>> STATE_START;
-
-			if (antState < 0 || antState > table.length - 1) {
-				System.err.println("Warning: Invalid ant state " + antState + " in cell " + neighbour);
-				return null;
-			}
 
 			// Check if neighbour is an ant moving to this cell.
-			if (isAnt && (direction + 2) % 4 == i / 2) {
-				// Update state
-				int newGround = table[antState].charAt(0 + ground * 4) - '0';
-				antState = table[antState].charAt(2 + ground * 4) - '0';
+			isAnt = (state & 0x01) == 1;
+			if (isAnt) {
+				// Retrieve what the ant stands on
+				int ground = state >>> GROUND_START;
+				if (ground < 0 || ground > table[0].length() / 4) {
+					System.err.println("Warning: Invalid ground value " + ground + " in cell " + neighbour);
+					return null;
+				}
+
+				// Current state of the ant
+				int antState = (state & (((1 << (GROUND_START - STATE_START)) - 1) << STATE_START)) >>> STATE_START;
+				if (antState < 0 || antState > table.length - 1) {
+					System.err.println("Warning: Invalid ant state " + antState + " in cell " + neighbour);
+					return null;
+				}
 
 				// Update direction
 				int turn = table[antState].charAt(1 + ground * 4);
-				if (turn == 'R') { // Right
+				int direction = (state & 0x06) >>> 1;
+				if (turn == 'R') { // Right (relative)
 					direction++;
-				} else if (turn == 'L') { // Left
+				} else if (turn == 'L') { // Left (relative)
 					direction--;
-				} else if (turn == 'U') { // U-turn
+				} else if (turn == 'U') { // U-turn (relative)
 					direction += 2;
+				} else if (turn == 'n') { // North (absolute)
+					direction = 0;
+				} else if (turn == 'e') { // East (absolute)
+					direction = 1;
+				} else if (turn == 's') { // South (absolute)
+					direction = 2;
+				} else if (turn == 'w') { // West (absolute)
+					direction = 3;
 				} else if (turn != 'N') { // No turn
 					System.err.println("Unknown turn: " + turn);
 				}
 				direction = (direction + 4) % 4;
 
-				// Calculate new state
-				state = (newGround << GROUND_START) + (antState << STATE_START) + (direction << 1) + 1;
-				return Cell.createCell(state);
+				// Did the ant turn and move to towards this cell?
+				if ((direction + 2) % 4 == i / 2) {
+					// Update state
+					antState = table[antState].charAt(2 + ground * 4) - '0';
+
+					// Calculate new state
+					state = (cellGround << GROUND_START) + (antState << STATE_START) + (direction << 1) + 1;
+					return Cell.createCell(state);
+				}
 			}
 		}
 
 		// Ground cell
-		return ground != 0 ? Cell.createCell(ground << GROUND_START) : null;
+		return Cell.createCell(cellGround << GROUND_START);
 	}
 
 	@Override

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

 import java.awt.image.BufferedImage;
 import java.awt.image.WritableRaster;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.swing.JPanel;
 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.GameOfLife;
 import org.bitbucket.zetro.squaredino.misc.Point;
 import org.bitbucket.zetro.squaredino.misc.Selection;
 
 
 public class BoardPanel extends JPanel {
 	private static final long serialVersionUID = 4361849887580197351L;
+	private static final Color[] cellColors = {Color.LIGHT_GRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.RED, Color.ORANGE, Color.MAGENTA, Color.CYAN};
+
 	private Board board;
 	
 	private int width, height; //number of cells
 	private Point tempSelmove; 
 	
 	private boolean rightClicking = false; 
-	private boolean isKilling = false; 
+	private Cell paintCell; 
 	
 	private boolean copySelection = false;
 	private boolean moveDead = true;
 			public void mouseDragged(MouseEvent e) {
 				Point p = calPos(e.getX() - tempaddx, e.getY() - tempaddy);
 				if(rightClicking) {
-					if(isKilling && board.getCell(p.x, p.y) != null)
-						board.removeCell(p.x, p.y); 
-					else if(!isKilling && board.getCell(p.x, p.y) == null)
-						board.addCell(GameOfLife.ALIVE, p.x, p.y); 
+					setCell(paintCell, p.x, p.y);
 				}
 				else {
 					if(isSelecting) {
 				 */
 				if(e.getButton() == MouseEvent.BUTTON3) { 
 					rightClicking = true; 
-					if(board.getCell(p.x, p.y) == null) {
-						board.addCell(GameOfLife.ALIVE, p.x, p.y);
-						isKilling = false; 
-					}
-					else {
-						board.removeCell(p.x, p.y); 
-						isKilling = true; 
-					}
+					paintCell = getPaintCell(p.x, p.y);
+					setCell(paintCell, p.x, p.y); 
 				}
 				/*
 				 * Pressed something else (typically left mousebutton)
 						selected.moveX(tempSelmove.x); 
 						selected.moveY(tempSelmove.y);
 						if (moveDead)
-								board.clear(selected);
+							board.clear(selected);
 						board.insertBoard(selection, tempSelmove.x, tempSelmove.y); 
 					} else {
 						if (moveDead)
 				else {
 					if(!isSelecting) {
 						Point p = calPos(e.getX(), e.getY()); 
-						if(board.getCell(p.x, p.y) == null)
-							board.addCell(GameOfLife.ALIVE, p.x, p.y); 
-						else
-							board.removeCell(p.x, p.y);
+						paintCell = getPaintCell(p.x, p.y); 
+						setCell(paintCell, p.x, p.y); 
 						System.out.println(p.x + ":" + p.y);
 						repaint(); 
 					}
 			}
 		});
 	}
+
+	private Cell getPaintCell(int x, int y) {
+		List<Cell> states = board.getStates();
+		int idx = states.indexOf(board.getCell(x, y));
+		return states.get((idx + 1) % states.size());
+	}
+	
+	private void setCell(Cell cell, int x, int y) {
+		if (paintCell == null) {
+			board.removeCell(x, y);
+		} else {
+			board.addCell(cell, x, y);
+		}
+	}
+	
 	public Board getBoard() {
 		return board; 
 	}
 		}
 		Iterator<Map.Entry<Point, Cell>> cells = board.getLivingCells(view, ignoreLevel); 
 		int size = (int) Math.max(1, zoom);
-		if (size == 1) {
+		if (ignoreLevel > 0) {
 			int w = getWidth();
 			int h = getHeight();
 			BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
 			WritableRaster raster = image.getRaster();
 			
-			Object c = image.getColorModel().getDataElements(Color.black.getRGB(), null);
+			Object c = image.getColorModel().getDataElements(cellColors[1].getRGB(), null);
 			while(cells.hasNext()) {
 				Point p = cells.next().getKey(); 
 				int x = paddx + tempaddx + (int) (p.x * zoom);
 			g.drawImage(image, 0, 0, null);
 		} else {
 			while(cells.hasNext()) {
-				Point p = cells.next().getKey(); 
+				Map.Entry<Point, Cell> entry = cells.next();
+				Point p = entry.getKey(); 
+				g.setColor(cellColors[entry.getValue().state % cellColors.length]);
 				g.fillRect(paddx + tempaddx + (int) (p.x * zoom), 
 							paddy + tempaddy + (int) (p.y * zoom), 
 							size, size
 				g.setColor(Color.BLACK);
 				Iterator<Map.Entry<Point, Cell>> cells2 = selection.getLivingCells(view, ignoreLevel); 
 				while(cells2.hasNext()) {
-					Point p2 = cells2.next().getKey(); 
+					Map.Entry<Point, Cell> entry = cells2.next();
+					Point p2 = entry.getKey(); 
+					g.setColor(cellColors[entry.getValue().state % cellColors.length]);
 					g.fillRect(paddx + tempaddx + (int) ((p.x + p2.x) * zoom), 
 								paddy + tempaddy + (int) ((p.y + p2.y) * zoom), 
 								size, size