Langton's ant: Difference between revisions

→‎{{header|D}}: Added Euphoria language header, code, output, image for this task in text mode and an SDL guide. 3000% more euphoric ant.
(→‎{{header|REXX}}: changed output character (for the black color) to conform to others. ----)
(→‎{{header|D}}: Added Euphoria language header, code, output, image for this task in text mode and an SDL guide. 3000% more euphoric ant.)
Line 777:
...........................................................................
...........................................................................</pre>
 
=={{header|Euphoria}}==
{{works with|Euphoria|4.0.3, 4.0.0 RC1 and later}}
<lang euphoria>include std\console.e
include std\graphics.e
 
sequence grid = repeat(repeat(1,100),100) --fill 100 by 100 grid with white (1)
sequence antData = {48, 53, 360} --ant x coordinate, y coordinate, facing angle
integer iterations = 0
 
--while ant isn't out of bounds of the 100 by 100 area..
while antData[1] > 0 and antData[1] < 100 and antData[2] > 0 and antData[2] < 100 do
switch grid[antData[1]][antData[2]] do
case 1 then--cell is already white
grid[antData[1]][antData[2]] = 0 --cell turns black, ant turns right
antData[3] += 90
break
case 0 then--cell is already black
grid[antData[1]][antData[2]] = 1 --cell turns white, ant turns left
antData[3] -= 90
break
end switch
--wrap ant directions if > 360 or < 90 (by 90)
switch antData[3] do
case 450 then
antData[3] = 90
break
case 0 then
antData[3] = 360
break
end switch
--move ant based on its new facing, one square
--first north, then south, east, west
switch antData[3] do
case 360 then
antData[2] -= 1
break
case 180 then
antData[2] += 1
break
case 90 then
antData[1] += 1
break
case 270 then
antData[1] -= 1
break
end switch
iterations += 1
end while
 
wrap(0) --don't wrap text output, the grid wouldnt display as a square
 
for y=1 to 100 do
printf(1,"\n")
for x=1 to 100 do
switch grid[x][y] do--each grid block , based on color
case 0 then
printf(1,".")
break
case 1 then
printf(1,"#")
break
end switch
end for
end for
 
printf(1,"\n%d Iterations\n",iterations)
any_key()--wait for keypress, put default message 'press any key..'</lang>[[File:LangtonsAnt_Euphoria_SDL.png|right|thumb|SDL output. Click to enlarge]]
Code needed to run SDL example with Mark Akita's SDL_gfx_Test1.exw (as template) included with his SDL_gfx package from rapideuphoria.com's archive -
In initialization section :<lang euphoria>
sequence grid = repeat(repeat(1,100),100) --fill 100 by 100 grid with white (1)
sequence antData = {48, 53, 360} --x coordinate, y coordinate, facing angle</lang>
In main() , after keystate=SDL_GetKeyState(NULL) , you can adapt the program above to draw the ant's step each frame.
Use dummy=pixelColor(surface,x+20,y+12,#000000FF) (for example) to replace the text output.
Just before the close of the while loop, use dummy=pixelColor(surface,antData[1]+20,antData[2]+12,#FF0000FF) for the ant
and SDL_UpdateRect(surface,0,0,0,0) to display the graphic.
 
=={{header|Fantom}}==
Anonymous user