查找用户输入字符串中附近,完全或不相符的字母

时间:2017-11-14 12:22:07

标签: ruby-on-rails ruby rubygems

我目前正在为儿童开发Rails中的一个小型修改版Hangman。游戏从文本文件中随机生成一个单词开始,用户必须通过输入四个字母的单词来猜测单词。每个单词由每个字符分开,例如“r”,“e”,“a”,“l”,并返回关于它们如何与单词相关的消息。

随机生成的单词是“真实的”

输入    rlax

输出     Correct, Close, Correct, Incorrect

我尝试过其他我在网上找到的但没有工作的东西,我对Ruby和Rails相当新。希望有人能引导我朝着正确的方向前进。

这是一些代码

def letterCheck(lookAtLetter)   
  lookAHead = lookAtLetter =~ /[[:alpha:]]/ 
end 

def displayWord         
  $ranWordBool.each_index do |i|        
      if($ranWordBool[i])           
          print $ranWordArray[i]        
          $isWin += 1                   
      else
          print "_"     
      end               
  end           
end 

def gameLoop            
  turns = 10            
  turnsLeft = 0     
  lettersUsed = []  
  while(turnsLeft < turns)      
     $isWin = 0                 
     displayWord                    

     if($isWin == $ranWordBool.length)          
       system "cls"                         
       puts "1: Quit"                           
       puts "The word is #{$ranWord} and You Win"   
       puts "Press any key to continue"     
       return               
     end            

     print "\n" + "Words Used: "            
     lettersUsed.each_index do |looper|     
     print " #{lettersUsed[looper]} "   
     end                    
     puts "\n" + "Turns left: #{turns - turnsLeft}" 

     puts "Enter a word"        
     input = gets.chomp         
     system "cls"   

     if(input.length != 4)  
       puts "Please enter 4 lettered word"
     elsif(letterCheck(input))  
       if(lettersUsed.include?(input))  
         puts "#{input} already choosen"    
         elsif($ranWordArray.include?(input))
           puts "Close"
           $ranWordArray.each_index do |i|  
             if(input == $ranWordArray[i])
               $ranWordBool[i] = true   
             end    
             if($ranWordBool[i] = true)
               puts "Correct"
             else
               puts "Incorrect"
             end
           end          
          else
            lettersUsed << input        
            turnsLeft += 1          
          end                   
          else
            puts "Not a letter"     
          end                       
        end                         
        puts "You lose"             
        puts "The word was #{$ranWord}"     
        puts "Press any key to continue"    
      end           

      words = []            
      File.foreach('words.txt') do |line|       
      words << line.chomp               
      end           

      while(true)
        $ranWord = words[rand(words.length) + 1]    
        $ranWordArray = $ranWord.chars          
        $ranWordBool = []                           
        $ranWordArray.each_index do |i|             
        $ranWordBool[i] = false         
      end           

      system "cls"          
      gameLoop
      input = gets.chomp
      shouldQuit(input) 
    end 

1 个答案:

答案 0 :(得分:1)

类似的东西:

You are able to Disable **Autolayout** in view controller But there is no direct way to disable **Auto-Resizing** in iOS. 

You need to manage PIN in **Auto-Resizing**. Set appropriate PIN of relevant object. Please review attached screenshot for **Auto-Resizing**.

Happy Coding! :)