posted
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!
posted
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 |
posted
The twenty minutes included five of sitting and watching the ant do its thing. And grammar is important.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
// 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; }
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 |
posted
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.
posted
Well, there's only so many colours. 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 |
posted
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.
posted
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 |
posted
I hadn't heard of Langton's Ant before. What a cool little mathematical/algorithmic oddity! Spiffy!
Posts: 903 | Registered: May 2003
| IP: Logged |
posted
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 |
posted
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.