Jump to content

One-dimensional cellular automata: Difference between revisions

m
Fixed lang tags.
No edit summary
m (Fixed lang tags.)
Line 15:
 
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Cellular_Automata is
Line 57 ⟶ 56:
Step (Culture);
end loop;
end Cellular_Automata;</lang>
</lang>
The implementation defines Petri dish type with Boolean items identifying whether a place is occupied by a living cell. State transition is determined by a simple Boolean expression of three arguments. Sample output:
<pre>
Line 74 ⟶ 72:
=={{header|ALGOL 68}}==
===Using the low level packed arrays of BITS manipulation operators===
<lang algol68>INT stop generation = 9;
<pre>
INT stop generation = 9;
INT universe width = 20;
FORMAT alive or dead = $b("#","_")$;
Line 117 ⟶ 114:
FI;
universe := next universe
OD</lang>
</pre>
===Using high level BOOL arrays===
<lang algol68>INT stop generation = 9;
<pre>
INT stop generation = 9;
INT upb universe = 20;
FORMAT alive or dead = $b("#","_")$;
Line 155 ⟶ 150:
next universe[UPB universe] := couple(universe[UPB universe - 1: ]);
universe := next universe
OD</lang>
</pre>
Output:
<lang algol68>Generation 0: _###_##_#_#_#_#__#__
<pre>
Generation 0: _###_##_#_#_#_#__#__
Generation 1: _#_#####_#_#_#______
Generation 2: __##___##_#_#_______
Line 168 ⟶ 161:
Generation 7: __##_____#__________
Generation 8: __##________________
Generation 9: __##________________</lang>
</pre>
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=147 discussion]
Line 382 ⟶ 374:
}
return 0;
}</lang>
}
</lang>
 
The output is:
Line 501 ⟶ 492:
 
=={{header|Forth}}==
<lang forth>: init ( bits count -- )
0 do dup 1 and c, 2/ loop drop ;
20 constant size
create state $2556e size init 0 c,
: .state
cr size 0 do
state i + c@ if ." #" else space then
loop ;
: ctable create does> + c@ ;
ctable rules $68 8 init
: gen
state c@ ( window )
size 0 do
2* state i + 1+ c@ or 7 and
dup rules state i + c!
loop drop ;
: life1d ( n -- )
.state 1 do gen .state loop ;
 
20 constant size
10 life1d
create state $2556e size init 0 c,
 
: .state
cr size 0 do
state i + c@ if ." #" else space then
loop ;
 
: ctable create does> + c@ ;
ctable rules $68 8 init
 
: gen
state c@ ( window )
size 0 do
2* state i + 1+ c@ or 7 and
dup rules state i + c!
loop drop ;
 
: life1d ( n -- )
.state 1 do gen .state loop ;
 
10 life1d</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran> PROGRAM LIFE_1D
IMPLICIT NONE
LOGICAL :: cells(20) = (/ .FALSE., .TRUE., .TRUE., .TRUE., .FALSE., .TRUE., .TRUE., .FALSE., .TRUE., .FALSE., &
.TRUE., .FALSE., .TRUE., .FALSE., .TRUE., .FALSE., .FALSE., .TRUE., .FALSE., .FALSE. /)
INTEGER :: i
IMPLICIT NONE
DO i = 0, 9
 
WRITE(*, "(A,I0,A)", ADVANCE = "NO") "Generation ", i, ": "
LOGICAL :: cells(20) = (/ .FALSE., .TRUE., .TRUE., .TRUE., .FALSE., .TRUE., .TRUE., .FALSE., .TRUE., .FALSE., &
CALL Drawgen(cells)
.TRUE., .FALSE., .TRUE., .FALSE., .TRUE., .FALSE., .FALSE., .TRUE., .FALSE., .FALSE. /)
CALL Nextgen(cells)
INTEGER END:: DOi
DO i = 0, 9
CONTAINS
WRITE(*, "(A,I0,A)", ADVANCE = "NO") "Generation ", i, ": "
SUBROUTINE Nextgen CALL Drawgen(cells)
LOGICAL, INTENTCALL Nextgen(IN OUT) :: cells(:)
END DO
LOGICAL :: left, centre, right
 
INTEGER :: i
CONTAINS
 
left = .FALSE.
SUBROUTINE DO i = 1, SIZENextgen(cells)-1
LOGICAL, INTENT (IN OUT) centre =:: cells(i:)
LOGICAL :: left, centre, right = cells(i+1)
INTEGER :: i
IF (left .AND. right) THEN
cells(i) = .NOT. cells(i)
ELSE IF (.NOT. left .AND. .NOT. right) THEN
cells(i) = .FALSE.
END IF
left = centre
END DO
cells(SIZE(cells)) = left .AND. right
END SUBROUTINE Nextgen
SUBROUTINE Drawgen(cells)
LOGICAL, INTENT (IN OUT) :: cells(:)
INTEGER :: i
DO i = 1, SIZE(cells)
IF (cells(i)) THEN
WRITE(*, "(A)", ADVANCE = "NO") "#"
ELSE
WRITE(*, "(A)", ADVANCE = "NO") "_"
END IF
END DO
WRITE(*,*)
END SUBROUTINE Drawgen
left = .FALSE.
END PROGRAM LIFE_1D</lang>
DO i = 1, SIZE(cells)-1
centre = cells(i)
right = cells(i+1)
IF (left .AND. right) THEN
cells(i) = .NOT. cells(i)
ELSE IF (.NOT. left .AND. .NOT. right) THEN
cells(i) = .FALSE.
END IF
left = centre
END DO
cells(SIZE(cells)) = left .AND. right
END SUBROUTINE Nextgen
 
SUBROUTINE Drawgen(cells)
LOGICAL, INTENT (IN OUT) :: cells(:)
INTEGER :: i
DO i = 1, SIZE(cells)
IF (cells(i)) THEN
WRITE(*, "(A)", ADVANCE = "NO") "#"
ELSE
WRITE(*, "(A)", ADVANCE = "NO") "_"
END IF
END DO
WRITE(*,*)
END SUBROUTINE Drawgen
END PROGRAM LIFE_1D</lang>
Output
Generation 0: _###_##_#_#_#_#__#__
Line 592 ⟶ 583:
 
=={{header|Haskell}}==
<lang haskell>module Life1D where
<pre>
module Life1D where
 
import Data.List
Line 616 ⟶ 606:
g <- newStdGen
let oersoep = map ("_#"!!). take 36 $ randomRs(0,1) g
mapM_ print . lahmahgaan $ oersoep</lang>
</pre>
Some output:
<lang haskell>*Life1D> mapM_ print . lahmahgaan $ "_###_##_#_#_#_#__#__"
<pre>
*Life1D> mapM_ print . lahmahgaan $ "_###_##_#_#_#_#__#__"
"_###_##_#_#_#_#__#__"
"_#_#####_#_#_#______"
Line 638 ⟶ 626:
"________________________________#_#_"
"_________________________________#__"
"____________________________________"</lang>
</pre>
=={{header|J}}==
<lang j>life1d=: '_#'{~ (3(2=+/\) 0,],0:)^:a:</lang>
Example use:
<lang j> life1d ? 20 # 2
_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________</lang>
 
=={{header|Java}}==
Line 712 ⟶ 699:
=={{header|Logo}}==
{{works with|UCBLogo}}
<lang logo>make "cell_list [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]
<lang logo>
make "cell_list [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]
make "generations 9
 
Line 750 ⟶ 736:
end
 
CA_1D :cell_list :generations</lang>
</lang>
Sample Output:
<pre>
Line 767 ⟶ 752:
 
=={{header|M4}}==
<lang M4>divert(-1)
divert(-1)
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn(`$1[$2]')')
Line 803 ⟶ 787:
for(`j',1,10,
`show(x)`'evolve(`x',`y')`'swap(`x',x,`y')
')`'show(x)</lang>
</lang>
 
Output:
Line 823 ⟶ 806:
=={{header|Mathematica}}==
Built-in function:
<lang Mathematica>CellularAutomaton[{{0,0,_}->0,{0,1,0}->0,{0,1,1}->1,{1,0,0}->0,{1,0,1}->1,{1,1,0}->1,{1,1,1}->0},{{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1},0},12]
<lang Mathematica>
Print @@@ (% /. {1 -> "#", 0 -> "."});</lang>
CellularAutomaton[{{0,0,_}->0,{0,1,0}->0,{0,1,1}->1,{1,0,0}->0,{1,0,1}->1,{1,1,0}->1,{1,1,1}->0},{{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1},0},12]
Print @@@ (% /. {1 -> "#", 0 -> "."});
</lang>
gives back:
<lang Mathematica>###.##.#.#.#.#..#
###.##.#.#.#.#..#
#.#####.#.#.#....
.##...##.#.#.....
Line 841 ⟶ 821:
.##..............
.##..............
.##..............</lang>
</lang>
 
=={{header|Modula-3}}==
Line 907 ⟶ 886:
=={{header|Nial}}==
(life.nial)
<lang nial>% we need a way to write a values and pass the same back
wi is rest link [write, pass]
% calculate the neighbors by rotating the array left and right and joining them
neighbors is pack [pass, sum [-1 rotate, 1 rotate]]
% calculate the individual birth and death of a single array element
igen is fork [ = [ + [first, second], 3 first], 0 first, = [ + [first, second], 2 first], 1 first, 0 first ]
% apply that to the array
nextgen is each igen neighbors
% 42
life is fork [ > [sum pass, 0 first], life nextgen wi, pass ]</lang>
Using it
<lang nial>|loaddefs 'life.nial'
|I := [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]
|life I</lang>
 
=={{header|OCaml}}==
Line 952 ⟶ 931:
else print_char '#'
done;
print_newline()</lang>
</lang>
 
put the code above in a file named "life.ml", and then use it in the ocaml toplevel like this:
Line 1,011 ⟶ 989:
universe.replace('0', printdead).replace('1', printlive) )
universe = offendvalue + universe + offendvalue
universe = ''.join(neighbours2newstate[universe[i:i+3]] for i in range(cellcount))</lang>
</lang>
Sample output:
<pre>Generation 0: _###_##_#_#_#_#__#__
Line 1,043 ⟶ 1,020:
=={{header|R}}==
 
<lang R>set.seed(15797, kind="Mersenne-Twister")
<lang R>
set.seed(15797, kind="Mersenne-Twister")
 
maxgenerations = 10
Line 1,076 ⟶ 1,052:
universe <- cellularAutomata(universe, stayingAlive)
cat(format(i, width=3), deadOrAlive2string(universe), "\n")
}</lang>
}
</lang>
 
Sample output,
Line 1,201 ⟶ 1,176:
 
This implementation writes the calculated patterns into an edit buffer, where the results can viewed and saved into a file if required. The edit buffer also acts as storage during calculations.
<lang vedit>IT("Gen 0: ..###.##.#.#.#.#..#.....") // initial pattern
<pre>
IT("Gen 0: ..###.##.#.#.#.#..#.....") // initial pattern
#9 = Cur_Col
 
Line 1,219 ⟶ 1,193:
IT("Gen ") Num_Ins(#8, LEFT+NOCR) IT(": ")
Reg_Ins(20)
}</lang>
}
</pre>
 
Sample output:
<lang vedit>Gen 0: ..###.##.#.#.#.#..#.....
<pre>
Gen 0: ..###.##.#.#.#.#..#.....
Gen 1: ..#.#####.#.#.#.........
Gen 2: ...##...##.#.#..........
Line 1,233 ⟶ 1,205:
Gen 7: ...##.....#.............
Gen 8: ...##...................
Gen 9: ...##...................</lang>
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.