All pastes #610946 Raw Edit

Hangman

public ruby v1 · immutable
·published 2007-07-09 18:26 UTC
rendered paste body
dict = File.open("/usr/share/dict/cracklib-small",'r')i = 0LIVES = 6@guesses = ''wordb = Array.newposs = Array.new@words = Array.newguessed = falseSTACK = []class Label  attr_accessor :name;  attr_accessor :block;    def initialize(name, block);    @name  = name    @block = block  end  def ==(sym)    @name == sym  endendclass Goto < Exception;  attr_accessor :label  def initialize(label); @label = label; endenddef label(sym, &block)  STACK.last << Label.new(sym, block)enddef frame_start  STACK << []enddef frame_end  frame = STACK.pop  idx   = 0  begin    for i in (idx...frame.size)      frame[i].block.call if frame[i].block    end  rescue Goto => g    idx = frame.index(g.label)    retry  endenddef goto(label)  raise Goto.new(label)endclass Array	def count		count = 0		self.each do count += 1 end		return count	endendclass String	def each_char		if block_given?			scan(/./m) do |x|      		  yield x      		end		else			scan(/./m)	endend	def count_char(char)		a = 0		self.each_char do |ch|			if ch == char then a+=1 end		end		return a	endenddict.each_line do |word|	@words[i] = word	i+=1endj = 0print "Word length: "length = gets.to_iwhile j < length	wordb[j] = '_'	print '_'	j+=1endprint "\n"j+=1x,y = 0,0while x < i	if @words[x].length != j 		poss[y] = @words[x]		y+=1	end 	x+=1end@words = @words - possdef guess()	char = 'a'	x = 0	y = Hash.new(0)		while x < @words.count		while char != 'aa'			if @guesses !~ /#{char}/ 				y[char] += @words[x].count_char(char) 			end			char = char.succ		end		x+=1		char = 'a'	end	x = y.sort {|a,b| b[1]<=>a[1]}	@guesses = "#{@guesses}#{x[0][0]}"	return  x[0][0]endframe_startlabel(:a) {	@g = guess()	print "Guess: #{@g}\nCorrect? "	ans = gets.chomp	case ans	when 'y','yes':		print "How many times? "		t = gets.to_i		x = 0		while x < t			print "Position: "			pos = gets.to_i			wordb[pos-1] = @g			x+=1		end		print "#{wordb}\n"	when 'n','no':		puts "Damn..."	else		puts "Please type yes or no"	end}if @g !~ /[a-z]*/ then label(:b){goto :a} endframe_end