FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » I am teh 1337est!

   
Author Topic: I am teh 1337est!
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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.

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
What language? And it took 20 minutes? Do you have a slow machine? [Razz]

The LAnt has always fascinated me.

Dagonee

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Java. And twenty minutes is not a long time for making a program that includes even such basic graphics.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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 ]

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
[Grumble] [Wink] The twenty minutes included five of sitting and watching the ant do its thing. And grammar is important. [Big Grin]
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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 ]

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Nope, sorry. Not so much the pattern, anyway, as the way it developed. Why not borrow my code and see for yourself?
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
Um, don't use 17 as an offset - it's a factor of 255 so just loops around.
Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
So for state N, the color would be

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

Dagonee

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
Dragon
Member
Member # 3670

 - posted      Profile for Dragon   Email Dragon         Edit/Delete Post 
[Angst]
Posts: 3420 | Registered: Jun 2002  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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 ]

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
signal
Member
Member # 6828

 - posted      Profile for signal   Email signal         Edit/Delete Post 
AHHH! You've found my kryptonite! Take it away... the code... it hurts!
Posts: 298 | Registered: Sep 2004  |  IP: Logged | Report this post to a Moderator
jebus202
Member
Member # 2524

 - posted      Profile for jebus202   Email jebus202         Edit/Delete Post 
OMG IM A GEEK
Posts: 3564 | Registered: Sep 2001  |  IP: Logged | Report this post to a Moderator
WheatPuppet
Member
Member # 5142

 - posted      Profile for WheatPuppet   Email WheatPuppet         Edit/Delete Post 
I hadn't heard of Langton's Ant before. What a cool little mathematical/algorithmic oddity! Spiffy!
Posts: 903 | Registered: May 2003  |  IP: Logged | Report this post to a Moderator
Annie
Member
Member # 295

 - posted      Profile for Annie   Email Annie         Edit/Delete Post 
I see "1337" a lot. What does it mean?
Posts: 8504 | Registered: Aug 1999  |  IP: Logged | Report this post to a Moderator
rivka
Member
Member # 4859

 - posted      Profile for rivka   Email rivka         Edit/Delete Post 
LEET, or "elite."
Posts: 32919 | Registered: Mar 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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 ]

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
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

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

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