dict = File.open("/usr/share/dict/cracklib-small",'r') i = 0 LIVES = 6 @guesses = '' wordb = Array.new poss = Array.new @words = Array.new guessed = false STACK = [] class Label attr_accessor :name; attr_accessor :block; def initialize(name, block); @name = name @block = block end def ==(sym) @name == sym end end class Goto < Exception; attr_accessor :label def initialize(label); @label = label; end end def label(sym, &block) STACK.last << Label.new(sym, block) end def frame_start STACK << [] end def 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 end end def goto(label) raise Goto.new(label) end class Array def count count = 0 self.each do count += 1 end return count end end class String def each_char if block_given? scan(/./m) do |x| yield x end else scan(/./m) end end def count_char(char) a = 0 self.each_char do |ch| if ch == char then a+=1 end end return a end end dict.each_line do |word| @words[i] = word i+=1 end j = 0 print "Word length: " length = gets.to_i while j < length wordb[j] = '_' print '_' j+=1 end print "\n" j+=1 x,y = 0,0 while x < i if @words[x].length != j poss[y] = @words[x] y+=1 end x+=1 end @words = @words - poss def 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] end frame_start label(: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} end frame_end