第11届浙江大学校赛比赛点评beta » ZOJ3480gen

ZOJ3480gen
ZOJ3480gen.rb


class Array
	def rand
		at(Kernel.rand(size))
	end
end

def randstr
	str = ['_', *'a'..'Z', *'A'..'Z'].rand
	rand(20).times{ str << ['_', *'0'..'9', *'a'..'Z', *'A'..'Z'].rand }
	return str
end

class Class
	def initialize(name, parent = nil)
		parent = Class.new(parent) if parent.is_a? String
		@name = name
		@parent = parent
		@depth = parent.nil? ? 0 : parent.depth.succ
		@methods = ['undef']
	end

	def name
		@name
	end

	def depth
		@depth
	end

	def parents
		(@parent.nil? ? [] : @parent.parents) << self
	end

	def add(method)
		@methods << method
		"#{name}.#{method}"
	end

	def rand_call
		Kernel.rand(@depth + 1) == 0 ? @methods.rand : @parent.rand_call
	end

	def rand_method
		"#{name}.#{@methods.rand}"
	end

	def to_s
		"class #{name}#{@parent.nil? ? '' : ":#{@parent.name}"}"
	end
end

cl = []
ml = %w(new delete gao _ __ NEW Delete set_x get_x setY getY def undef call invoke class)
ARGV << 10000

puts 'begin'
ARGV[0].to_i.times do
	if cl.empty? or rand(5) == 0
		# class (1/5)
		c = Class.new(
			cl.any? && rand(4) == 0 ?
				cl.rand.name :	# old class (oops)
				randstr,		# new class
			cl.any? && rand(3) > 0 ?
				cl.rand	:		# good parent
				rand(2) > 0 ?
					nil	:		# no parent
					randstr		# bad parent (oops)
		)
		next if c.depth > 20
		cl << c
		puts c.to_s
		while rand(3) > 0
			puts "def #{c.add(ml.rand)}"
		end
	elsif rand(4) == 0
		# undef (1/5)
		if rand(5) == 0
			puts "undef #{rand(2) == 0 ? cl.rand.name : randstr}.#{rand(2) == 0 ? ml.rand : randstr}"	# oops
		elsif rand(2) == 0
			c = cl.rand
			puts "undef #{c.name}.#{c.rand_call}"
		else
			puts "undef #{cl.rand.rand_method}"
		end
	elsif rand(3) == 0
		# def (1/5)
		if rand(5) == 0
			puts "def #{randstr}.#{rand(2) == 0 ? ml.rand : randstr}"	# oops
		elsif rand(2) == 0
			puts "def #{cl.rand.add(randstr)}"							# def
		else
			c = cl.rand
			puts "def #{c.name}.#{c.rand_call}"							# def/redef
		end
	else
		# invoke (2/5)
		if rand(5) == 0
			puts "call #{rand(2) == 0 ? cl.rand.name : randstr}.#{rand(2) == 0 ? ml.rand : randstr}"	# oops
		elsif rand(4) == 0
			puts "call #{cl.rand.rand_method}"
		else
			c = cl.rand
			puts "call #{c.name}.#{c.rand_call}"
		end
	end
end
puts 'end'
puts ''
Leave a Reply