ruby - Hangman game with a bug in my choice method -


excuse global variables, hangman project working on. working should except 1 part of method.

def choice(n)   $random_word2.each_with_index |i,index|     if == n       $cipher[index] =      end    end      if n == $random_word       puts "you win"     end     if $random_word2.include?(n) == false       $counter -= 1       display       puts "incorrect guess"     end   puts $cipher.join  end 

specifically these lines. program print out $random_word know since random. if type whole word should print "you win!" instead skips last "if" , accepts incorrect guess. tried different variables , works, leads me believe $random_word variable changes different random word @ point? lost, prints word, type in exact same thing , somehow it's not == .

if n == $random_word   puts "you win" 

this whole program.

 load 'display.rb'  class hangman   attr_accessor :name   @name = name    def initialize     puts "what name?"     @name = gets.chomp     puts " ################################################                    hangman ################################################                 _________               |                       |       |               |       o               |      /|\\               |       |               |      / \\               |               ----------------- welcome #{@name} hangman. computer generate 5-12 letter random word. try guess word 1 letter @ time. try solve puzzle before time runs out!    "   gameplay.new   end end  class gameplay   def initialize     $array = []     file.foreach('5text.txt') |x| $array << x if (x.chomp.length >= 5 , x.chomp.length <= 12) end     $random_word = $array.sample     $random_word2 = $random_word.split(//)     puts $random_word2.join     $cipher = $random_word.gsub(/[a-z]/, '*').split(//)     puts $cipher.join    def choice(n)     $random_word2.each_with_index |i,index|       if == n         $cipher[index] =        end      end        if n == $random_word2.join.to_s         puts "you win"       end       if $random_word2.include?(n) == false         $counter -= 1         display         puts "incorrect guess"       end       if $counter == 0         puts "you lose!!!!!!"         puts "would start game? y/n"         new_game = gets.chomp         if new_game == "y"           hangman.new         end       end          puts $cipher.join  end   $counter = 5 while $counter > 0 choice(gets.chomp)     end   end end hangman.new 

change

file.foreach('5text.txt') |x|   chomped = x.chomp   $array << chomped if (chomped.length >= 5 , chomped.length <= 12) end 

because words have \n not chomped in origin program.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -