Author Topic: Langton's Ant in EGSL  (Read 4647 times)

Tomaaz

  • Guest
Langton's Ant in EGSL
« on: July 14, 2013, 12:18:50 PM »
You need my simple turtle library for this.

Code: [Select]
require "turtle"
openwindow (400, 400, 0, "Longton's Ant")
setbgcolour("white")
setcolour("black")
goxy (200, 200)
penup()
for steps = 1, 12000 do
x = pozycjax
y = pozycjay
if (screenred(x, y) == 255) then
turnright(90)
forward(1)
setcolour("black")
dot(x, y)
else
turnleft(90)
forward(1)
setcolour("white")
dot(x, y)
end
end
a = inkey(0)
« Last Edit: July 14, 2013, 12:35:41 PM by Tomaaz »

Rick3137

  • Guest
Re: Langton's Ant in EGSL
« Reply #1 on: July 14, 2013, 04:24:15 PM »
 That's real nice and short Tomaaz. It just proves what someone has told me in the past. My programming style is way too messy.
 What I would like to try, is to make a similar version for Browser Basic and compare the speeds. I have a feeling that Java Script might not be as fast as everyone thinks it is.
 

Tomaaz

  • Guest
Re: Langton's Ant in EGSL
« Reply #2 on: July 14, 2013, 08:02:31 PM »
I have a feeling that Java Script might not be as fast as everyone thinks it is.

It is fast. Check these examples.

Rick3137

  • Guest
Re: Langton's Ant in EGSL
« Reply #3 on: July 14, 2013, 11:45:18 PM »
 My slow computer took 9 seconds.

Here's my files. I hope I did this post thing right.

Tomaaz

  • Guest
Re: Langton's Ant in EGSL
« Reply #4 on: July 14, 2013, 11:52:04 PM »
Yes, that works fast, but instead of removing redraw() from the library you could use setredraw(). ;)

Code: [Select]
require "turtle"
openwindow (400, 400, 0, "Longton's Ant")
setbgcolour("white")
setcolour("black")
goxy (200, 200)
setredraw(0)
penup()
for steps = 1, 12000 do
x = pozycjax
y = pozycjay
if (screenred(x, y) == 255) then
turnright(90)
forward(1)
setcolour("black")
dot(x, y)
else
turnleft(90)
forward(1)
setcolour("white")
dot(x, y)
end
end
redraw()
a = inkey(0)

BTW: Did you check JavaScript version once again. That 11 sec. was very strange...

Rick3137

  • Guest
Re: Langton's Ant in EGSL
« Reply #5 on: July 15, 2013, 12:21:33 AM »
 Must be something slowing down my computer. I ran it again in Google Chrome and it took 12 seconds for me to see the image. About the same as Internet Explorer.

SteveOW

  • Guest
Re: Langton's Ant in EGSL
« Reply #6 on: July 15, 2013, 07:14:39 AM »
The javascript version took 0.103 seconds on my Windows7 i3 4Gb laptop.

SteveOW

  • Guest
Re: Langton's Ant in EGSL
« Reply #7 on: July 15, 2013, 09:56:40 AM »
Here is a scaled up version of Tomaaz's elegant Javascript.
Easier for my eyes to track at this scale.
Takes about 68 seconds (12000 steps).
Code: [Select]
<!DOCTYPE html>
<html>
<head>
<title>Langton's Ant v04</title>
<meta charset="utf-8">
<style type="text/css">
body {background-color: black}
div {text-align: center}
</style>
<script>
var scaler = 4 //... **** NB IF YOU CHANGE SCALER MUST ALSO ADJUST CANVAS  HEIGHT & WIDTH IN HTML AT END OF THIS PAGE
var x = 100 * scaler; //... initial x position
var y = 100 * scaler; //... initial y position
var Direction = 1;
var step = 12000;
function F_Setup()
{
Kanvas01 = document.getElementById("My_Kanvas");
Kontext01 = Kanvas01.getContext("2d");
Kontext01.fillStyle = "#ffffff"; //... we shall paint it white
Kontext01.fillRect(0, 0, (200 * scaler)-1, (200 * scaler)-1);
t = setTimeout("F_Looper()", 1); //...call F_Looper function
}
function F_Looper()
{
//Kanvas01 = document.getElementById("My_Kanvas");
//Kontext01 = Kanvas01.getContext("2d");
Imago = Kontext01.getImageData(x, y, 1, 1);  //...gets the current x,y pixel canvas image into an imagedata pixel object (buffer/array).
if (Imago.data[0] == 255) {          //... the Red component is 255 --> indicates that this pixel is white.
Kontext01.fillStyle = "#000000"; //...paint it black
Kontext01.fillRect(x, y, 1 * scaler, 1 * scaler);  //...this (x,y) is the rectangle to paint (=  a pixel is scaler ==1).
switch (Direction) {
case 1:
Direction = 4;
x=x+  scaler;
break;
case 2:
Direction = 1;
y=y+  scaler;
break;
case 3:
Direction = 2;
x=x-  scaler;
break;
case 4:
Direction = 3;
y=y-  scaler;
break;
}
}
else {
Kontext01.fillStyle = "#ffffff"; //... paint it white
Kontext01.fillRect(x, y, 1 *   scaler, 1 *   scaler);
switch(Direction) {
case 1:
Direction = 2;
x=x-  scaler;
break;
case 2:
Direction = 3;
y=y-  scaler;
break;
case 3:
Direction = 4;
x=x+  scaler;
break;
case 4:
Direction = 1;
y=y+  scaler;
break;
}
}
step--;
if (step) {
setTimeout("F_Looper()", 1); //...call self again
}
}
</script>
</head>
<!-- tell browser to run F_Setup script when loading the page -->
<body onload = "F_Setup()">
<!-- *** Define the canvas dimensions, e.g.  if scaler =4 then W=H=600, if scaler = 2 then W=H=300. -->
<div>
<canvas id="My_Kanvas" width="600" height="600"></canvas>
</div>
</body>
</html>



Rick3137

  • Guest
Re: Langton's Ant in EGSL
« Reply #8 on: July 15, 2013, 12:05:12 PM »
  My computer did it in 45 seconds. (Windows7 hp G62-355DX)