This is topic I am teh 1337est! in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=029864

Posted by King of Men (Member # 6684) on :
 
I just hacked together a Langton's Ant implementation in twenty minutes. Easily extensible, too, you just need to add an extra clause to the switch, and extend the direction-change array. Yay for me!

Fascinating stuff, Langton.
 
Posted by Dagonee (Member # 5818) on :
 
What language? And it took 20 minutes? Do you have a slow machine? [Razz]

The LAnt has always fascinated me.

Dagonee
 
Posted by King of Men (Member # 6684) on :
 
Java. And twenty minutes is not a long time for making a program that includes even such basic graphics.
 
Posted by Dagonee (Member # 5818) on :
 
True. At least in Java.

In Delphi, I bet it wouldn't take 10 minutes. I love Delphi.

And I'm only giving you a hard time because you grammar-checked my law exam essay I posted.

Dagonee

[ December 11, 2004, 07:54 PM: Message edited by: Dagonee ]
 
Posted by King of Men (Member # 6684) on :
 
[Grumble] [Wink] The twenty minutes included five of sitting and watching the ant do its thing. And grammar is important. [Big Grin]
 
Posted by Dagonee (Member # 5818) on :
 
The next thing to do is to generalize it to N states and allow assignment of the rule for each state (left, right, back, forward).

I've never written that, but it doesn't seem too much more difficult.

Dagonee
 
Posted by King of Men (Member # 6684) on :
 
As I said, I deliberately designed it for that to be easy. Here's the code :
code:
import java.awt.*;

class Ant extends Frame {

private int[][] theMap;
private int mapEdge;

// 0 is north, 1 is east, 2 is south, 3 is west.
private int antDirection;

// Current position.
private int antXpos;
private int antYpos;

// Direction-change table. -1 indicates left, 1 indicates right.
private static final int[] dirChange = {-1, 1};

public static void main (String[] args) {
Ant n = new Ant();
}

public Ant() {

// Setup graphics logistics
super();
setUndecorated(true);
mapEdge = 700;
setSize(mapEdge, mapEdge);
setResizable(false);

// Initialise ant
antXpos = 50;
antYpos = 50;
antDirection = 0;
theMap = new int[mapEdge][mapEdge];

show();


// Main decision loop, run until ant goes off the edge
while ((antXpos >= 0) && (antXpos < mapEdge) &&
(antYpos >= 0) && (antYpos < mapEdge)) {

// Change directions
antDirection += dirChange[theMap[antXpos][antYpos]];
if (antDirection > 3) antDirection = 0;
else if (antDirection < 0) antDirection = 3;

// Change colour and show new grid
theMap[antXpos][antYpos]++;
if (theMap[antXpos][antYpos] >= dirChange.length) theMap[antXpos][antYpos] = 0;
paint(getGraphics());

switch (antDirection) {
case 0 : antYpos--; // North
break;
case 1 : antXpos++; // East
break;
case 2 : antYpos++; //South
break;
case 3 : antXpos--; // West
break;
// Default should never happen, treat it as north.
default :
antYpos--;
antDirection = 0;
break;
}

try {
Thread.sleep(25);
}
catch (InterruptedException e) {}
}


System.exit(0);
}

public void paint(Graphics g) {
switch (theMap[antXpos][antYpos]) {
case 0 :
g.setColor(Color.white);
break;
case 1 :
g.setColor(Color.black);
break;
default :
g.setColor(Color.green);
break;
}
g.fillRect(2*antXpos, 2*antYpos, 2, 2);
}
}

So you can easily see that all that's required is to add the extra states to the switch statement in the paint method, and to extend the dirChange table.
 
Posted by Dagonee (Member # 5818) on :
 
Ah, but that's not generalized. Have it accept an input from the user and proceed without recompiling. You'd need to change the switch statement to a loop of some kind.

Dagonee
 
Posted by King of Men (Member # 6684) on :
 
Well, there's only so many colours. [Big Grin] So there's an upper limit on the number of states anyway. I suppose I could make an array of colours, and just select the current value for the square; more elegant.

EDIT : Man, that turns out to be truly, incredibly cool with four colours and (-1, 1, 1, -1) for the directions.

[ December 11, 2004, 08:37 PM: Message edited by: King of Men ]
 
Posted by Dagonee (Member # 5818) on :
 
With RGB 24 bit color, you could make a formula to vary each color byte differently. you could even use offsets based on prime numbers so they don't repeat/match up often.

R would go 1, 4, 7, 10, 13; G would go 255, 242, 229, 216; B would go 128, 145, 152, etc. Let each wrap around as needed.

Got a screenshot of the 4 color pattern?

Dagonee
 
Posted by King of Men (Member # 6684) on :
 
Nope, sorry. Not so much the pattern, anyway, as the way it developed. Why not borrow my code and see for yourself?
 
Posted by Dagonee (Member # 5818) on :
 
Um, don't use 17 as an offset - it's a factor of 255 so just loops around.
 
Posted by Dagonee (Member # 5818) on :
 
So for state N, the color would be

RGB((N * 7) % 255, (N * 13) % 255, (N * 17) % 255)

Dagonee
 
Posted by Dragon (Member # 3670) on :
 
[Angst]
 
Posted by Dagonee (Member # 5818) on :
 
You could add offsets to the N * Prime terms as well: (N * 7 + 128) % 255.

This would give a little more variability to the mix, especially for lower max Ns.

[ December 11, 2004, 09:14 PM: Message edited by: Dagonee ]
 
Posted by signal (Member # 6828) on :
 
AHHH! You've found my kryptonite! Take it away... the code... it hurts!
 
Posted by jebus202 (Member # 2524) on :
 
OMG IM A GEEK
 
Posted by WheatPuppet (Member # 5142) on :
 
I hadn't heard of Langton's Ant before. What a cool little mathematical/algorithmic oddity! Spiffy!
 
Posted by Annie (Member # 295) on :
 
I see "1337" a lot. What does it mean?
 
Posted by rivka (Member # 4859) on :
 
LEET, or "elite."
 
Posted by King of Men (Member # 6684) on :
 
Dag, that gives you the colours, but what about the left-right sequence? For more than ten or so states, it becomes a bit of a chore to enter it on the command line.

I've been experimenting a bit with this. If the direction changes are of the form LLRRLL, ie pairs of directions, the patterns are supposed to be symmetrical, and this is true. But there also seems to be a difference between the case where the first change matches the last, and the case where it matches the second,

LRRL
vs
LLRR

The first produces rectangular patterns, with the edges in fairly pure colours. The second gives a much more rounded pattern, with a strong mix of all the colours, reminiscent of a Mandelbrot fractal. Fascinating.

Also, if there are many more lefts than rights, the pattern will grow only very slowly - the extreme case, of course, is having only lefts, in which case, the Ant sticks to four squares, changing their colours in a boring, cyclic pattern. Symmetry, in this case, leads to strong expansion.

[ December 11, 2004, 10:55 PM: Message edited by: King of Men ]
 
Posted by Dagonee (Member # 5818) on :
 
For the left right sequence (I'd throw in reverse/forward as well), I'd simply allow as many entered as the user desires, with the pattern repeated as needed. If there are 23 states, and the user enters LLRRLL, then it would be LLRRLLLLRRLLLLRRLLLLRRL. Not great, but flexible enough to work.

I hope you post the source code when your done tweaking. This is great fun.

Dagonee
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2