Commit 44dd4083 authored by Yngve Sekse Kristiansen's avatar Yngve Sekse Kristiansen
Browse files

Add missing files

parent 09edba93
package inf101.v19.cell;
public class Ant {
protected int x, y;
protected Direction dir;
public Ant(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
public void turnLeft() {
// TODO update the x, y and dir fields according to a left turn
// of the ant.
public void turnRight() {
// TODO update the x, y and dir fields according to a right turn
// of the ant.
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
public Ant copy() {
return new Ant(x, y, dir);
...@@ -6,13 +6,32 @@ import java.util.Random; ...@@ -6,13 +6,32 @@ import java.util.Random;
* *
* The State of a cell * The State of a cell
*/ */
public enum CellState { public class CellState {
public static final CellState ALIVE = new CellState(2),
DYING = new CellState(1),
DEAD = new CellState(0);
public static final CellState ANT = new CellState(-1);
private static final CellState[] values = new CellState[]{ALIVE,DYING,DEAD};
protected int data;
public CellState(int data) { = data;
public int getValue() {
return data;
* Returns one of the 'standard' values.
* @param rand
* @return
public static CellState random(Random rand){ public static CellState random(Random rand){
return CellState.values()[rand.nextInt(3)]; return values[rand.nextInt(3)];
} }
} }
package inf101.v19.cell;
public enum Direction {
package inf101.v19.cell;
import java.awt.Color;
import inf101.v19.datastructures.IGrid;
import inf101.v19.datastructures.MyGrid;
public class LangtonsAnt implements ICellAutomaton {
* The maximum length of any valid rule. Should not exceed 256,
* otherwise not all colors will be distinct.
public static final int MAX_RULE_LENGTH = 32;
* Stores the rule, in the following sense: Upon reading a state whose value is i,
* char[i] indicates whether to turn left ('L') or right ('R')
protected char[] rule;
protected IGrid currentGeneration;
protected Ant ant;
* The state the ant saw upon moving to the field it is placed on
* currently. Determines the next move (using the given rule).
protected CellState seenState;
public LangtonsAnt(int width, int height, String rule) {
this.currentGeneration = new MyGrid(width, height, null);
this.rule = rule.toCharArray();
* Checks the rule for validity.
* A string is not a rule its length exceeds the maximum length ({@link #MAX_RULE_LENGTH})
* or if it contains characters other than 'L' and 'R'.
* @param rule
private void checkRule(String rule) {
// TODO check if the length of the rule is within MAX_RULE_LENGTH and
// throw an exception otherwise.
// TODO check if the string rule only consists of the characters
// 'L' and 'R' and throw an exception otherwise.
public Color getColorInCurrentGeneration(int x, int y) {
// This method returns different shades of the same color.
// The scaling depends on the length of the given rule.
CellState state = currentGeneration.get(x, y);
if (state == CellState.ANT) return Color.yellow;
int val = (int) (state.getValue()*256/rule.length);
return new Color(255, 255-val, 255-val);
public void initializeGeneration() {
// TODO Set all fields to be in state 0.
// TODO Initialize the ant and place it in the middle of the grid.
// TODO Initialize the seenState field (to state 0).
public void stepAutomaton() {
// Retrieve the color of the cell the ant is on using the seenState field.
int color = seenState.getValue();
// Initialize the next position of the any by copying the ant field.
// Make sure that all operations concerning the *next* position of the
// ant in the grid are performed on this object.
Ant nextAnt = ant.copy();
if (rule[color] == 'L') {
// TODO turn left
if (rule[color] == 'R') {
// TODO turn right
// TODO Check whether the new coordinates are within the grid, otherwise
// reset them in some way of your choosing. (Note that this will greatly
// influence the patterns being drawn by the ant.)
// TODO Update the state of the filed the ant is leaving.
// TODO Update the seenState field, i.e. the state the ant is reading in
// the field it is moving to.
// TODO Move the ant to the new position in the grid.
// TODO Update the ant field variable.
public int getHeight() {
return this.currentGeneration.getHeight();
public int getWidth() {
return this.currentGeneration.getWidth();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment