Let's write β

プログラミング中にできたことか、思ったこととか

Rubyで一次元オートマトン

とりあえずルールとかみながらつくってみました。
次は生成関数から生成できるようにしたいとおもいます。

class World 

        def initialize(size)
                @map = Array.new(size, 0)
                @rule = Hash.new
        end

        def show
                @map.each do |cell|
                        if cell == 0
                                print " "
                        else
                                print "X"
                        end
                end
                puts
        end

        def cellNeighbors(idx)
                [
                        @map[idx-1],
                        @map[idx],
                        @map[(idx+1) % (@map.size)]
                ]
        end

        def getNextState(idx)
                neighbors = cellNeighbors(idx)
                nextState = @rule.fetch(neighbors,0)
                return nextState
        end

        def update
                newMap = []
                (0..@map.size-1).each do |idx|
                        newMap << (getNextState(idx))
                end
                @map = newMap
        end

        def genRandom()
                (0..@map.size-1).each do |idx|
                        @map[idx] = rand(2)
                end
        end

        def setCell(idx, n)
                @map[idx] = n
        end

        def setRule(rule, n)
                @rule[rule] = n
        end

        private :cellNeighbors
        private :getNextState
end

def rule30(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],0)
        world.setRule([0,0,1],1)
        world.setRule([0,1,0],1)
        world.setRule([0,1,1],1)
        world.setRule([1,0,0],1)
        world.setRule([1,0,1],0)
        world.setRule([1,1,0],0)
        world.setRule([1,1,1],0)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end

def rule90(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],0)
        world.setRule([0,0,1],1)
        world.setRule([0,1,0],0)
        world.setRule([0,1,1],1)
        world.setRule([1,0,0],1)
        world.setRule([1,0,1],0)
        world.setRule([1,1,0],1)
        world.setRule([1,1,1],0)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end

def rule137(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],1)
        world.setRule([0,0,1],0)
        world.setRule([0,1,0],0)
        world.setRule([0,1,1],1)
        world.setRule([1,0,0],0)
        world.setRule([1,0,1],0)
        world.setRule([1,1,0],0)
        world.setRule([1,1,1],1)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end

def rule150(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],0)
        world.setRule([0,0,1],1)
        world.setRule([0,1,0],1)
        world.setRule([0,1,1],0)
        world.setRule([1,0,0],1)
        world.setRule([1,0,1],0)
        world.setRule([1,1,0],0)
        world.setRule([1,1,1],1)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end

def rule62(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],0)
        world.setRule([0,0,1],1)
        world.setRule([0,1,0],1)
        world.setRule([0,1,1],1)
        world.setRule([1,0,0],1)
        world.setRule([1,0,1],1)
        world.setRule([1,1,0],0)
        world.setRule([1,1,1],0)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end

def rule73(size,n)
        world = World.new(size)
        world.setCell(size/2,1)
        world.setRule([0,0,0],1)
        world.setRule([0,0,1],0)
        world.setRule([0,1,0],0)
        world.setRule([0,1,1],1)
        world.setRule([1,0,0],0)
        world.setRule([1,0,1],0)
        world.setRule([1,1,0],1)
        world.setRule([1,1,1],0)
        world.show
        (1..n).each do |x|
                world.update
                world.show
        end
end