Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
38
Task/Langtons-ant/ANSI-BASIC/langtons-ant.basic
Normal file
38
Task/Langtons-ant/ANSI-BASIC/langtons-ant.basic
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
100 REM Langton!'s ant
|
||||
110 REM ** Simulation
|
||||
120 DIM Board(0 TO 99, 0 TO 99)
|
||||
130 LET W = UBOUND(Board, 1) + 1
|
||||
140 LET H = UBOUND(Board, 2) + 1
|
||||
150 REM Start in the middle, facing East
|
||||
160 LET X = INT(W / 2)
|
||||
170 LET Y = INT(H / 2)
|
||||
180 LET Dir = 0
|
||||
190 DO
|
||||
200 IF Board(X, Y) <> 0 THEN
|
||||
210 LET Dir = Dir - 1
|
||||
220 LET Board(X, Y) = 0
|
||||
230 ELSE
|
||||
240 LET Dir = Dir + 1
|
||||
250 LET Board(X, Y) = 1
|
||||
260 END IF
|
||||
270 SELECT CASE MOD(Dir, 4)
|
||||
280 CASE 0
|
||||
290 LET X = X + 1
|
||||
300 CASE 1
|
||||
310 LET Y = Y + 1
|
||||
320 CASE 2
|
||||
330 LET X = X - 1
|
||||
340 CASE 3
|
||||
350 LET Y = Y - 1
|
||||
360 END SELECT
|
||||
370 LOOP WHILE X >= 0 AND X < W AND Y >= 0 AND Y < H
|
||||
380 REM ** Visualization
|
||||
390 SET WINDOW 0, W, 0, H
|
||||
400 SET AREA COLOR 0
|
||||
410 SET POINT COLOR 1
|
||||
420 FOR X = 0 TO W - 1
|
||||
430 FOR Y = 0 TO H - 1
|
||||
440 IF Board(X, Y) <> 0 THEN GRAPH POINTS: X, Y
|
||||
450 NEXT Y
|
||||
460 NEXT X
|
||||
470 END
|
||||
56
Task/Langtons-ant/Ada/langtons-ant.adb
Normal file
56
Task/Langtons-ant/Ada/langtons-ant.adb
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Langtons_Ant is
|
||||
|
||||
Size: constant Positive := 100; -- change this to extend the playground
|
||||
|
||||
subtype Step is Integer range -1 .. +1;
|
||||
|
||||
procedure Right(N, W: in out Step) is
|
||||
Tmp: Step := W;
|
||||
begin
|
||||
W := - N;
|
||||
N := Tmp;
|
||||
end Right;
|
||||
|
||||
procedure Left(N, W: in out Step) is
|
||||
begin
|
||||
for I in 1 .. 3 loop
|
||||
Right(N, W);
|
||||
end loop;
|
||||
end Left;
|
||||
|
||||
Color_Character: array(Boolean) of Character :=
|
||||
(False => ' ', True => '#');
|
||||
|
||||
Is_Black: array (1 .. Size, 1 .. Size) of Boolean :=
|
||||
(others => (others => False)); -- initially, the world is white;
|
||||
|
||||
Ant_X, Ant_Y: Natural := Size/2; -- Position of Ant;
|
||||
Ant_North: Step := 1; Ant_West: Step := 0; -- initially, Ant looks northward
|
||||
|
||||
Iteration: Positive := 1;
|
||||
|
||||
begin
|
||||
loop -- iterate the loop until an exception is raised
|
||||
if Is_Black(Ant_X, Ant_Y) then
|
||||
Left(Ant_North, Ant_West);
|
||||
else
|
||||
Right(Ant_North, Ant_West);
|
||||
end if;
|
||||
Is_Black(Ant_X, Ant_Y) := not Is_Black(Ant_X, Ant_Y);
|
||||
Ant_X := Ant_X - Ant_North; -- this may raise an exception
|
||||
Ant_Y := Ant_Y - Ant_West; -- this may raise an exception
|
||||
Iteration := Iteration + 1;
|
||||
end loop;
|
||||
|
||||
exception
|
||||
when Constraint_Error => -- Ant has left its playground ... now output
|
||||
for X in 1 .. Size loop
|
||||
for Y in 1 .. Size loop
|
||||
Ada.Text_IO.Put(Color_Character(Is_Black(X, Y)));
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line("# Iteration:" & Integer'Image(Iteration));
|
||||
end Langtons_Ant;
|
||||
76
Task/Langtons-ant/AutoIt/langtons-ant.au3
Normal file
76
Task/Langtons-ant/AutoIt/langtons-ant.au3
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
Global $iCountMax = 100000
|
||||
Global $aFields[100][100][2]
|
||||
Global $iDelayStep = 10 ; stop between steps in msec
|
||||
|
||||
Global $aDirection[4][4] = [ _ ; [ direction 0-3 ][ left change x, y, right change x, y ]
|
||||
[-1, 0, +1, 0], _ ; == direction 0
|
||||
[ 0, -1, 0, +1], _ ; == direction 1
|
||||
[+1, 0, -1, 0], _ ; == direction 2
|
||||
[ 0, +1, 0, -1]] ; == direction 3
|
||||
|
||||
Global $hGui = GUICreate("Langton's ant", 100*8, 100*8)
|
||||
GUISetBkColor(0xFFFFFF)
|
||||
|
||||
For $i = 0 To 99
|
||||
For $j = 0 To 99
|
||||
$aFields[$i][$j][0] = GUICtrlCreateLabel('', $j*8, $i*8)
|
||||
GUICtrlSetColor(-1, 0xFF0000)
|
||||
$aFields[$i][$j][1] = 0
|
||||
Next
|
||||
Next
|
||||
|
||||
GUISetState()
|
||||
|
||||
GUICtrlSetData($aFields[49][49][0], '#')
|
||||
|
||||
Do
|
||||
Sleep($iDelayStep)
|
||||
Until Not _SetAnt()
|
||||
|
||||
Do
|
||||
Until GUIGetMsg() = -3
|
||||
|
||||
|
||||
Func _SetAnt()
|
||||
Local Static $iRowLast = 49, $iColLast = 49, $iCount = 0
|
||||
Local Static $aCol[2] = [0xFFFFFF,0x000000], $iDirection = 0
|
||||
Local $iRow, $iCol, $fRight = False
|
||||
If $iCount = $iCountMax Then Return 0
|
||||
|
||||
; == get current color
|
||||
Local $iLastColor = $aFields[$iRowLast][$iColLast][1]
|
||||
|
||||
; == go to left/right
|
||||
If $iLastColor = 0 Then $fRight = True
|
||||
|
||||
; == set the ant to the next field
|
||||
Local $indexX = 0, $indexY = 1
|
||||
If $fRight Then
|
||||
$indexX = 2
|
||||
$indexY = 3
|
||||
EndIf
|
||||
$iRow = $iRowLast + ($aDirection[$iDirection][$indexX])
|
||||
$iCol = $iColLast + ($aDirection[$iDirection][$indexY])
|
||||
If $iRow < 0 Or $iRow > 99 Or $iCol < 0 Or $iCol > 99 Then Return 0
|
||||
GUICtrlSetData($aFields[$iRowLast][$iColLast][0], '')
|
||||
GUICtrlSetData($aFields[$iRow][$iCol][0], '#')
|
||||
|
||||
; == direction for next step
|
||||
If $fRight Then
|
||||
$iDirection += 1
|
||||
If $iDirection = 4 Then $iDirection = 0
|
||||
Else
|
||||
$iDirection -= 1
|
||||
If $iDirection = -1 Then $iDirection = 3
|
||||
EndIf
|
||||
|
||||
; == change the color of the current field
|
||||
GUICtrlSetBkColor($aFields[$iRowLast][$iColLast][0], $aCol[(Not $iLastColor)*1])
|
||||
$aFields[$iRowLast][$iColLast][1] = (Not $iLastColor)*1
|
||||
|
||||
$iRowLast = $iRow
|
||||
$iColLast = $iCol
|
||||
$iCount += 1
|
||||
WinSetTitle($hGui, '', "Langton's ant [ step: " & StringFormat('%06d', $iCount) & " ]")
|
||||
Return 1
|
||||
EndFunc ;==>_SetAnt
|
||||
91
Task/Langtons-ant/COBOL/langtons-ant.cob
Normal file
91
Task/Langtons-ant/COBOL/langtons-ant.cob
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. langtons-ant.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
78 Grid-Size VALUE 100.
|
||||
01 grid-area.
|
||||
03 grid-x OCCURS Grid-Size TIMES.
|
||||
05 grid-y OCCURS Grid-Size TIMES.
|
||||
07 cell-colour PIC X VALUE "W".
|
||||
88 black VALUE "B".
|
||||
88 white VALUE "W".
|
||||
|
||||
01 ant-x PIC 999.
|
||||
01 ant-y PIC 999.
|
||||
|
||||
01 ant-direction PIC 9.
|
||||
88 upward VALUE 0.
|
||||
88 rightward VALUE 1.
|
||||
88 downward VALUE 2.
|
||||
88 leftward VALUE 3.
|
||||
|
||||
78 Pause-Time-Ns VALUE 10000000.
|
||||
|
||||
01 display-y PIC 999.
|
||||
|
||||
78 Black-Background VALUE 0.
|
||||
78 White-Background VALUE 7.
|
||||
|
||||
01 i PIC 999.
|
||||
01 j PIC 999.
|
||||
|
||||
01 pause PIC X.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
main-line.
|
||||
DIVIDE Grid-Size BY 2 GIVING ant-x, ant-y
|
||||
|
||||
PERFORM display-initial-grid
|
||||
PERFORM UNTIL (ant-x = Grid-Size OR 0)
|
||||
OR (ant-y = Grid-Size OR 0)
|
||||
PERFORM step-simulation
|
||||
CALL "CBL_OC_NANOSLEEP" USING Pause-Time-Ns
|
||||
END-PERFORM
|
||||
|
||||
DISPLAY "Press enter to quit." AT LINE 1 COLUMN 1
|
||||
ACCEPT pause
|
||||
|
||||
GOBACK
|
||||
.
|
||||
step-simulation.
|
||||
IF black (ant-x, ant-y)
|
||||
SET white (ant-x, ant-y) TO TRUE
|
||||
PERFORM display-ant-cell
|
||||
COMPUTE ant-direction =
|
||||
FUNCTION MOD(ant-direction + 1, 4)
|
||||
ELSE
|
||||
SET black (ant-x, ant-y) TO TRUE
|
||||
PERFORM display-ant-cell
|
||||
COMPUTE ant-direction =
|
||||
FUNCTION MOD(ant-direction - 1, 4)
|
||||
END-IF
|
||||
|
||||
EVALUATE TRUE
|
||||
WHEN upward
|
||||
ADD 1 TO ant-y
|
||||
WHEN rightward
|
||||
ADD 1 TO ant-x
|
||||
WHEN downward
|
||||
SUBTRACT 1 FROM ant-y
|
||||
WHEN leftward
|
||||
SUBTRACT 1 FROM ant-x
|
||||
END-EVALUATE
|
||||
.
|
||||
display-ant-cell.
|
||||
SUBTRACT ant-y FROM Grid-Size GIVING display-y
|
||||
IF black (ant-x, ant-y)
|
||||
DISPLAY SPACE AT LINE display-y COLUMN ant-x
|
||||
WITH BACKGROUND-COLOR Black-Background
|
||||
ELSE
|
||||
DISPLAY SPACE AT LINE display-y COLUMN ant-x
|
||||
WITH BACKGROUND-COLOR White-Background
|
||||
END-IF
|
||||
.
|
||||
display-initial-grid.
|
||||
PERFORM VARYING i FROM 1 BY 1 UNTIL i > Grid-Size
|
||||
AFTER j FROM 1 BY 1 UNTIL j > Grid-Size
|
||||
DISPLAY SPACE AT LINE i COLUMN j
|
||||
WITH BACKGROUND-COLOR White-Background
|
||||
END-PERFORM
|
||||
.
|
||||
68
Task/Langtons-ant/Chapel/langtons-ant.chpl
Normal file
68
Task/Langtons-ant/Chapel/langtons-ant.chpl
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
config const gridHeight: int = 100;
|
||||
config const gridWidth: int = 100;
|
||||
|
||||
class PBMWriter {
|
||||
var imgDomain: domain(2);
|
||||
var imgData: [imgDomain] int;
|
||||
|
||||
proc PBMWriter( height: int, width: int ){
|
||||
imgDomain = { 1..#height, 1..#width };
|
||||
}
|
||||
|
||||
proc this( i : int, j : int) ref : int{
|
||||
return this.imgData[ i, j ];
|
||||
}
|
||||
|
||||
proc writeImage( fileName: string ){
|
||||
var file = open(fileName, iomode.cw);
|
||||
var writingChannel = file.writer();
|
||||
writingChannel.write("P1\n", imgDomain.dim(1).size, " " ,imgDomain.dim(2).size,"\n");
|
||||
|
||||
for px in imgData {
|
||||
writingChannel.write( px, " " );
|
||||
}
|
||||
|
||||
writingChannel.write( "\n" );
|
||||
writingChannel.flush();
|
||||
writingChannel.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum Color { white, black };
|
||||
|
||||
inline proc nextDirection( position: 2*int, turnLeft: bool ): 2*int {
|
||||
return ( (if turnLeft then 1 else -1 ) * position[2], (if turnLeft then -1 else 1 ) * position[1] );
|
||||
}
|
||||
|
||||
proc <( left: 2*int, right: 2*int ){
|
||||
return left[1] < right[1] && left[2] < right[2];
|
||||
}
|
||||
|
||||
proc <=( left: 2*int, right: 2*int ){
|
||||
return left[1] <= right[1] && left[2] <= right[2];
|
||||
}
|
||||
|
||||
proc main{
|
||||
const gridDomain: domain(2) = {1..#gridHeight, 1..#gridWidth};
|
||||
var grid: [gridDomain] Color;
|
||||
|
||||
var antPos = ( gridHeight / 2, gridWidth / 2 );
|
||||
var antDir = (1,0); // start up;
|
||||
|
||||
while (0,0) < antPos && antPos <= (gridHeight, gridWidth ) {
|
||||
var currColor = grid[ antPos ];
|
||||
grid[antPos] = if currColor == Color.white then Color.black else Color.white ;
|
||||
|
||||
antDir = nextDirection( antDir, currColor == Color.black );
|
||||
antPos = antPos + antDir;
|
||||
}
|
||||
|
||||
var image = new PBMWriter( height = gridHeight, width = gridWidth );
|
||||
|
||||
for (i, j) in gridDomain {
|
||||
image[i,j] = if grid[gridHeight-j+1,gridHeight-i+1] == Color.black then 0 else 1;
|
||||
}
|
||||
|
||||
image.writeImage( "output.png" );
|
||||
}
|
||||
29
Task/Langtons-ant/Crystal/langtons-ant.cr
Normal file
29
Task/Langtons-ant/Crystal/langtons-ant.cr
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
SIZE = 100
|
||||
|
||||
WHITE = false
|
||||
BLACK = true
|
||||
|
||||
plane = Array.new(SIZE) { Array.new(SIZE, WHITE) }
|
||||
|
||||
dirs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
|
||||
|
||||
row = col = 50
|
||||
dir = 0
|
||||
|
||||
loop do
|
||||
break unless 0 <= row < SIZE && 0 <= col < SIZE
|
||||
color = plane[row][col]
|
||||
dir = (dir + (color == BLACK ? -1 : 1)) % dirs.size
|
||||
dx, dy = dirs[dir]
|
||||
plane[row][col] = !color
|
||||
row += dy; col += dx
|
||||
end
|
||||
|
||||
# crop
|
||||
start_row = plane.index! {|row| row.any? BLACK }
|
||||
end_row = plane.rindex! {|row| row.any? BLACK }
|
||||
|
||||
# print
|
||||
plane[start_row..end_row].each do |row|
|
||||
puts row.map {|color| color == BLACK ? '#' : ' ' }.join
|
||||
end
|
||||
65
Task/Langtons-ant/Euphoria/langtons-ant-1.eu
Normal file
65
Task/Langtons-ant/Euphoria/langtons-ant-1.eu
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
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..'
|
||||
2
Task/Langtons-ant/Euphoria/langtons-ant-2.eu
Normal file
2
Task/Langtons-ant/Euphoria/langtons-ant-2.eu
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
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
|
||||
16
Task/Langtons-ant/GW-BASIC/langtons-ant.basic
Normal file
16
Task/Langtons-ant/GW-BASIC/langtons-ant.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
100 REM Langton's ant
|
||||
110 W% = 100: H% = 100
|
||||
120 REM Set 320x200 graphic video mode
|
||||
130 SCREEN 1
|
||||
140 X% = W% \ 2: Y% = H% \ 2
|
||||
150 REM Start in the middle, facing East
|
||||
160 DIR% = 0
|
||||
170 IF POINT(X%, Y%) <> 0 THEN DIR% = DIR% - 1: PSET (X%, Y%), 0 ELSE DIR% = DIR% + 1: PSET (X%, Y%), 3
|
||||
180 ON (DIR% MOD 4) + 1 GOSUB 220, 230, 240, 250
|
||||
190 IF X% >= 0 AND X% < W% AND Y% >= 0 AND Y% < H% THEN GOTO 170
|
||||
200 LOCATE 13
|
||||
210 END
|
||||
220 X% = X% + 1: RETURN
|
||||
230 Y% = Y% + 1: RETURN
|
||||
240 X% = X% - 1: RETURN
|
||||
250 Y% = Y% - 1: RETURN
|
||||
16
Task/Langtons-ant/Nascom-BASIC/langtons-ant.basic
Normal file
16
Task/Langtons-ant/Nascom-BASIC/langtons-ant.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
100 REM Langton's ant
|
||||
110 CLS:W=95:H=44
|
||||
120 X=INT(W/2):Y=INT(H/2)
|
||||
130 REM Start in the middle, facing East
|
||||
140 D=0
|
||||
150 IF POINT(X,Y)=0 THEN 170
|
||||
160 D=D-1:RESET(X,Y):GOTO 180
|
||||
170 D=D+1:SET(X,Y)
|
||||
180 ON D-INT(D/4)*4+1 GOSUB 220,230,240,250
|
||||
190 IF X>=0 AND X<W AND Y>=0 AND Y<H THEN 150
|
||||
200 SCREEN 1,13
|
||||
210 END
|
||||
220 X=X+1:RETURN
|
||||
230 Y=Y+1:RETURN
|
||||
240 X=X-1:RETURN
|
||||
250 Y=Y-1:RETURN
|
||||
34
Task/Langtons-ant/Pluto/langtons-ant.pluto
Normal file
34
Task/Langtons-ant/Pluto/langtons-ant.pluto
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
require "table2"
|
||||
|
||||
local width = 75
|
||||
local height = 52
|
||||
local max_steps = 12000
|
||||
|
||||
enum begin white = 0, black end
|
||||
enum begin up = 0, right, down, left end
|
||||
|
||||
local x = width // 2
|
||||
local y = height // 2
|
||||
local m = table.create(height)
|
||||
for i = 1, height do m[i] = table.rep(width, 0) end
|
||||
local dir = up
|
||||
local i = 0
|
||||
while i < max_steps and 0 <= x < width and 0 <= y < height do
|
||||
local turn = (m[y + 1][x + 1] == black)
|
||||
dir = (dir + (turn ? 1 : -1)) & 3
|
||||
m[y + 1][x + 1] = (m[y + 1][x + 1] == black) ? white : black
|
||||
if dir == up then
|
||||
y -= 1
|
||||
elseif dir == right then
|
||||
x -= 1
|
||||
elseif dir == down then
|
||||
y += 1
|
||||
else
|
||||
x += 1
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
for j = 1, height do
|
||||
for k = 1, width do io.write((m[j][k] == white) ? "." : "#") end
|
||||
print()
|
||||
end
|
||||
27
Task/Langtons-ant/PowerShell/langtons-ant.ps1
Normal file
27
Task/Langtons-ant/PowerShell/langtons-ant.ps1
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
$Size = 100
|
||||
|
||||
$G = @()
|
||||
1..$Size | ForEach { $G += ,( @( 1 ) * $Size ) }
|
||||
|
||||
$x = $y = $Size / 2
|
||||
|
||||
# Direction of next move
|
||||
$Dx = 1
|
||||
$Dy = 0
|
||||
|
||||
# While we are still on the grid...
|
||||
While ( $x -ge 0 -and $y -ge 0 -and $x -lt $Size -and $y -lt $Size )
|
||||
{
|
||||
# Change direction
|
||||
$Dx, $Dy = ( $Dy * $G[$x][$y] ), -( $Dx * $G[$x][$y] )
|
||||
|
||||
# Change state of current square
|
||||
$G[$x][$y] = -$G[$x][$y]
|
||||
|
||||
# Move forward
|
||||
$x += $Dx
|
||||
$y += $Dy
|
||||
}
|
||||
|
||||
# Convert to strings for output
|
||||
ForEach ( $Row in $G ) { ( $Row | ForEach { ( ' ', '', '#')[$_+1] } ) -join '' }
|
||||
30
Task/Langtons-ant/QuickBASIC/langtons-ant.basic
Normal file
30
Task/Langtons-ant/QuickBASIC/langtons-ant.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
' Langton's ant
|
||||
|
||||
CONST W = 100, H = 100
|
||||
DIM X AS INTEGER, Y AS INTEGER, Dir AS INTEGER
|
||||
' Set 320x200 graphic video mode
|
||||
SCREEN 1
|
||||
' Start in the middle, facing East
|
||||
X = W \ 2
|
||||
Y = H \ 2
|
||||
Dir = 0
|
||||
DO
|
||||
IF POINT(X, Y) <> 0 THEN
|
||||
Dir = Dir - 1
|
||||
PSET (X, Y), 0
|
||||
ELSE
|
||||
Dir = Dir + 1
|
||||
PSET (X, Y), 3
|
||||
END IF
|
||||
SELECT CASE (Dir MOD 4)
|
||||
CASE 0
|
||||
X = X + 1
|
||||
CASE 1
|
||||
Y = Y + 1
|
||||
CASE 2
|
||||
X = X - 1
|
||||
CASE 3
|
||||
Y = Y - 1
|
||||
END SELECT
|
||||
LOOP WHILE X >= 0 AND X < W AND Y >= 0 AND Y < H
|
||||
END
|
||||
46
Task/Langtons-ant/V-(Vlang)/langtons-ant.v
Normal file
46
Task/Langtons-ant/V-(Vlang)/langtons-ant.v
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
const size = 100
|
||||
const white = false
|
||||
const black = true
|
||||
|
||||
fn main() {
|
||||
dirs := [[0, -1], [1, 0], [0, 1], [-1, 0]]
|
||||
mut plane := [][]bool{len: size, init: []bool{len: size, init: white}}
|
||||
mut row, mut col, mut start_row, mut end_row, mut dir := 50, 50, 0, 0, 0
|
||||
for {
|
||||
if !(row >= 0 && row < size && col >= 0 && col < size) { break }
|
||||
color := plane[row][col]
|
||||
if color == black { dir = (dir + dirs.len - 1) % dirs.len }
|
||||
else { dir = (dir + 1) % dirs.len }
|
||||
dx := dirs[dir][0]
|
||||
dy := dirs[dir][1]
|
||||
plane[row][col] = !color
|
||||
row += dy
|
||||
col += dx
|
||||
}
|
||||
// find first and last rows containing black
|
||||
start_row = 0
|
||||
end_row = size - 1
|
||||
// find start_row
|
||||
for i in 0 .. size {
|
||||
if plane[i].any(it == black) {
|
||||
start_row = i
|
||||
break
|
||||
}
|
||||
}
|
||||
// find end_row
|
||||
for i := size - 1; i >= 0; i-- {
|
||||
if plane[i].any(it == black) {
|
||||
end_row = i
|
||||
break
|
||||
}
|
||||
}
|
||||
// print cropped plane
|
||||
for i in start_row .. end_row + 1 {
|
||||
mut line := []string{}
|
||||
for color in plane[i] {
|
||||
if color == black { line << "#" }
|
||||
else { line << " " }
|
||||
}
|
||||
println(line.join(''))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue