Sha256: e6c4f027767645ed8600c295cf9ebcd82c701b906152feb40c85efbb16e4eee9

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

#!/usr/bin/env ruby

class StreetFighter2
  def initialize(code)
    @tokens = code.scan(/(波動拳|昇竜拳|竜巻旋風脚|ヨガフレイム|タイガーアハ|ソニックブーム|↓R↑LYBXA|↑X↓BLYRA)/).flatten
    @jumps = analyze_jumps(@tokens)
  end

  def run
    array = []
    index = 0
    now = 0

    while index < @tokens.size
      case @tokens[index]
        when "竜巻旋風脚"
          array[now] ||= 0
          array[now] += 1
        when "ヨガフレイム"
          array[now] ||= 0
          array[now] -= 1
        when "波動拳"
          now += 1
        when "昇竜拳"
          now -= 1
        when "タイガーアハ"
          n = (array[now] || 0)
          print n.chr
        when "ソニックブーム"
          array[now] = $stdin.getc
        when "↓R↑LYBXA"
          index = @jumps[index] if array[now] == 0
        when "↑X↓BLYRA"
          index = @jumps[index] if array[now] != 0
      end
      index += 1
    end
  end

  private

  def analyze_jumps(tokens)
    stack = []
    jumps = {}
    start_word = "↓R↑LYBXA"
    end_word = "↑X↓BLYRA"
    tokens.each_with_index do |v,i|
      if v == start_word
        stack.push(i)
      elsif v == end_word
        from = stack.pop
        to = i
        jumps[from] = to
        jumps[to] = from
      end
    end
    jumps
  end
end

StreetFighter2.new(ARGF.read).run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gen_brain-0.0.1 samples/street_fighter2.rb