title: Langton's Ant Algorithm
body: <p>The problem of <a href="http://en.wikipedia.org/wiki/Langton%27s_ant" rel="nofollow">Langton's Ant</a> is simple. An ant walks a board with white and black squares. </p>
  
  <ul>
  <li>At a white square, turn 90° right, flip the color of the square, move forward one unit</li>
  <li>At a black square, turn 90° left, flip the color of the square, move forward one unit</li>
  </ul>
  
  <p>When the "game" starts the board is all white.</p>
  
  <p>I have implemented the simulation but has a really ugly solution for walkning and turning that I need help improving. Right now the walk is as follows:</p>
  
  <pre><code>if (dir == "NORTH") {
      // Walk up
  } else if (dir == "EAST") {
      // Walk right
  } else if (dir == "SOUTH") {
      // Walk down
  } else if (dir == "WEST") {
      // Walk left
  }           
  </code></pre>
  
  <p>And the function for changing direction:</p>
  
  <pre><code>private void turn(String leftOrRight){
      if(dir == "NORTH" &amp;&amp; lor == "RIGHT" ){
          dir = "EAST";
      } else if (dir == "NORTH" &amp;&amp; leftOrRight == "LEFT" ){
          dir = "WEST";
      } else if (dir == "EAST" &amp;&amp; leftOrRight == "RIGHT" ){
          dir = "SOUTH";
      } else if (dir == "EAST" &amp;&amp; leftOrRight == "LEFT" ){
          dir = "NORTH";
      } else if (dir == "SOUTH" &amp;&amp; leftOrRight == "RIGHT" ){
          dir = "WEST";
      } else if (dir == "SOUTH" &amp;&amp; leftOrRight == "LEFT" ){
          dir = "EAST";
      } else if (dir == "WEST" &amp;&amp; leftOrRight == "RIGHT" ){
          dir = "NORTH";
      } else if (dir == "WEST" &amp;&amp; leftOrRight == "LEFT" ){
          dir = "SOUTH";
      }
  }
  </code></pre>
  
  <p>I have thought about using ints instead of strings, but not quite sure how i should do it. Any help or hints is appreciated.</p>
  
  <hr>
  
  <p>EDIT: Now I have changed some. I Created an Ant class that has variables </p>
  
  <pre><code>int x, int y, Direction dir
  </code></pre>
  
  <p>And run it with the following method:</p>
  
  <pre><code>private void runAnt(Ant ant) {
      int x = ant.getX();
      int y = ant.getY();
  
      // Check rule 1
      if (matrix[x][y] == true) {
          matrix[x][y] = false;
          w.setDot(x, y, Color.WHITE);
          ant.setDirection(ant.getDirection().right());
  
      // Check rule 2
      } else if (matrix[x][y] == false) {
          matrix[x][y] = true;
          w.setDot(x, y, Color.BLACK);
          ant.setDirection(ant.getDirection().left());
      }
  
      // Moves one unit forward according to direction.
      if (ant.getDirection().equals(Direction.N)) {
          ant.setY((ant.getY() - 1 + wSize) % wSize);
  
      } else if (ant.getDirection().equals(Direction.E)) {
          ant.setX((ant.getX() + 1) % wSize);
  
      } else if (ant.getDirection().equals(Direction.S)) {
          ant.setY((ant.getY() + 1) % wSize);
  
      } else if (ant.getDirection().equals(Direction.W)) {
          ant.setX((ant.getX() - 1 + wSize) % wSize);
      }
  }
  </code></pre>
  
  <p>And my very first enum:</p>
  
  <pre><code>public static enum Direction {
      N, E, S, W;
      private static Direction[] vals = values();
  
      public Direction right() {
          return vals[(this.ordinal() + 1) % vals.length];
      }
  
      public Direction left() {
          return vals[(this.ordinal() + vals.length - 1) % vals.length];
      }
  }
  </code></pre>