Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
76
Task/Langtons-ant/AutoIt/langtons-ant.autoit
Normal file
76
Task/Langtons-ant/AutoIt/langtons-ant.autoit
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
|
||||
|
|
@ -21,7 +21,7 @@ my $dir = int rand @dirs;
|
|||
my $move;
|
||||
for ($move = 0; $x >= 0 && $x < $size && $y >= 0 && $y < $size; $move++) {
|
||||
# toggle cell's value (white->black or black->white)
|
||||
if ($plane[$x][$y] = 1 - $plane[$x][$y]) {
|
||||
if ($plane[$x][$y] = 1 - ($plane[$x][$y] ||= 0)) {
|
||||
# if it's now true (black), then it was white, so turn right
|
||||
$dir = ($dir - 1) % @dirs;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* we use the following conventions:
|
||||
* directions 0: up, 1: right, 2: down: 3: left
|
||||
* directions 0: up, 1: left, 2: down: 3: right
|
||||
*
|
||||
* pixel white: true, black: false
|
||||
*
|
||||
|
|
@ -36,7 +36,7 @@ int count=0;
|
|||
void draw() {
|
||||
for(int i=0;i<STEP;i++) {
|
||||
count++;
|
||||
boolean pix=get(x,y)!=-1;
|
||||
boolean pix=get(x,y)!=-1; //white =-1
|
||||
setBool(x,y,pix);
|
||||
|
||||
turn(pix);
|
||||
|
|
@ -59,13 +59,13 @@ void move() {
|
|||
y--;
|
||||
break;
|
||||
case 1:
|
||||
x++;
|
||||
x--;
|
||||
break;
|
||||
case 2:
|
||||
y++;
|
||||
break;
|
||||
case 3:
|
||||
x--;
|
||||
x++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
20
Task/Langtons-ant/R/langtons-ant.r
Normal file
20
Task/Langtons-ant/R/langtons-ant.r
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
langton.ant = function(n = 100) {
|
||||
map = matrix(data = 0, nrow = n, ncol = n)
|
||||
p = floor(c(n/2, n/2))
|
||||
d = sample(1:4, 1)
|
||||
i = 1
|
||||
while(p[1] > 0 & p[1] <= n & p[2] > 0 & p[2] <= n) {
|
||||
if(map[p[1], p[2]] == 1) {
|
||||
map[p[1], p[2]] = 0
|
||||
p = p + switch(d, c(0, 1), c(-1, 0), c(0, -1), c(1, 0))
|
||||
d = ifelse(d == 4, 1, d + 1)
|
||||
} else {
|
||||
map[p[1], p[2]] = 1
|
||||
p = p + switch(d, c(0, -1), c(1, 0), c(0, 1), c(-1, 0))
|
||||
d = ifelse(d == 1, 4, d - 1)
|
||||
}
|
||||
}
|
||||
return(map)
|
||||
}
|
||||
|
||||
image(langton.ant(), xaxt = "n", yaxt = "n", bty = "n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue