September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
67
Task/Langtons-ant/ALGOL-68/langtons-ant.alg
Normal file
67
Task/Langtons-ant/ALGOL-68/langtons-ant.alg
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
BEGIN
|
||||
# size of board for Langton's ant #
|
||||
INT max board = 100;
|
||||
[ 1 : max board, 1 : max board ]CHAR board;
|
||||
# start with the board all white #
|
||||
CHAR white = " ", black = "#";
|
||||
FOR r TO 1 UPB board DO FOR c TO 2 UPB board DO board[ r, c ] := white OD OD;
|
||||
# possible ant directions #
|
||||
INT head left = 0, head up = 1, head right = 2, head down = 3;
|
||||
# returns the new direction if we turn left from curr direction #
|
||||
OP LEFT = ( INT curr direction )INT:
|
||||
IF curr direction = head left THEN head down
|
||||
ELIF curr direction = head down THEN head right
|
||||
ELIF curr direction = head right THEN head up
|
||||
ELSE head left
|
||||
FI ; # LEFT #
|
||||
# returns the new direction if we turn right from curr direction #
|
||||
OP RIGHT = ( INT curr direction )INT:
|
||||
IF curr direction = head left THEN head up
|
||||
ELIF curr direction = head up THEN head right
|
||||
ELIF curr direction = head right THEN head down
|
||||
ELSE head left
|
||||
FI ; # RIGHT #
|
||||
# move the ant until it leaves the board #
|
||||
INT ant row := max board OVER 2;
|
||||
INT ant col := max board OVER 2;
|
||||
INT ant direction := head up;
|
||||
INT max row := 1;
|
||||
INT max col := 1;
|
||||
INT min row := max board;
|
||||
INT min col := max board;
|
||||
INT moves := 0;
|
||||
WHILE ant row >= 1 LWB board AND ant row <= 1 UPB board
|
||||
AND ant col >= 2 LWB board AND ant col <= 2 UPB board
|
||||
DO
|
||||
moves +:= 1;
|
||||
IF ant row > max row THEN max row := ant row FI;
|
||||
IF ant col > max col THEN max col := ant col FI;
|
||||
IF ant row < min row THEN min row := ant row FI;
|
||||
IF ant col < min col THEN min col := ant col FI;
|
||||
IF board[ ant row, ant col ] = white THEN
|
||||
# ant turns right on a white square #
|
||||
ant direction := RIGHT ant direction;
|
||||
board[ ant row, ant col ] := black
|
||||
ELSE
|
||||
# ant turns left on a black square #
|
||||
ant direction := LEFT ant direction;
|
||||
board[ ant row, ant col ] := white
|
||||
FI;
|
||||
# move the ant #
|
||||
IF ant direction = head up THEN ant row -:= 1
|
||||
ELIF ant direction = head down THEN ant row +:= 1
|
||||
ELIF ant direction = head left THEN ant col -:= 1
|
||||
ELSE # ant direction = head right # ant col +:= 1
|
||||
FI
|
||||
OD;
|
||||
# show resultant position #
|
||||
print( ( "After ", whole( moves, 0 ), " moves."
|
||||
, " Showing rows ", whole( min row,0 ), " to ", whole( max row, 0 )
|
||||
, " columns ", whole( min col,0 ), " to ", whole( max col, 0 )
|
||||
, newline
|
||||
)
|
||||
);
|
||||
FOR r FROM min row TO max row DO
|
||||
print( ( board[ r, min col : max col ], newline ) )
|
||||
OD
|
||||
END
|
||||
50
Task/Langtons-ant/Bc/langtons-ant.bc
Normal file
50
Task/Langtons-ant/Bc/langtons-ant.bc
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
define o() {
|
||||
auto i, j
|
||||
|
||||
"P1 "
|
||||
w
|
||||
h
|
||||
for (j = 0; j < h; j++) {
|
||||
for (i = 0; i < w; i++) {
|
||||
a[j * w + i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define l(w, h, x, y) {
|
||||
auto a[], d, i, x[], y[]
|
||||
|
||||
/* d represents one of the four possible directions:
|
||||
* 0
|
||||
* ⇑
|
||||
* 3⇐ ⇒1
|
||||
* ⇓
|
||||
* 2
|
||||
* The arrays x[] and y[] contain the changes to the x and y direction for
|
||||
* each value of d.
|
||||
*/
|
||||
x[1] = 1
|
||||
x[3] = -1
|
||||
y[0] = -1
|
||||
y[2] = 1
|
||||
|
||||
while (1) {
|
||||
i = y * w + x
|
||||
if (a[i] == 0) d += 1 /* turn right if white */
|
||||
if (a[i] == 1) d -= 1 /* turn left if black */
|
||||
if (d < 0) d = 3
|
||||
if (d > 3) d = 0
|
||||
x += x[d]
|
||||
y += y[d]
|
||||
a[i] = 1 - a[i] /* toggle cell colour */
|
||||
if (x < 0) break
|
||||
if (x == w) break
|
||||
if (y < 0) break
|
||||
if (y == h) break
|
||||
}
|
||||
|
||||
o()
|
||||
}
|
||||
|
||||
l(100, 100, 50, 50)
|
||||
quit
|
||||
76
Task/Langtons-ant/Gambas/langtons-ant.gambas
Normal file
76
Task/Langtons-ant/Gambas/langtons-ant.gambas
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
'This code will create a GUI Form to display the result
|
||||
|
||||
hGridView As GridView 'The display is on a GridView
|
||||
iCol As Integer = 38 'Column start position
|
||||
iRow As Integer = 30 'Row start position
|
||||
|
||||
Public Sub Form_show()
|
||||
|
||||
SetUpForm 'Run the SetUpForm routine
|
||||
Go 'Run the Go routine
|
||||
|
||||
End
|
||||
|
||||
Public Sub Go() 'This is what does the work
|
||||
Dim siDir As Short = 3 'Stores the Direction of the ant 0 = North, 1 = East, 2 = South ,3 = West
|
||||
Dim siCount As Short 'Counter
|
||||
|
||||
Repeat 'Repeat loop
|
||||
Inc siCount 'Increase siCount
|
||||
If hGridView[iRow, iCol].background = -1 Then 'If the Background of the cell is white then..(Right turn)
|
||||
hGridView[iRow, iCol].background = 0 'Make the Background black
|
||||
siDir = Direction(siDir, True) 'Get the direction to turn
|
||||
If siDir = 0 Then Dec iRow 'Decrease Row if facing North
|
||||
If siDir = 1 Then Inc iCol 'Increase Column if facing East
|
||||
If siDir = 2 Then Inc iRow 'Increase Row if facing South
|
||||
If siDir = 3 Then Dec iCol 'Decrease Column if facing West
|
||||
End If
|
||||
'Wait 'This will allow you to see the Grid being populated. Rem it out for an instant result
|
||||
If hGridView[iRow, iCol].background = 0 Then 'If the Background of the cell is black then.. Left Turn
|
||||
hGridView[iRow, iCol].background = -1 'Make the Background white
|
||||
siDir = Direction(siDir, False) 'Get the direction to turn
|
||||
If siDir = 0 Then Dec iRow 'Decrease Row if facing North
|
||||
If siDir = 1 Then Inc iCol 'Increase Column if facing East
|
||||
If siDir = 2 Then Inc iRow 'Increase Row if facing South
|
||||
If siDir = 3 Then Dec iCol 'Decrease Column if facing West
|
||||
End If
|
||||
Until siCount = 9660 'Loop 9660 times
|
||||
|
||||
End
|
||||
|
||||
Public Sub Direction(siDirection As Short, bWay As Boolean) As Byte 'To workout which way to go
|
||||
|
||||
If bWay Then 'If turning Right then
|
||||
Inc siDirection 'Increase siDirection e.g. 0 = North to 1 = East
|
||||
Else 'Else if turning Left
|
||||
Dec siDirection 'Decrease siDirection e.g. 2 = South to 1 = East
|
||||
End If
|
||||
|
||||
If siDirection < 0 Then siDirection = 3 'To address 0 - 1 = -1
|
||||
If siDirection > 3 Then siDirection = 0 'To address 3 + 1 = 4
|
||||
|
||||
Return siDirection 'Return the correct direction
|
||||
|
||||
End
|
||||
|
||||
Public Sub SetUpForm() 'Set up the Form and Create the Gridview
|
||||
|
||||
With Me 'Change the Properties of the Form
|
||||
.Height = 1012 'Set the Form Height
|
||||
.Width = 1012 'Set the Form Width
|
||||
.Arrangement = Arrange.Vertical 'Set the Form Arrangement
|
||||
.Padding = 5 'Set the Form Padding (Border)
|
||||
.title = "Langton's ant" 'Set the Form Title
|
||||
End With
|
||||
|
||||
hGridView = New GridView(Me) 'Create a GridView
|
||||
With hGridView 'Change the Properties of the GridView
|
||||
.Columns.count = 100 'Create 100 Columns
|
||||
.Rows.count = 100 'Create 100 Rows
|
||||
.Columns.Width = 10 'Set the Column Width
|
||||
.Rows.Height = 10 'Set the Column Height
|
||||
.expand = True 'Set the Gridview to Expand to fill the Form
|
||||
.background = -1 'Set the Gridview background to White
|
||||
End With
|
||||
|
||||
End
|
||||
71
Task/Langtons-ant/Scilab/langtons-ant.scilab
Normal file
71
Task/Langtons-ant/Scilab/langtons-ant.scilab
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
grid_size=100; //side length of the square grid
|
||||
ant_pos=round([grid_size/2 grid_size/2]); //ant's initial position at center of grid
|
||||
head_direction='W'; //ant's initial direction can be either
|
||||
//'N' north, 'S' south, 'E' east, or 'W' west
|
||||
|
||||
grid=~zeros(grid_size,grid_size) //blank grid
|
||||
col=[]; //cell color handler
|
||||
next_step=%T; //step flag
|
||||
i=0; //step counter
|
||||
|
||||
while next_step
|
||||
|
||||
col=grid(ant_pos(1),ant_pos(2)); //get cell color
|
||||
|
||||
if col then //if white cell
|
||||
grid(ant_pos(1),ant_pos(2))=~grid(ant_pos(1),ant_pos(2)); //switch color
|
||||
if head_direction=='N' then //if head to N
|
||||
head_direction='E'; //turn right to E
|
||||
ant_pos(2)=ant_pos(2)+1; //step forward
|
||||
|
||||
elseif head_direction=='E' then //if head to E
|
||||
head_direction='S'; //turn right to S
|
||||
ant_pos(1)=ant_pos(1)+1; //step forward
|
||||
|
||||
elseif head_direction=='S' then //if head to S
|
||||
head_direction='W'; //turn right to W
|
||||
ant_pos(2)=ant_pos(2)-1; //step forward
|
||||
|
||||
elseif head_direction=='W' then //if head to W
|
||||
head_direction='N'; //turn right to N
|
||||
ant_pos(1)=ant_pos(1)-1; //step forward
|
||||
end
|
||||
else //if black cell
|
||||
grid(ant_pos(1),ant_pos(2))=~grid(ant_pos(1),ant_pos(2)); //switch color
|
||||
if head_direction=='N' then //if head to N
|
||||
head_direction='W'; //turn left to E
|
||||
ant_pos(2)=ant_pos(2)-1; //step foward
|
||||
|
||||
elseif head_direction=='W' then //if head to W
|
||||
head_direction='S'; //turn left to S
|
||||
ant_pos(1)=ant_pos(1)+1; //step forward
|
||||
|
||||
elseif head_direction=='S' then //if head to S
|
||||
head_direction='E'; //turn left to E
|
||||
ant_pos(2)=ant_pos(2)+1; //step forward
|
||||
|
||||
elseif head_direction=='E' then //if head to E
|
||||
head_direction='N'; //turn left to N
|
||||
ant_pos(1)=ant_pos(1)-1; //step forward
|
||||
end
|
||||
end
|
||||
|
||||
i=i+1;
|
||||
|
||||
if ant_pos(1)<1 | ant_pos(1)>100 | ant_pos(2)<0 | ant_pos(2)>100 then //check ant's position
|
||||
disp("Out of bounds after "+string(i)+" steps");
|
||||
next_step=~next_step; //break loop if out of bounds
|
||||
end
|
||||
end
|
||||
|
||||
ascii_grid=string(zeros(grid)); //create grid of chars to display
|
||||
//on the console
|
||||
for a=1:length(grid)
|
||||
if grid(a) then
|
||||
ascii_grid(a)=" "; //blank space if cell is white
|
||||
else
|
||||
ascii_grid(a)="#"; //# if cell is black
|
||||
end
|
||||
end
|
||||
|
||||
disp(ascii_grid);
|
||||
15
Task/Langtons-ant/Zkl/langtons-ant.zkl
Normal file
15
Task/Langtons-ant/Zkl/langtons-ant.zkl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
white:=0xff|ff|ff; black:=0;
|
||||
w:=h:=100; bitmap:=PPM(w,h,white);
|
||||
x:=w/2; y:=h/2; dir:=0; // start in middle facing east
|
||||
do{
|
||||
if(bitmap[x,y]){ dir-=1; bitmap[x,y]=black; } // white-->black, turn left
|
||||
else { dir+=1; bitmap[x,y]=white; } // black-->white, turn right
|
||||
switch(dir.bitAnd(3)){ // dir is always <0
|
||||
case(0){ x+=1; } // east
|
||||
case(1){ y-=1; } // south
|
||||
case(2){ x-=1; } // west
|
||||
case(3){ y+=1; } // north
|
||||
}
|
||||
}while((0<=x<w) and (0<=y<h));
|
||||
|
||||
bitmap.write(File("foo.ppm","wb"));
|
||||
Loading…
Add table
Add a link
Reference in a new issue