0
0

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-15

ズンドコキヨシ物語

キヨシランドに自然と生まれてくるズン。

ズンだけで仲よく暮らすが、まれにキヨシランドの怒りによりドコにされてしまう。
ドコになったズンは夜になると周りのズンと戦い、生き延びたドコは新しいズンと関わることなく一人キヨシランドを生きていく...。

ドコになったズンが死ぬときキヨシランドの怒りが爆発する。

class KiyoshiLand
  def initialize
    @people = []
  end

  def <<(newcomer)
    @people.select(&:alive?).each {|person| person.hi(newcomer) and newcomer.hi(person) }
    @people << newcomer
  end

  def angry?
    rand(2).zero?
  end

  def each_person
    @people.each {|person| yield person }
  end

  def produce_zun
    Zun.new.tap {|zun| self << zun }
  end
end

class Zun
  def initialize
    @life = 1
    @neighbors = []
  end

  def <<(damage)
    @life -= damage
    dead unless @life > 0
  end

  def alive?
    true
  end

  def attack(person)
    person << damage
  end

  def bomb
    'キ・ヨ・シ!'
  end

  def damage
    1
  end

  def dead
    def self.alive?
      false
    end

    def self.spend_a_night
      :RIP
    end
  end

  def hi(other)
    @neighbors << other
  end

  def mutate_into_doko
    @life = 4

    def self.dead
      throw bomb
    end

    def self.name
      'ドコ'
    end

    def self.spend_a_night
      super

      @neighbors.each {|neighbor|
        attack(neighbor) and neighbor.attack(self) if neighbor.alive?
      }

      def self.hi(other)
        # |ω・`)チラ
      end
    end
  end

  def name
    'ズン'
  end

  def sleep
    <<-OFUTON
    (¦3[___]
    OFUTON
  end

  def spend_a_night
    # 初夜は( ゚д゚ )クワッ!!
    puts name

    def self.spend_a_night
      sleep
    end
  end
end

KiyoshiLand.new.instance_eval do
  loop do
    new_zun = produce_zun
    new_zun.mutate_into_doko if angry?
    each_person(&:spend_a_night)
  end
end

実行例

    _╰╰,   /バショ % ~/tmp
  _/o ŏァ  / タイチョー% ヤバイ
∈ミ;ノ,ノ   \コマンド% ruby hikiyoshi.rb
ズン
ドコ
ドコ
ズン
ズン
ドコ
ドコ
ドコ
ドコ
ズン
ズン
ドコ
ドコ
ズン
ズン
ドコ
ドコ
ドコ
ズン
ドコ
ズン
ドコ
ドコ
ドコ
ズン
ドコ
ドコ
ドコ
ドコ
ドコ
ズン
ドコ
ズン
ズン
ズン
ズン
ドコ
hikiyoshi.rb:69:in `throw': uncaught throw "キ・ヨ・シ!" (UncaughtThrowError)
	from hikiyoshi.rb:69:in `dead'
	from hikiyoshi.rb:32:in `<<'
	from hikiyoshi.rb:40:in `attack'
	from hikiyoshi.rb:80:in `block in spend_a_night'
	from hikiyoshi.rb:79:in `each'
	from hikiyoshi.rb:79:in `spend_a_night'
	from hikiyoshi.rb:16:in `block in each_person'
	from hikiyoshi.rb:16:in `each'
	from hikiyoshi.rb:16:in `each_person'
	from hikiyoshi.rb:113:in `block (2 levels) in <main>'
	from hikiyoshi.rb:110:in `loop'
	from hikiyoshi.rb:110:in `block in <main>'
	from hikiyoshi.rb:109:in `instance_eval'
	from hikiyoshi.rb:109:in `<main>'

その他のキヨシ

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?