Anonymous user
Wireworld/Ruby: Difference between revisions
no edit summary
m (add Shoes version) |
No edit summary |
||
Line 1:
<lang ruby>class WireWorld
EMPTY = ' '
HEAD = 'H'
TAIL = 't'
CONDUCTOR = '.'
NEIGHBOURS = [-1,0,1].product([-1,0,1]) - [0,0]
def initialize(string)
@grid = string.each_line.collect do |line|
line.chomp
case char
when EMPTY, HEAD, TAIL, CONDUCTOR
else
EMPTY
end
end
end
@width = @grid.collect{|row| row.length}.max + 1
@height = @grid.length
pad_grid
@original_grid = Marshal.restore(Marshal.dump @grid) # this is a deep copy
end
# initialize from a file
def self.open(filename)
self.new(File.read(filename))
end
def reset
@grid = @original_grid
end
# ensure all rows are the same length by padding short rows with empty cells
def pad_grid
@grid << []
@grid.each do |row|
end
end
# the "to_string" method
def to_s
@grid.
end
# transition all cells simultaneously
def transition
Line 59 ⟶ 53:
end
end
# how to transition a single cell
def transition_cell(current, x, y)
Line 66 ⟶ 60:
when HEAD then TAIL
when TAIL then CONDUCTOR
else neighbours_with_state(
end
end
# given a position in the grid, find the neighbour cells with a particular state
def neighbours_with_state(
NEIGHBOURS.count {|dx, dy| @grid[y+dy][x+dx] ==
end
# run a simulation up to a limit of transitions, or until a recurring
# pattern is found
# This will print text to the console
def run(iterations = 25)
seen =
for count
puts "Generation : #{count}"
puts
if seen[@grid]
puts "I've seen this grid before... after #{count} iterations"
end
seen[@grid] = count
transition
end
puts "ran through #{iterations} iterations"
end
end</lang>
Test (Text version)
<lang ruby># this is the "2 Clock generators and an XOR gate" example from the wikipedia page
text = <<WORLD
......tH
. ......
...Ht... .
....
. .....
....
tH...... .
. ......
...Ht...
WORLD
ww = WireWorld.new text
ww.run
puts 'bye'</lang>
{{out}}
<pre style="height:64ex;overflow:scroll">
Generation : 0
......tH
. ......
...Ht... .
....
. .....
....
tH...... .
. ......
...Ht...
Generation : 1
.......t
. H.....
..Ht.... .
....
. .....
....
.tH..... .
. ......
..Ht....
Generation : 2
........
. tH....
.Ht....H .
....
. .....
....
..tH.... .
. ......
.Ht.....
Generation : 3
........
. .tH...
Ht....Ht .
....
. .....
....
...tH... .
. ......
Ht......
Generation : 4
........
H ..tH..
t....Ht. .
....
. .....
....
....tH.. .
H ......
t.......
Generation : 5
H.......
t ...tH.
....Ht.. .
....
. .....
....
H....tH. .
t ......
........
Generation : 6
tH......
. ....tH
...Ht... .
....
. .....
....
tH....tH .
. ......
........
Generation : 7
.tH.....
. .....t
..Ht.... H
....
. .....
....
.tH....t .
. H.....
........
Generation : 8
..tH....
. ......
.Ht..... t
HHH.
. .....
....
..tH.... .
. tH....
.......H
Generation : 9
...tH...
. ......
Ht...... .
tttH
H H....
....
...tH... .
. .tH...
......Ht
Generation : 10
....tH..
H ......
t....... .
...t
t tH...
HHHH
....tH.. .
. ..tH..
.....Ht.
Generation : 11
H....tH.
t ......
........ .
....
. .tH..
tttt
.....tH. .
. ...tH.
....Ht..
Generation : 12
tH....tH
. ......
........ .
....
. ..tH.
....
......tH .
. ....tH
...Ht...
Generation : 13
.tH....t
. H.....
........ .
....
. ...tH
....
.......t H
. H....t
..Ht....
Generation : 14
..tH....
. tH....
.......H .
....
. ....t
HHH.
........ t
. tH....
.Ht....H
Generation : 15
...tH...
. .tH...
......Ht .
....
H H....
tttH
........ .
. .tH...
Ht....Ht
Generation : 16
....tH..
. ..tH..
.....Ht. .
HHHH
t tH...
...t
........ .
H ..tH..
t....Ht.
Generation : 17
.....tH.
. ...tH.
....Ht.. .
tttt
. .tH..
....
H....... .
t ...tH.
....Ht..
Generation : 18
......tH
. ....tH
...Ht... .
....
. ..tH.
....
tH...... .
. ....tH
...Ht...
Generation : 19
.......t
. H....t
..Ht.... H
....
. ...tH
....
.tH..... H
. .....t
..Ht....
Generation : 20
........
. tH....
.Ht....H t
HHH.
. ....t
HHH.
..tH.... t
. ......
.Ht.....
Generation : 21
........
. .tH...
Ht....Ht .
tttH
. H....
tttH
...tH... .
. ......
Ht......
Generation : 22
........
H ..tH..
t....Ht. .
...t
. t....
...t
....tH.. .
H ......
t.......
Generation : 23
H.......
t ...tH.
....Ht.. .
....
. .....
....
H....tH. .
t ......
........
I've seen this grid before... after 23 iterations
bye
</pre>
The GUI version
{{libheader|Ruby/Tk}}
The GUI is somewhat "halfway", in that it animates a text widget so it's not "real" graphics.
<lang ruby>require 'tk'
class WireWorld
def run_tk
@tk_root = TkRoot.new(title: "WireWorld")
@tk_text = TkText.new(width: @width, height: @height, font: 'courier')
@tk_text.insert('end', self.to_s).state('disabled')
@tk_after_interval = 150
faster_cmd = proc {@tk_after_interval = [25, @tk_after_interval-25].max}
Line 130 ⟶ 399:
@tk_root.destroy
end
controls = TkFrame.new
[ TkButton.new(controls, text:
TkButton.new(controls, text:
TkButton.new(controls, text:
TkButton.new(controls, text:
].each {|btn| btn.pack(expand:
@tk_text.pack(expand:
controls.pack(
@tk_after_id = @tk_root.after(500) {animate}
Tk.mainloop
end
def animate
transition
Line 155 ⟶ 424:
end
ww = WireWorld.new text
ww.run_tk</lang>
{{libheader|Shoes}}
<lang ruby>
Shoes.app(title: "Wireworld") do
world = para('', family: 'monospace')
animate(4) do
end
end</lang>
|