All pastes #374955 Raw Edit

Someone

public ruby v1 · immutable
#374955 ·published 2007-02-27 22:12 UTC
rendered paste body
# wordsearchclass WordSearch	def initialize(words=%w{					one						two					three					four					five					six					seven					eight					nine					ten					eleven					twelve					thirteen					fourteen					fifteen					sixteen					seventeen					eighteen					nineteen					twenty										}, 					width = 30,					height = 20)		@words = words.map {|x| x.upcase}		# make blank grid		@width = width		@height = height		@grid = []		1.upto height do |h|			@grid << " " * width			end		# put all the words in		@words.each do |w|			tries = 0			begin				tries += 1				down = (-4..3).to_a[rand*8] % 3 - 1				right = (-4..3).to_a[rand*8] % 3 - 1				x_lim = (@width - right * w.length) % (@width )				y_lim = (@height - down * w.length) % (@height )				# <eugh reason="This is not very elegant">				case right					when -1						x = x_lim + ((@width-1 - x_lim) * rand).to_i					when 0						x = ((@width-1) * rand).to_i					else down == 1						x = (x_lim * rand).to_i				end				case down					when -1					y = y_lim + ((@height-1 - y_lim) * rand).to_i				when 0					y = ((@height-1) * rand).to_i					else 1					y = (y_lim * rand).to_i				end				# </eugh>				count = 0				w.each_byte do |b|					if @grid[y][x] == 32 or @grid[y][x] == b						@grid[y][x] = b 					else						throw 					end					count += 1					y += down					x += right				end			rescue 				# give up if we are taking too long				retry if tries <= 16			end		end				# print out two versions		# one with just answers and one with random letters		@grid.each do |l|			l.split(//).each do |c|				print c == " " ? "." : c 			end			puts		end		puts		@grid.each do |l|			l.split(//).each do |c|				print c == " " ? ("A".."Z").to_a[rand*26] : c			end.join			puts		end	endend# testsrequire 'test/unit'class TestOne < Test::Unit::TestCase	def test_everything		error_count = 0		test_count = 20		test_count.times do			begin				WordSearch.new			rescue 				puts "Error!"				error_count += 1				retry			end		end		puts "#{error_count} errors of #{test_count}"	endend# main codeWordSearch.new