4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ズンドコキヨシオートマトン with Ruby

Last updated at Posted at 2016-03-13

ズンドコキヨシマシン

ズンズンズンズンドコ状態になると「キ・ヨ・シ♡」でクラッシュする

zundoko.png

require 'gviz'

Graph do
  route :Silent       => [:Zun, :Doko]
  route :Zun          => [:ZunZun, :Doko]
  route :Doko         => [:Zun, :Doko]
  route :ZunZun       => [:ZunZunZun, :Doko]
  route :ZunZunZun    => [:ZunZunZunZun, :Doko]
  route :ZunZunZunZun => [:ZunZunZunZun, :ZunZunZunZunDoko]

  save(:zundoko, :png)
end

実装

class Silent
  def zun
    Zun.new
  end

  def doko
    Doko.new
  end
end

class Zun
  def zun
    ZunZun.new
  end

  def doko
    Doko.new
  end
end

class Doko
  def zun
    Zun.new
  end

  def doko
    Doko.new
  end
end

class ZunZun
  def zun
    ZunZunZun.new
  end

  def doko
    Doko.new
  end
end

class ZunZunZun
  def zun
    ZunZunZunZun.new
  end

  def doko
    Doko.new
  end
end

class ZunZunZunZun
  def zun
    ZunZunZunZun.new
  end

  def doko
    ZunZunZunZunDoko.new
  end
end

class ZunZunZunZunDoko
  def initialize
    raise 'キ・ヨ・シ♡'
  end
end

class ZunOp
  def call(state)
    puts 'ズン'
    state.send(:zun)
  end
end

class DokoOp
  def call(state)
    puts 'ドコ'
    state.send(:doko)
  end
end

class RandomZundokoProgram
  include Enumerable

  def each
    loop { yield [ZunOp, DokoOp].sample.new }
  end
end

class ZunDokoMachine
  def initialize
    @state = Silent.new
  end

  def run(program)
    program.each do |op|
      @state = op.call(@state)
    end
  end
end

machine = ZunDokoMachine.new
program = RandomZundokoProgram.new.lazy
machine.run(program)

実行例

    _╰╰,   /バショ % ~/tmp
  _/o ŏァ  / タイチョー% ヤバイ
∈ミ;ノ,ノ   \コマンド% ruby zundoko.rb
ドコ
ズン
ドコ
ズン
ドコ
ドコ
ドコ
ドコ
ドコ
ドコ
ドコ
ズン
ズン
ドコ
ドコ
ズン
ズン
ズン
ドコ
ズン
ズン
ズン
ズン
ズン
ズン
ズン
ズン
ドコ
zundoko.rb:63:in `initialize': キ・ヨ・シ♡ (RuntimeError)
	from zundoko.rb:57:in `new'
	from zundoko.rb:57:in `doko'
	from zundoko.rb:77:in `call'
	from zundoko.rb:96:in `block in run'
	from zundoko.rb:85:in `block in each'
	from zundoko.rb:85:in `loop'
	from zundoko.rb:85:in `each'
	from zundoko.rb:95:in `each'
	from zundoko.rb:95:in `run'
	from zundoko.rb:103:in `<main>'
    _╰╰,   /バショ % ~/tmp
  _/oஇ ァ  / タイチョー% ヤバイ
∈ミ;ノ,ノ   \コマンド%

その他のキヨシ

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?