View of Rule 30 Re: Auto Cellular automaton
"valery_vi" <[email protected]>
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I've rewritten it to have a more quick and short script (aca.au3):
; ACA, © Valery Ivanov, 20 June, 2007
; ACA is Auto Cellular automaton example
; About CA see here:
; http://en.wikipedia.org/wiki/Cellular_automata
#include <GUIConstants.au3>
Global $hDC, $GDI, $hWnd
; Default Rule 30
; Rule 30 cellular automaton
Global $RuleNum = 30
Global $CA_Rule[8] = [0,0,0,1,1,1,1,0]
; Rule 30 cellular automaton
; You can set Rule 110
;$RuleNum = 110
Set_CA_Rule($RuleNum)
$hWnd = GUICreate("Auto Cellular automaton - © Valery Ivanov, 20
June, 2007", 400, 200)
GUISetState(@SW_SHOW)
$GDI = DllOpen("gdi32.dll")
$hDC = DllCall ("user32.dll", "int", "GetDC", "hwnd", $hWnd)
$hDC = $hDC[0]
$String = GetFirstLine(400)
$s = StringSplit($String,'')
$i = 0
for $i = 1 To 200
for $j = 1 To $s[0]
if $s[$j] = '1' then
PutPoint($hDC,$j,$i,0xAA0000)
endif
Next
$String = GetNextLine($String)
$s = StringSplit($String,'')
Next
; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE
;===============
func PutPoint($hDC, $x, $y, $color)
DllCall ($GDI, "long", "SetPixel", "long", $hDC, "long", $x, "long",
$y, "long", $color)
endfunc
;===============
;set Rule
; There are 256 rules [0 - 255]
; The known CA rule are 30 and 110!
func Set_CA_Rule($RuleNum)
for $i = 1 to 8
$CA_Rule[8-$i] = 0
if mod($RuleNum,2) then $CA_Rule[8-$i] = 1
$RuleNum = BitShift($RuleNum,1)
next
endfunc
;===============
;get first CA line
func GetFirstLine($Dim)
local $String = ''
for $i = 1 to $Dim
if $i = $Dim/2 then
$String &= '1'
else
$String &= '0'
endif
next
return $String
endfunc
;===============
;get next CA line
func GetNextLine($String)
local $S, $N, $l, $c, $r
$S = StringSplit($String,'')
$String = ''
$N = $S[0]
for $i = 1 to $N
$l = $S[$N]
if $i <> 1 then $l = $S[$i-1]
$c = $S[$i]
$r = $S[1]
if $i <> $n then $r = $S[$i+1]
$String &= $CA_Rule[7-($r+2*$c+4*$l)]
next
return $String
endfunc
About CA read the following:
http://en.wikipedia.org/wiki/Cellular_automata
Enjoy,
Valery